添加 Cache[Key]=object or Cache.Insert
移除 Cache.Remove(key)
1、將值直接寫入Cache
| 代碼如下 |
複製代碼 |
HttpContext.Current.Cache["One"] = "1"; |
使用'絕對到期'方式處理緩衝,到期時間為:9999年12月31日 (不推薦使用該方法處理緩衝,並且應在適當的時候清空緩衝Key)
2、使用Insert(String, Object)插入Cache
| 代碼如下 |
複製代碼 |
string cacheKey = "Two"; object cacheValue = HttpContext.Current.Cache.Get(cacheKey); if(cacheValue == null) { cacheValue = WebConfigurationManager.ConnectionStrings["ApplicationServices"].ConnectionString; HttpContext.Current.Cache.Insert(cacheKey, cacheValue); } //顯示指定緩衝的Key 與 Value this.ShowMessage(cacheKey, cacheValue.ToString()); |
3、使用Insert(String, Object, CacheDependency, DateTime, TimeSpan)插入Cache
| 代碼如下 |
複製代碼 |
string cacheKey = "__cache__students"; DataSet dataSet = this.Cache.Get(cacheKey) as DataSet; if(dataSet == null) { dataSet = new DataSet(); //載入XML並填充至DataSet dataSet.ReadXml(this.Server.MapPath(@"XMLFile.xml")); //加入緩衝,並設定'絕對到期時間'為5分鐘 this.Cache.Insert(cacheKey, dataSet, null, DateTime.Now.AddMinutes(5), System.Web.Caching.Cache.NoSlidingExpiration); } //綁定DataGrid資料 grdDefault.DataSource = dataSet; 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 public partial class PriorityAndCallbackDemo : System.Web.UI.Page { #region 靜態欄位 static bool CacheItemRemoved = false; static CacheItemRemovedReason Reason; static string CacheItemKey = "fw__cache__students"; #endregion #region 事件處理 //頁面載入 protected void Page_Load(object sender, EventArgs e) { //快取項目已移除 if(PriorityAndCallbackDemo.CacheItemRemoved) { ltMessage.Text = string.Format("Key={0}已從緩衝移出,原因為:{1}", PriorityAndCallbackDemo.CacheItemKey, PriorityAndCallbackDemo.Reason.ToString()); } } //'添加緩衝'按鈕 點擊事件 處理 protected void btnAddCache_Click(object sender, EventArgs e) { DataSet dataSet = this.Cache.Get(PriorityAndCallbackDemo.CacheItemKey) as DataSet; if(dataSet == null) { dataSet = new DataSet(); dataSet.ReadXml(this.Server.MapPath(@"XMLFile.xml")); //使用 Web.config 作為緩衝到期依賴項 CacheDependency dependency = new CacheDependency(this.Server.MapPath(@"Web.config"), DateTime.Now); //加入緩衝,設定優先順序為預設層級 this.Cache.Insert(PriorityAndCallbackDemo.CacheItemKey, dataSet, dependency, DateTime.Now.AddMinutes(1), System.Web.Caching.Cache.NoSlidingExpiration, CacheItemPriority.Default, new CacheItemRemovedCallback(this.CacheItemRemovedHandler)); } //綁定GridView資料 grdDefault.DataSource = dataSet; grdDefault.DataBind(); } //'移除緩衝'按鈕 點擊事件 處理 protected void btnRemoveCache_Click(object sender, EventArgs e) { if(this.Cache[PriorityAndCallbackDemo.CacheItemKey] != null) { this.Cache.Remove(PriorityAndCallbackDemo.CacheItemKey); } } #endregion #region 私人方法 //快取項目移除事件處理 private void CacheItemRemovedHandler(string key, object value, CacheItemRemovedReason relason) { PriorityAndCallbackDemo.CacheItemRemoved = true; PriorityAndCallbackDemo.Reason = relason; } #endregion } |
該方法較重要的兩個參數為CacheItemPriority及CacheItemRemovedCallback
CacheItemPriority 快取項目優先順序,當伺服器記憶體不夠時,優先順序越高的項越不容易被移除
CacheItemRemovedCallback 該參數為委託類型,當快取項目被移除時所調用,包含Reason參數用於表示快取項目被移除的原因
【我是怎麼用的】
首先理解緩衝策略。可調到期策略 和 絕對到期策略。注意,兩則不能同時使用
使用可調到期策略,需要將absoluteExpiration=DateTime.MaxValue ,TimeSpan .FromMinutes(10)設定項目只有在10分鐘內不被使用才會被移除
| 代碼如下 |
複製代碼 |
Cache.Insert("data", "123", null , DateTime.MaxValue, TimeSpan.FromMinutes(10)); |
絕對策略,如天氣報告,將資訊儲存60分鐘
| 代碼如下 |
複製代碼 |
Cache.Insert("data", "123", null , DateTime.Now.AddMinutes(60), TimeSpan.Zero); |
緩衝依賴。
即一個緩衝的失效依賴另外一個object。這裡的object可以指另外一個緩衝,或者一個檔案,或者....
類:CacheDependency 命名空間 System.Web.Caching.CacheDependency依賴於其它快取項目
| 代碼如下 |
複製代碼 |
System.Web.Caching.CacheDependency cacheDependency = new System.Web.Caching.CacheDependency (null, new string [] { "time" }); Cache.Insert( "number", ++num, cacheDependency); 依賴於檔案或檔案夾 System.Web.Caching. CacheDependency cacheDependency = new System.Web.Caching.CacheDependency ( "test.xml"); 當test.xml檔案刪除、更新時自動從緩衝中移除 System.Web.Caching.CacheDependency cacheDependency = new System.Web.Caching.CacheDependency(null, new string[] { "time" }); Cache.Insert("test", "123", cacheDependency); 移除項目回調 Cache.Insert("test", "123", null , DateTime.Now.AddSeconds(10), TimeSpan.Zero, new CacheItemUpdateCallback(Test)); private void Test(string key, CacheItemUpdateReason reason, out object expensiveObject, out CacheDependency dependency, out DateTime absoluteExpiration, out TimeSpan slidingExpiration) { } |