麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁 > 學院 > 開發設計 > 正文

ASP.NET緩存 Cache之數據緩存

2019-11-15 02:26:11
字體:
來源:轉載
供稿:網友

asp.net緩存 Cache之數據緩存

添加 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) { }


發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 国产69精品久久久久久野外 | 草莓视频在线导航 | 国内精品伊人久久久久网站 | 久久精品视频在线免费观看 | 小视频免费在线观看 | 国产中文99视频在线观看 | 久久精品欧美一区二区 | 1024亚洲天堂 | 黄色小视频免费在线观看 | 免费黄色入口 | 午夜精品小视频 | 精品二区在线观看 | 欧美囗交 | 毛片视频网站在线观看 | 成人激情视频网站 | 欧美成人黄色小视频 | av在线直播观看 | h网站在线观看 | 91九色电影 | www.成人在线 | 久久久久国产一区二区三区不卡 | 久久蜜臀一区二区三区av | 久久精品小短片 | 国产一区日韩精品 | 91国内精品久久久久免费影院 | 精品亚洲成a人在线观看 | 成人毛片100部 | 欧美黄色一级带 | 蜜桃网站在线观看 | 久章草在线观看 | 一级色毛片| 巨根插入| 欧美天堂一区 | 国产精品区一区二区三区 | 污片视频网站 | 性片免费看 | 日韩.www | 精品国产乱码久久久久久丨区2区 | 欧美一级黑人 | 亚洲免费视| 国产亚洲高清在线精品不卡 |