您好,登錄后才能下訂單哦!
這篇文章主要介紹“ASP.NET緩存數(shù)據(jù)添加方法是什么”,在日常操作中,相信很多人在ASP.NET緩存數(shù)據(jù)添加方法是什么問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對(duì)大家解答”ASP.NET緩存數(shù)據(jù)添加方法是什么”的疑惑有所幫助!接下來,請(qǐng)跟著小編一起來學(xué)習(xí)吧!
ASP.NET緩存數(shù)據(jù)添加需求概述
ASP.NET使用緩存機(jī)制,將需要大量服務(wù)器資源來創(chuàng)建的對(duì)象存儲(chǔ)在內(nèi)存中。緩存這些類型的資源會(huì)大大改進(jìn)應(yīng)用程序的性能。緩存是有Cache類實(shí)現(xiàn)的,可以通過對(duì)緩存設(shè)置優(yōu)先級(jí)CacheItemPriority枚舉值控制內(nèi)存不夠時(shí)的“清理”優(yōu)先順序。還可以為緩存設(shè)置過期策略,以及為緩存設(shè)置依賴項(xiàng)。
ASP.NET緩存數(shù)據(jù)添加(將數(shù)據(jù)項(xiàng)添加到緩存中)
1、通過鍵值對(duì)添加
Cache["CacheItem"] = "Cached Item";
2、通過Insert 方法添加
Insert 方法向緩存添加項(xiàng),并且已經(jīng)存在與現(xiàn)有項(xiàng)同名的項(xiàng),則緩存中的現(xiàn)有項(xiàng)將被替換。
Cache.Insert("CacheItem", "Cached Item");
3、指定依賴項(xiàng)并添加(對(duì)添加到緩沖中的數(shù)據(jù)項(xiàng)指定依賴項(xiàng))
數(shù)據(jù)項(xiàng)依賴一個(gè)字符串?dāng)?shù)組對(duì)象的情況:
string[] dependencies = { "Dependences" }; Cache.Insert("CacheItem", "Cached Item", new System.Web.Caching.CacheDependency(null, dependencies));
數(shù)據(jù)項(xiàng)依賴一個(gè)XML文件的情況:
Cache.Insert("CacheItem", "Cached Item", new System.Web.Caching.CacheDependency( Server.MapPath("XMLFile.xml")));
數(shù)據(jù)項(xiàng)依賴多個(gè)依賴項(xiàng)的情況:
System.Web.Caching.CacheDependency dep1 = new System.Web.Caching.CacheDependency(Server.MapPath("XMLFile.xml")); string[] keyDependencies2 = { "CacheItem1" }; System.Web.Caching.CacheDependency dep2 = new System.Web.Caching.CacheDependency(null, keyDependencies2); System.Web.Caching.AggregateCacheDependency aggDep = new System.Web.Caching.AggregateCacheDependency(); aggDep.Add(dep1); aggDep.Add(dep2); Cache.Insert("CacheItem", "Cached Item", aggDep);
4、設(shè)置過期策略并添加
添加一分鐘絕對(duì)過期時(shí)間到緩存中:
Cache.Insert("CacheItem", "Cached Item", null, DateTime.Now.AddMinutes(1d), System.Web.Caching.Cache.NoSlidingExpiration);
添加10 分鐘彈性過期時(shí)間到緩存中:
Cache.Insert("CacheItem", "Cached Item", null, System.Web.Caching.Cache.NoAbsoluteExpiration, new TimeSpan(0, 10, 0));
5、設(shè)置優(yōu)先級(jí)并添加
調(diào)用 Insert 方法,從 CacheItemPriority 枚舉中指定一個(gè)值。
Cache.Insert("CacheItem", "Cached Item", null, System.Web.Caching.Cache.NoAbsoluteExpiration, System.Web.Caching.Cache.NoSlidingExpiration, System.Web.Caching.CacheItemPriority.High, null);
6、通過Add方法添加
Add 方法將返回您添加到緩存中的對(duì)象。另外,如果使用 Add 方法,并且緩存中已經(jīng)存在與現(xiàn)有項(xiàng)同名的項(xiàng),則該方法不會(huì)替換該項(xiàng),并且不會(huì)引發(fā)異常。
string CachedItem = (string)Cache.Add("CacheItem", "Cached Item", null, System.Web.Caching.Cache.NoAbsoluteExpiration, System.Web.Caching.Cache.NoSlidingExpiration, System.Web.Caching.CacheItemPriority.Default, null);
到此,關(guān)于“ASP.NET緩存數(shù)據(jù)添加方法是什么”的學(xué)習(xí)就結(jié)束了,希望能夠解決大家的疑惑。理論與實(shí)踐的搭配能更好的幫助大家學(xué)習(xí),快去試試吧!若想繼續(xù)學(xué)習(xí)更多相關(guān)知識(shí),請(qǐng)繼續(xù)關(guān)注億速云網(wǎng)站,小編會(huì)繼續(xù)努力為大家?guī)砀鄬?shí)用的文章!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。