[英]ASP.Net Cache problem/question. Where and how to cache. | Costum ADO.Net code
i am developing an ASP.Net e-commerce application. 我正在开发ASP.Net电子商务应用程序。 I use ASP.Net Web Forms. 我使用ASP.Net Web表单。 When i finish the overall application i was consider about Cache some data so the application performance will increase. 当我完成整个应用程序时,我考虑过缓存一些数据,因此应用程序性能将提高。 (bad idea...i must have consider that from the beginning) (糟糕的主意...我必须从一开始就考虑到这一点)
I don't use build-in controls such datagrid. 我不使用内置控件,例如datagrid。 I neather use MS-Sql. 我什至使用MS-Sql。
I use MySql server us database and custom UserControls that i develop. 我使用MySql server us数据库和我开发的自定义UserControls。 (Custom ADO.Net code) (自定义ADO.Net代码)
To get data from the Database i use : 要从数据库中获取数据,请使用:
An object that represent the fields of the table IE : If my table (MyTable) has : 代表表IE字段的对象:如果我的表(MyTable)具有:
ID , Title, Date
I create will create an object like the above : 我创建将创建一个类似上面的对象:
public class MyTable{
public int ID {get;set;}
public string Title {get;set;}
public Datetime Date {get;set;}
}
To manage fields from the table i use something like above: 要管理表中的字段,我使用类似上面的内容:
public class MyTableDatabaseManager{
//Insert a row
public bool insertMyTable(MyTable item){
//...Open a mysql connection , do job , close it.
}
//Update a row
public bool updateMyTable(MyTable item){
//...Open a mysql connection , do job , close it.
}
//Delete a row
public bool deleteMyTable(MyTable item){
//...Open a mysql connection , do job , close it.
}
//Get one row
public MyTable deleteMyTable(int id){
//...Open a mysql connection , do job , close it.
}
//Get some rows or all of them
public List<MyTable> deleteMyTable(int limit,int page,bool Ordering){
//...Open a mysql connection , do job , close it.
}
}
My questions are: 我的问题是:
How can i store some data in the cache so i have not to communicate all the time with the database? 如何将某些数据存储在缓存中,这样我就不必一直与数据库进行通信?
How can i reset the cache programmaticaly when data is changed? 更改数据后,如何以编程方式重置缓存?
(I know how, but where i put the code:P it can be in the code-behind files when i do changes or insert some values, or somewhere else?) (我知道如何,但是当我进行更改或插入一些值或其他位置时,它可以放在代码隐藏文件中的什么位置?)
Output cache don't work for me, because the most of the provided data are user-specific. 输出缓存对我不起作用,因为提供的大多数数据都是特定于用户的。 (about 80% of them) (其中约80%)
Thank you very much:) 非常感谢你:)
Simply insert into the cache with the key being your userId or whatever you want to vary it by. 只需将key插入为您的userId或您要更改它的任何内容,即可将其插入缓存。
Cache.Insert(yourUserIdOrWhateverKey, yourObject);
http://msdn.microsoft.com/en-us/library/6hbbsfk6%28v=VS.100%29.aspx http://msdn.microsoft.com/zh-cn/library/6hbbsfk6%28v=VS.100%29.aspx
Note this cache viewer (also check out the msdn link above) 注意此缓存查看器(也请查看上面的msdn链接)
http://authors.aspalliance.com/aldotnet/examples/cacheviewer.aspx http://authors.aspalliance.com/aldotnet/examples/cacheviewer.aspx
To remove you can just call 要删除,您可以致电
Cache.Remove(yourKey);
See: http://aspdotnetfaq.com/Faq/How-to-clear-your-ASP-NET-applications-Cache.aspx 请参阅: http : //aspdotnetfaq.com/Faq/How-to-clear-your-ASP-NET-applications-Cache.aspx
To check if your item is in the cache simply do 要检查您的物品是否在缓存中,只需执行
var yourCachedObject = (CustomClass)Cache["YourUserKey"]; if(yourCachedObject == null) { //it wasnt in the cache }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.