在普通aspx.cs代码里可以用:
Cache cache = new Cache();
但在XXXX.CS里,就不能用上面的方式了,得用:
永不时间过期
Ht***ontext.Current.Cache.Insert("Name", "王翔", null, Da***ime.MaxValue, Ti***pan.Zero, Ca***ItemPriority.NotRemovable, null);
Name:Key
王翔:Value
null:表示没有缓存依赖项
Da***ime.MaxValue:时间的最大值(9999-99-99 12:59:59),表示不使用绝对时间过期策略
Ti***pan.Zero:表示不使用平滑过期
Ca***ItemPrority.NotRemovable:表示优先权为不删除该Cache
null:不怎么用,就null吧
ASP.NET
绝对时间过期 (10秒后,自动过期)
Ht***ontext.Current.Cache.Insert("Name", "王翔", null, Da***ime.Now.AddSeconds(10), Ti***pan.Zero, Ca***ItemPriority.NotRemovable, null);
平滑时间过期 (连续10秒没有访问该缓存,则自动过期)
Ht***ontext.Current.Cache.Insert("Name", "王翔", null, Da***ime.MaxValue, Ti***pan.FromSeconds(10));
缓存的更新策略
if (Ht***ontext.Current.Cache["UserCacheList"] != null)
{
ht = (Hashtable)Ht***ontext.Current.Cache["UserCacheList"];
ht.Add(uId, Ht***ontext.Current.Cache["User" + uId]);
}
else
{
ht.Add(uId, Ht***ontext.Current.Cache["User" + uId]);
//Ht***ontext.Current.Cache["UserCacheList"] = ht;
Ht***ontext.Current.Cache.Insert("UserCacheList", ht, null, Da***ime.MaxValue, Ti***pan.Zero, Ca***ItemPriority.NotRemovable, null);
}
上一篇: 回收站玩“失踪” 简单方法一分钟找回
下一篇: C# Dictionary 终极使用方法