日韩无码专区无码一级三级片|91人人爱网站中日韩无码电影|厨房大战丰满熟妇|AV高清无码在线免费观看|另类AV日韩少妇熟女|中文日本大黄一级黄色片|色情在线视频免费|亚洲成人特黄a片|黄片wwwav色图欧美|欧亚乱色一区二区三区

RELATEED CONSULTING
相關咨詢
選擇下列產(chǎn)品馬上在線溝通
服務時間:8:30-17:00
你可能遇到了下面的問題
關閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
ASP.NET緩存數(shù)據(jù)添加方法一覽

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

創(chuàng)新互聯(lián)建站擁有10余年成都網(wǎng)站建設工作經(jīng)驗,為各大企業(yè)提供網(wǎng)站設計制作、成都網(wǎng)站建設服務,對于網(wǎng)頁設計、PC網(wǎng)站建設(電腦版網(wǎng)站建設)、成都app軟件開發(fā)公司、wap網(wǎng)站建設(手機版網(wǎng)站建設)、程序開發(fā)、網(wǎng)站優(yōu)化(SEO優(yōu)化)、微網(wǎng)站、申請域名等,憑借多年來在互聯(lián)網(wǎng)的打拼,我們在互聯(lián)網(wǎng)網(wǎng)站建設行業(yè)積累了很多網(wǎng)站制作、網(wǎng)站設計、網(wǎng)絡營銷經(jīng)驗,集策劃、開發(fā)、設計、營銷、管理等網(wǎng)站化運作于一體,具備承接各種規(guī)模類型的網(wǎng)站建設項目的能力。

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

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

1、通過鍵值對添加

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

2、通過Insert 方法添加

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

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

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

數(shù)據(jù)項依賴一個字符串數(shù)組對象的情況:

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

數(shù)據(jù)項依賴一個XML文件的情況:

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

數(shù)據(jù)項依賴多個依賴項的情況:

 
 
 
  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、設置過期策略并添加

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

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

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

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

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

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

 
 
 
  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)有項同名的項,則該方法不會替換該項,并且不會引發(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ù)添加的六種方法。本文來自菩提屋:《緩存應用程序數(shù)據(jù)(一)》

【編輯推薦】

  1. ASP.NET緩存機制基礎概念
  2. 再談ASP.NET緩存機制:開發(fā)效率與優(yōu)化的平衡
  3. .NET分布式緩存之Memcached執(zhí)行速度檢測
  4. 如何避免ASP.NET緩存占用系統(tǒng)資源
  5. .NET緩存機制探討與比對

分享名稱:ASP.NET緩存數(shù)據(jù)添加方法一覽
文章來源:http://m.5511xx.com/article/dhopcsj.html