標籤:
緩衝一個List 泛型結構
1.顯示
var s = Get("personsl"); foreach (var item in s) { Response.Write(item.Name); }
2.獲得資料
//獲得資料 public List<Personal> Get(string key) { List<Personal> list = DTcms.Common.CacheHelper.Get<List<Personal>>(key); if (list == null || list.Count == 0) { DTcms.Common.CacheHelper.Insert("personsl", Create(), 30); list = DTcms.Common.CacheHelper.Get<List<Personal>>(key); } return list; }
3.建立資料來源
//建立資料來源 public List<Personal> Create() { List<Personal> list = new List<Personal>(); for (int i = 0; i < 10; i++) { Personal p = new Personal(i.ToString(), "xiaoming" + i); list.Add(p); } return list; }
//資料對象 public class Personal { public string ID { get; set; } public string Name { get; set; } public Personal(string id, string name) { this.ID = id; this.Name = name; } }
4.查看緩衝個數
//異常不能樣本話的
Response.Write(new System.Web.Caching.Cache().Count);
Response.Write(HttpRuntime.Cache.Count);
5.Cache 的絕對到期與滑動到期
絕對到期:設定絕對到期時間 到了指定時間以後會失效。(類似Cookie機制)
相對到期也稱滑動到期:設定相對到期時間 指定時間內無訪問會失效。(類似Session機制)
HttpRuntime.Cache與HttpContext.Current.Cache 為同一個對象
HttpRuntime.Cache.Add 存在相同的鍵會異常,返回緩衝成功的對象 HttpRuntime.Cache.Insert存在相同的鍵會替換無傳回值
HttpRuntime.Cache["key"] 使用字典的方式也可以讀取和設定
HttpRuntime.Cache.Insert(key, value, null, DateTime.Now.AddSeconds(seconds), TimeSpan.Zero); //設定絕對到期時間 到了指定時間以後會失效 ps: TimeSpan.Zero == System.Web.Caching.Cache.NoSlidingExpiration
HttpRuntime.Cache.Insert(key, value, null, DateTime.MaxValue, TimeSpan.FromSeconds(seconds)); //設定相對到期時間 指定時間內無訪問會失效 ps: DateTime.MaxValue == System.Web.Caching.Cache.NoAbsoluteExpiration
ASP.NET-【緩衝】-使用ASP.NET緩衝