添加 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 與 Valuethis.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及slidingExpirationabsoluteExpiration 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,
更多詳細內容請查看:http://www.111cn.net/net/net/56762.htm
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及CacheItemRemovedCallbackCacheItemPriority 緩存項優先級,當服務器內存不夠時,優先級越高的項越不容易被移除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) { }
新聞熱點
疑難解答