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>  
原文地址:https://www.cnblogs.com/Jeely/p/10785317.html