[转]ASP.NET cache缓存的用法

本文转自:https://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用得过多也会增大服务器的压力。整页输出缓存,又会影响数据的更新。 如果真的需要缓存很大量的数据,可以考虑静态技术。

扫描二维码关注公众号,回复: 89965 查看本文章

二、下面介绍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>  
三、实战,个人项目
1、第一步,在global.asax 加入
[html]  view plain  copy
 
  1. <strong>    void Application_Start(object sender, EventArgs e)  
  2.     {  
  3.         // 在应用程序启动时运行的代码  
  4.         try  
  5.         {  
  6.             //定义连接字符串  
  7.             string conStr = System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;  
  8.             System.Data.SqlClient.SqlDependency.Start(conStr);//启动监听服务,ps:只需启动一次  
  9.             System.Web.Caching.SqlCacheDependencyAdmin.EnableNotifications(conStr);//设置通知的数据库连接,ps:只需设置一次  
  10.             string[] str = { "TMS_OptionScoreDetails", "TMS_TeachAssess", "TMS_TeachAssessDetail", "TMS_TeachAssessPublish", "TMS_TeachAssessRecord", "TMS_TeachAssessRecordDetails", "TMS_TeachOption" };  
  11.             System.Web.Caching.SqlCacheDependencyAdmin.EnableTableForNotifications(conStr, str);//设置通知的数据库连接和表,ps:只需设置一次  
  12.   
  13.   
  14.         }  
  15.         catch  
  16.         {  
  17.   
  18.         }  
  19.     }  
  20. </strong>  
2、第二步,在引用缓存类,并在项目用应用。
[csharp]  view plain  copy
 
  1. //缓存  
  2.   DataSet myDataSet = new DataSet();  
  3.   
  4.   string CacheKey = "SearchDate" + TeachAssessID;  
  5.   object objModel = TMSCommonMethod.GetCache(CacheKey);//从缓存中获取  
  6.   if (objModel == null)//缓存里没有  
  7.   {  
  8.       TMSTeachAssessDetailManager myTMSTeachAssessDetailManager = new TMSTeachAssessDetailManager();  
  9.       TMSTeachAssessDetailQuery myTMSTeachAssessDetailQuery = new TMSTeachAssessDetailQuery();  
  10.       //myTMSTeachAssessDetailQuery.TeachAssessPublishID = TeachAssessPublishID;  
  11.       myTMSTeachAssessDetailQuery.TeachAssessID = TeachAssessID;  
  12.       myDataSet = myTMSTeachAssessDetailManager.SearchDate(null, myTMSTeachAssessDetailQuery);  
  13.   
  14.       objModel = myDataSet;//把数据存入缓存  
  15.       if (objModel != null)  
  16.       {  
  17.             
  18.           System.Web.Caching.SqlCacheDependency dep = new System.Web.Caching.SqlCacheDependency(ConfigurationManager.AppSettings["CacheDataBaseName"].ToString(), TMSTeachAssessDetail.TABLENAME);  
  19.           TMSCommonMethod.SetCache(CacheKey, objModel, dep);//写入缓存  
  20.       }  
  21.   }  
  22.   else  
  23.   {  
  24.       myDataSet = (DataSet)objModel;  
  25.   }  


 

猜你喜欢

转载自www.cnblogs.com/freeliver54/p/8971165.html