自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

ASP.NET緩存數(shù)據(jù)添加方法一覽

開發(fā) 后端
ASP.NET使用緩存機(jī)制,將需要大量服務(wù)器資源來創(chuàng)建的對象存儲在內(nèi)存中。本文介紹ASP.NET緩存數(shù)據(jù)添加的六種方法。

ASP.NET緩存數(shù)據(jù)添加需求概述

ASP.NET使用緩存機(jī)制,將需要大量服務(wù)器資源來創(chuàng)建的對象存儲在內(nèi)存中。緩存這些類型的資源會大大改進(jìn)應(yīng)用程序的性能。緩存是有Cache類實(shí)現(xiàn)的,可以通過對緩存設(shè)置優(yōu)先級CacheItemPriority枚舉值控制內(nèi)存不夠時(shí)的“清理”優(yōu)先順序。還可以為緩存設(shè)置過期策略,以及為緩存設(shè)置依賴項(xiàng)。

ASP.NET緩存數(shù)據(jù)添加(將數(shù)據(jù)項(xiàng)添加到緩存中)

1、通過鍵值對添加

  1. Cache["CacheItem"] = "Cached Item"

2、通過Insert 方法添加

Insert 方法向緩存添加項(xiàng),并且已經(jīng)存在與現(xiàn)有項(xiàng)同名的項(xiàng),則緩存中的現(xiàn)有項(xiàng)將被替換。

  1. Cache.Insert("CacheItem""Cached Item"); 

3、指定依賴項(xiàng)并添加(對添加到緩沖中的數(shù)據(jù)項(xiàng)指定依賴項(xiàng))

數(shù)據(jù)項(xiàng)依賴一個(gè)字符串?dāng)?shù)組對象的情況:

  1. string[] dependencies = { "Dependences" };  
  2. Cache.Insert("CacheItem",  
  3.     "Cached Item",  
  4.     new System.Web.Caching.CacheDependency(null, dependencies)); 

數(shù)據(jù)項(xiàng)依賴一個(gè)XML文件的情況:

  1. Cache.Insert("CacheItem""Cached Item",  
  2.     new System.Web.Caching.CacheDependency(  
  3.     Server.MapPath("XMLFile.xml"))); 

數(shù)據(jù)項(xiàng)依賴多個(gè)依賴項(xiàng)的情況:

  1. System.Web.Caching.CacheDependency dep1 =   
  2.     new System.Web.Caching.CacheDependency(Server.MapPath("XMLFile.xml"));  
  3. string[] keyDependencies2 = { "CacheItem1" };  
  4. System.Web.Caching.CacheDependency dep2 =   
  5.     new System.Web.Caching.CacheDependency(null, keyDependencies2);  
  6. System.Web.Caching.AggregateCacheDependency aggDep =   
  7.     new System.Web.Caching.AggregateCacheDependency();  
  8. aggDep.Add(dep1);  
  9. aggDep.Add(dep2);  
  10. Cache.Insert("CacheItem""Cached Item", aggDep); 

4、設(shè)置過期策略并添加

添加一分鐘絕對過期時(shí)間到緩存中:

  1. Cache.Insert("CacheItem""Cached Item",  
  2.     null, DateTime.Now.AddMinutes(1d),   
  3.     System.Web.Caching.Cache.NoSlidingExpiration); 

添加10 分鐘彈性過期時(shí)間到緩存中:

  1. Cache.Insert("CacheItem""Cached Item",  
  2.     null, System.Web.Caching.Cache.NoAbsoluteExpiration,  
  3.     new TimeSpan(0, 10, 0)); 

5、設(shè)置優(yōu)先級并添加

調(diào)用 Insert 方法,從 CacheItemPriority 枚舉中指定一個(gè)值。 

  1. Cache.Insert("CacheItem""Cached Item",  
  2.     null, System.Web.Caching.Cache.NoAbsoluteExpiration,  
  3.     System.Web.Caching.Cache.NoSlidingExpiration,  
  4.     System.Web.Caching.CacheItemPriority.High, null); 

6、通過Add方法添加

Add 方法將返回您添加到緩存中的對象。另外,如果使用 Add 方法,并且緩存中已經(jīng)存在與現(xiàn)有項(xiàng)同名的項(xiàng),則該方法不會替換該項(xiàng),并且不會引發(fā)異常。

  1. string CachedItem = (string)Cache.Add("CacheItem",  
  2.     "Cached Item"null,  
  3.     System.Web.Caching.Cache.NoAbsoluteExpiration,  
  4.     System.Web.Caching.Cache.NoSlidingExpiration,   
  5.     System.Web.Caching.CacheItemPriority.Default,  
  6.     null); 

以上就介紹了ASP.NET緩存數(shù)據(jù)添加的六種方法。本文來自菩提屋:《緩存應(yīng)用程序數(shù)據(jù)(一)》

【編輯推薦】

  1. ASP.NET緩存機(jī)制基礎(chǔ)概念
  2. 再談ASP.NET緩存機(jī)制:開發(fā)效率與優(yōu)化的平衡
  3. .NET分布式緩存之Memcached執(zhí)行速度檢測
  4. 如何避免ASP.NET緩存占用系統(tǒng)資源
  5. .NET緩存機(jī)制探討與比對
責(zé)任編輯:yangsai 來源: 菩提屋
相關(guān)推薦

2009-07-28 12:52:50

ASP.NET coo

2009-08-10 18:31:42

什么是ASP.NET

2009-07-28 17:30:55

ASP.NET 2.0

2009-07-20 17:48:44

ASP.NET MVC

2009-08-07 14:55:15

ASP.NET復(fù)合控件

2009-08-03 18:47:12

ASP.NET數(shù)據(jù)緩存

2009-08-03 18:35:51

ASP.NET數(shù)據(jù)緩存

2009-07-31 10:23:44

緩存頁面ASP.NET緩存

2009-07-27 09:28:55

TableAdapte

2009-07-31 09:57:47

ASP.NET數(shù)據(jù)庫緩

2009-08-17 17:36:57

ASP.NET緩存數(shù)據(jù)

2009-07-29 10:35:51

ASP.NET緩存

2009-07-29 14:35:34

頁面輸出緩存ASP.NET

2009-07-31 10:33:54

ASP.NET頁面輸出

2009-08-04 15:22:33

ASP.NET緩存機(jī)制

2009-07-28 10:04:54

添加WebPartASP.NET

2022-08-20 19:04:27

數(shù)據(jù)緩存技術(shù)

2009-07-27 09:35:57

業(yè)務(wù)邏輯層

2009-08-17 16:59:36

ASP.NET緩存機(jī)制

2009-05-11 13:48:00

ASP.NET 2.0緩存效率
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號