ASP.NET cache緩存的用法


轉載自:http://blog.csdn.net/mss359681091/article/details/51076712

 

 
本文導讀:在.NET運用中經常用到緩存(Cache)對象。有HttpContext.Current.Cache以及HttpRuntime.Cache,HttpRuntime.Cache是應用程序級別的,而HttpContext.Current.Cache是針對當前WEB上下文定義的。HttpRuntime下的除了WEB中可以使用外,非WEB程序也可以使用。
 

1、HttpRuntime.Cache 相當於就是一個緩存具體實現類,這個類雖然被放在了 System.Web 命名空間下了。但是非 Web 應用也是可以拿來用的。


2、HttpContext.Cache 是對上述緩存類的封裝,由於封裝到了 HttpContext ,局限於只能在知道 HttpContext 下使用,即只能用於 Web 應用。

綜上所屬,在可以的條件,盡量用 HttpRuntime.Cache ,而不是用 HttpContext.Cache 。

 

一、有以下幾條緩存數據的規則


第一,數據可能會被頻繁的被使用,這種數據可以緩存。


第二,數據的訪問頻率非常高,或者一個數據的訪問頻率不高,但是它的生存周期很長,這樣的數據最好也緩存起來。


第三是一個常常被忽略的問題,有時候我們緩存了太多數據,通常在一台X86的機子上,如果你要緩存的數據超過800M的話,就會出現內存溢出的錯誤。所以說緩存是有限的。換名話說,你應該估計緩存集的大小,把緩存集的大小限制在10以內,否則它可能會出問題。在Asp.net中,如果緩存過大的話也會報內存溢出錯誤,特別是如果緩存大的DataSet對象的時候。

你應該認真分析你的程序。根據實際情況來看哪里該用,哪里不該用。如:cache用得過多也會增大服務器的壓力。整頁輸出緩存,又會影響數據的更新。 如果真的需要緩存很大量的數據,可以考慮靜態技術。

 

二、下面介紹HttpRuntime.Cache常用方法

 

C# 代碼 
[csharp]  view plain  copy
 
 
 
  1. <strong>using System;  
  2. using System.Web;  
  3. using System.Collections;  
  4.   
  5. public class CookiesHelper  
  6.     {  
  7.     /**//// <summary>  
  8.     /// 獲取數據緩存  
  9.     /// </summary>  
  10.     /// <param name="CacheKey">鍵</param>  
  11.     public static object GetCache(string CacheKey)  
  12.     {  
  13.         System.Web.Caching.Cache objCache = HttpRuntime.Cache;  
  14.         return objCache[CacheKey];  
  15.     }  
  16.   
  17.     /**//// <summary>  
  18.     /// 設置數據緩存  
  19.     /// </summary>  
  20.     public static void SetCache(string CacheKey, object objObject)  
  21.     {  
  22.         System.Web.Caching.Cache objCache = HttpRuntime.Cache;  
  23.         objCache.Insert(CacheKey, objObject);  
  24.     }  
  25.   
  26.     /**//// <summary>  
  27.     /// 設置數據緩存  
  28.     /// </summary>  
  29.     public static void SetCache(string CacheKey, object objObject, TimeSpan Timeout)  
  30.     {  
  31.         System.Web.Caching.Cache objCache = HttpRuntime.Cache;  
  32.         objCache.Insert(CacheKey, objObject, null, DateTime.MaxValue, Timeout, System.Web.Caching.CacheItemPriority.NotRemovable, null);  
  33.     }  
  34.   
  35.     /**//// <summary>  
  36.     /// 設置數據緩存  
  37.     /// </summary>  
  38.     public static void SetCache(string CacheKey, object objObject, DateTime absoluteExpiration, TimeSpan slidingExpiration)  
  39.     {  
  40.         System.Web.Caching.Cache objCache = HttpRuntime.Cache;  
  41.         objCache.Insert(CacheKey, objObject, null, absoluteExpiration, slidingExpiration);  
  42.     }  
  43.   
  44.     /**//// <summary>  
  45.     /// 移除指定數據緩存  
  46.     /// </summary>  
  47.     public static void RemoveAllCache(string CacheKey)  
  48.     {  
  49.         System.Web.Caching.Cache _cache = HttpRuntime.Cache;  
  50.         _cache.Remove(CacheKey);  
  51.     }  
  52.   
  53.     /**//// <summary>  
  54.     /// 移除全部緩存  
  55.     /// </summary>  
  56.     public static void RemoveAllCache()  
  57.     {  
  58.         System.Web.Caching.Cache _cache = HttpRuntime.Cache;  
  59.         IDictionaryEnumerator CacheEnum = _cache.GetEnumerator();  
  60.         while (CacheEnum.MoveNext())  
  61.         ...{  
  62.             _cache.Remove(CacheEnum.Key.ToString());  
  63.         }  
  64.     }  
  65. }  
  66. </strong>  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM