ASP.NET Cache类 使用方式
1、将值直接写入Cache
HttpContext.Current.Cache["One"] = "1";
使用'绝对过期'方式处理缓存,过期时间为:9999年12月31日 (不推荐使用该方法处理缓存,并且应在适当的时候清空缓存Key)
2、使用Insert(String, Object)插入Cache
1 string cacheKey = "Two";
2 object cacheValue = HttpContext.Current.Cache.Get(cacheKey);
3
4 if(cacheValue == null)
5 {
6 cacheValue = WebConfigurationManager.ConnectionStrings["ApplicationServices"].ConnectionString;
7
8 HttpContext.Current.Cache.Insert(cacheKey, cacheValue);
9 }
10
11 //显示指定缓存的Key 与 Value
12 this.ShowMessage(cacheKey, cacheValue.ToString());
3、使用Insert(String, Object, CacheDependency, DateTime, TimeSpan)插入Cache
1 string cacheKey = "__cache__students";
2
3 DataSet dataSet = this.Cache.Get(cacheKey) as DataSet;
4
5 if(dataSet == null)
6 {
7 dataSet = new DataSet();
8
9 //加载XML并填充至DataSet
10 dataSet.ReadXml(this.Server.MapPath(@"XMLFile.xml"));
11
12 //加入缓存,并设定'绝对过期时间'为5分钟
13 this.Cache.Insert(cacheKey, dataSet, null, DateTime.Now.AddMinutes(5), System.Web.Caching.Cache.NoSlidingExpiration);
14 }
15
16 //绑定DataGrid数据
17 grdDefault.DataSource = dataSet;
18 grdDefault.DataBind();
该方法较重要的两个参数为absoluteExpiration及slidingExpiration
absoluteExpiration DateTime类型,代表绝对过期时间
slidingExpiration TimeSpan类型,代表滑动过期时间
absoluteExpiration与slidingExpiration不能同时使用
例如:设定了absoluteExpiration参数时,slidingExpiration必须设定为System.Web.Caching.Cache.NoSlidingExpiration
反之:设定了slidingExpiration参数时,absoluteExpiration必须设定为System.Web.Caching.Cache.NoAbsoluteExpiration
4、使用Insert(String, Object, CacheDependency, DateTime, TimeSpan, CacheItemPriority, CacheItemRemovedCallback)插入Cache
1 public partial class PriorityAndCallbackDemo : System.Web.UI.Page
2 {
3 #region 静态字段
4 static bool CacheItemRemoved = false;
5 static CacheItemRemovedReason Reason;
6 static string CacheItemKey = "fw__cache__students";
7 #endregion
8
9 #region 事件处理
10 //页面加载
11 protected void Page_Load(object sender, EventArgs e)
12 {
13 //缓存项已移除
14 if(PriorityAndCallbackDemo.CacheItemRemoved)
15 {
16 ltMessage.Text = string.Format("Key={0}已从缓存移出,原因为:{1}", PriorityAndCallbackDemo.CacheItemKey, PriorityAndCallbackDemo.Reason.ToString());
17 }
18 }
19
20 //'添加缓存'按钮 点击事件 处理
21 protected void btnAddCache_Click(object sender, EventArgs e)
22 {
23 DataSet dataSet = this.Cache.Get(PriorityAndCallbackDemo.CacheItemKey) as DataSet;
24
25 if(dataSet == null)
26 {
27 dataSet = new DataSet();
28 dataSet.ReadXml(this.Server.MapPath(@"XMLFile.xml"));
29
30 //使用 Web.config 作为缓存过期依赖项
31 CacheDependency dependency = new CacheDependency(this.Server.MapPath(@"Web.config"), DateTime.Now);
32
33 //加入缓存,设定优先级为默认级别
34 this.Cache.Insert(PriorityAndCallbackDemo.CacheItemKey, dataSet, dependency, DateTime.Now.AddMinutes(1), System.Web.Caching.Cache.NoSlidingExpiration, CacheItemPriority.Default, new CacheItemRemovedCallback(this.CacheItemRemovedHandler));
35 }
36
37 //绑定GridView数据
38 grdDefault.DataSource = dataSet;
39 grdDefault.DataBind();
40 }
41
42 //'移除缓存'按钮 点击事件 处理
43 protected void btnRemoveCache_Click(object sender, EventArgs e)
44 {
45 if(this.Cache[PriorityAndCallbackDemo.CacheItemKey] != null)
46 {
47 this.Cache.Remove(PriorityAndCallbackDemo.CacheItemKey);
48 }
49 }
50 #endregion
51
52 #region 私有方法
53 //缓存项移除事件处理
54 private void CacheItemRemovedHandler(string key, object value, CacheItemRemovedReason relason)
55 {
56 PriorityAndCallbackDemo.CacheItemRemoved = true;
57 PriorityAndCallbackDemo.Reason = relason;
58 }
59 #endregion
60 }
该方法较重要的两个参数为CacheItemPriority及CacheItemRemovedCallback
CacheItemPriority 缓存项优先级,当服务器内存不够时,优先级越高的项越不容易被移除
CacheItemRemovedCallback 该参数为委托类型,当缓存项被移除时所调用,包含Reason参数用于表示缓存项被移除的原因

浙公网安备 33010602011771号