文章详情

  • 游戏榜单
  • 软件榜单
关闭导航
热搜榜
热门下载
热门标签
php爱好者> php文档>Caching For .Net Framework Applications(一)

Caching For .Net Framework Applications(一)

时间:2010-11-22  来源:传说哥

  (1)最简单的Cache的使用:

Cache["Time"] = DateTime.Now.ToString();
lblCacheTime.Text = Cache["Time"].ToString()
Cache.Remove("Time");

 

  (2):基于硬盘上的文件变化的一种Cache的过期策略

CacheDependency dependucy = new CacheDependency(“customdatasource.xml");
                Cache.Insert("Time", strDateTime, dependucy); 

  当CustomDataSource.xml中的内容发生变化时Cache中Time为Key的缓存就会被移除。当然我们还基于一批文件。CacheDependency这个类中提供很多构造函数,有兴趣的朋友可以去查一下MSDN。

  (3):基于其它缓存的Key的一种Cache过期策略

  

代码 // Create a cache entry.
Cache["key1"] = "Value 1";

// Make key2 dependent on key1.
String[] dependencyKey = new String[1];
dependencyKey[0] = "key1";
CacheDependency dependency = new CacheDependency(null, dependencyKey);

Cache.Insert("key2", "Value 2", dependency);

 

  (4):基于相对时间和绝对时间的一种Cache过期策略

 

代码 /// Absolute expiration
Cache.Insert("CachedItem", item, null, DateTime.Now.AddSeconds(5), 
    Cache.NoSlidingExpiration);
/// Sliding expiration
Cache.Insert("CachedItem", item, null, Cache.NoAbsoluteExpiration, 
    TimeSpan.FromSeconds(5));
  

  (5)当Cache过期时被执行的时候,还可以执行一些函数,来做一些工作。

 

代码 CacheItemRemovedCallback onRemove = new 
CacheItemRemovedCallback(this.RemovedCallback);
Cache.Insert("CachedItem", 
    item, 
    null,
    Cache.NoAbsoluteExpiration,
    Cache.NoSlidingExpiration,
    CacheItemPriority.Default,
    onRemove);

// Implement the function to handle the expiration of the cache.
public void RemovedCallback(string key, object value, CacheItemRemovedReason r)
{
   // Test whether the item is expired, and reinsert it into the cache.
   if (r == CacheItemRemovedReason.Expired)
   {
      // Reinsert it into the cache again.
      CacheItemRemovedCallback onRemove = null;
      onRemove = new CacheItemRemovedCallback(this.RemovedCallback);
      Cache.Insert(key,  
                       value,
                       null,
                       Cache.NoAbsoluteExpiration,
                       Cache.NoSlidingExpiration,
                       CacheItemPriority.Default,
                       onRemove);
   }
}
    本文只是记录和总结了一下看到的信息,接下的文章将介绍Cache的一些实际的应用。  
相关阅读 更多 +
排行榜 更多 +
夕鸟

夕鸟

生活实用 下载
partyplay

partyplay

聊天通讯 下载
婚礼纪

婚礼纪

生活实用 下载