Enterprise Library 4.1学习笔记1——缓存应用程序块

要点:
添加引用
using Microsoft.Practices.EnterpriseLibrary.Caching;

private ICacheManager primitivesCache;

声明一个缓存实例
this.primitivesCache = CacheFactory.GetCacheManager();

            
this.primitivesCache = CacheFactory.GetCacheManager("manager");

赋值
string id="ProductOneId";
string name = "ProductOneName";
int price = 50;

Product product 
= new Product(id, name, price);
创建一个product对象实例
primitivesCache.Add(product.ProductID, product, CacheItemPriority.Normal, null,
                     
new SlidingTime(TimeSpan.FromMinutes(5)));
将该对象添加到缓存中去。
注意:
add方法第一个参数为该缓存的key,第二个参数为value,第三个参数为优先级别,包括“hight”,“low”,“none”等级别。下一个参数一般为空。最后一个参数可以表示时间间隔。可以为空如:
new AbsoluteTime(enterNewItemForm.AbsoluteTime) 指超过指定的绝对时间
new ExtendedFormatTime("0 0 * * *")指每间隔多久的时间(如每天午夜,每半个小时等)
new FileDependency("DependencyFile.txt")指监视文件改变就过期
new SlidingTime(TimeSpan.FromMinutes(1))指延时多久过期

移除缓存
primitivesCache.Remove(key);
清空缓存
primitivesCache.Flush();

 
posted @ 2010-04-10 13:12  Rice wheat  阅读(277)  评论(0编辑  收藏  举报