Cache 缓存
参考 : http://www.cnblogs.com/fish-li/archive/2011/12/27/2304063.html#_label2
http://www.jb51.net/article/22516.htm
这里只说要点 :
cache 是线程安全的. 原理也是用lock,不过有做过优化.
引用有3个way , HttpRuntime.Cache , HttpContext.Cache , Page.Cache 都是同一个对象
cache["key"] 读写内部就是调用了 .Get() 和 .Insert() 方法
.Insert() 会覆盖原有的cache , .Add() 则不会哦.
.net大约每20秒会检查一次cache过期,也就是说即使你设置一个绝对过期时间,他也不会那么准过期的.
Insert 时可以定义很多东西 parameters
1. key
2.value (object 类型)
3.依赖项(就是你依赖的对象如果改变了,cache就会失效,参考上面的link)
4.绝对过期时间 (DateTime 类型) 如果有设置了5. 那么这里请放 System.Web.Caching.Cache.NoAbsoluteExpiration
5.最后一次访问后多久会过期 (TimeSpan 类型) , 如果有设置了4. 那么这里请放 System.Web.Caching.Cache.NoSlidingExpiration
6.移除优先 System.Web.Caching.CacheItemPriority (Enum 类型), 可以设置永不移除
7.new System.Web.Caching.CacheItemRemovedCallback(delegate(string key, object value, System.Web.Caching.CacheItemRemovedReason reason)) //可以直接放一个匿名委托
上面的是 CacheItemRemovedCallback ,触发时 cache 已经被移除了 , 还有一种是在要移除之前触发的
1,2,3,4,5 一样
6.
delegate(string key, CacheItemUpdateReason reason, out object value, out CacheDependency dependency, out DateTime exipriation, out TimeSpan slidingExpiration)
{
var old = Cache["key"];
dependency = null;
exipriation = Cache.NoAbsoluteExpiration;
slidingExpiration = Cache.NoSlidingExpiration;
value = "haha";
});

浙公网安备 33010602011771号