Mastering Caching in Asp.net
private void MyItemRemovedCallBack(string key, object value,
?????????????????? CacheItemRemovedReason reason)
{
??? // remove the item from the cache
??? if (key == "ItemA" ) Cache.Remove("ItemB");
??? else if (key.Equals == "ItemB") Cache.Remove("ItemB");
}
2.?????? Cache dependency on a file.
Cache.Insert("Menu", ds, new System.Web.Caching.CacheDependency(
??????????? Server.MapPath(menuPath)),DateTime.Now.AddMinutes(60),
??????????? TimeSpan.Zero,
??????????? System.Web.Caching.CacheItemPriority.Default,
??????????? new System.Web.Caching.CacheItemRemovedCallback(
????????????????????????????????? CacheItemRemovedCallBack));
3.?????? in asp.net 2.0 we can dependency an item with a databse table.
aspnet_regsql -ed -E -d MyDataBase //enable database to listen to asp.net item which it is Caching.
aspnet_regsql -et -E -d MyDataBase -t MyTable // point out the table caching in cache.
l???????? We need to add these XML to web.config:
<connectionStrings>
??? <add name="ConnectionString"
???????? connectionString="Server=localhost;Database=School;
Trusted_Connection=true"/>
</connectionStrings>
?
<system.web>
??? <caching>
??????? <sqlCacheDependency pollTime="10000" enabled="true" >
??????????? <databases>
??????????????? <add connectionStringName="ConnectionString" name="School"/>
??????????? </databases>
??????? </sqlCacheDependency>
</caching>lt;/caching>
l???????? The Code is :
Cache.Insert("Users", ds, dep);
4.?????? Time-based Dependency
TimeSpan span = new TimeSpan(0,10,0);// ten minute
Cache.Insert("ProductData", LoadDataSet(), nothing, nothing, span)
5.?????? Key-based Dependency
Dim dependency (1) As String dependencyKey(0) = "ProductData" Dim productDataDependency As new CacheDependency(nothing, dependencyKey) Cache.Insert("SalesData", LoadDataSet("Sales"), productDataDependency)
In the above example code, we use the Insert() method to create a new Cache entry named SalesData passing in an instance of a CacheDependency class named productDataDependency which names the Cache key ProductData. Now, whenever ProductData changes, SalesData will be removed from the Cache.
Notice:
Always check if the item exists in the Cache before attempting to use it.
?
總結
以上是生活随笔為你收集整理的Mastering Caching in Asp.net的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 什么是SSID
- 下一篇: 天使投资家李镇樟:如何培养世界级企业家