ASP.NET缓存数据技巧三则

开发 后端
ASP.NET使用缓存机制,将需要大量服务器资源来创建的对象存储在内存中。本文介绍在编写ASP.NET应用时可能会用到的三个ASP.NET缓存数据技巧。

ASP.NET缓存数据技巧:访问缓存的值

由于缓存中所存储的信息为易失信息,即该信息可能由 ASP.NET 移除,因此建议先确定该项是否在缓存中。如果不在,则应将它重新添加到缓存中,然后检索该项。

  1. string cachedString;  
  2. if (Cache["CacheItem"] != null)  
  3. {  
  4.     cachedString = (string)Cache["CacheItem"];  
  5. }  
  6. else 
  7. {  
  8.  
  9.      //缓存不存在时  
  10.     Cache.Insert("CacheItem""Hello, World.")  
  11.     cachedString = (string)Cache["CacheItem"];  
  12. }  

ASP.NET缓存数据技巧:删除缓存项

由于以下任一原因,缓存中的数据可能会自动移除:缓存已满、该项已过期、依赖项发生更改。注意:如果调用 Insert 方法,并向缓存中添加与现有项同名的项,则将从缓存中删除该旧项。显示删除缓存的值:

  1. Cache.Remove("MyCacheKey"); 

ASP.NET缓存数据技巧:删除缓存项时通知应用程序

从缓存中移除项时通知应用程序,可能非常有用。例如,可能具有一个缓存的报告,创建该报告需花费大量的时间进行处理。当该报告从缓存中移除时,希望重新生成该报告,并立即将其置于缓存中,以便下次请求该报告时,用户不必等待对此报告进行处理。

ASP.NET 提供了CacheItemRemovedCallback 委托,在从缓存中移除项时能够发出通知。还提供 CacheItemRemovedReason 枚举,用于指定移除缓存项的原因。举例:假设有一个 ReportManager 对象,该对象具有两种方法,即 GetReport 和 CacheReport。GetReport 报告方法检查缓存以查看报告是否已缓存;如果没有,该方法将重新生成报告并将其缓存。CacheReport 方法具有与 CacheItemRemovedCallback 委托相同的函数签名;从缓存中移除报告时,ASP.NET 会调用 CacheReport 方法,然后将报告重新添加到缓存中。

1)创建一个 ASP.NET 网页,该网页将调用类中用于将项添加到缓存中的方法。

  1. protected void Page_Load(object sender, EventArgs e)  
  2. {  
  3.     this.Label1.Text = ReportManager.GetReport();  

2)创建用于在从缓存中删除项时处理通知的完整类ReportManager。

  1. using System;  
  2. using System.Web;  
  3. using System.Web.Caching;  
  4. public static class ReportManager  
  5. {  
  6.     private static bool _reportRemovedFromCache = false;  
  7.       
  8.     static ReportManager() { }  
  9.       
  10.     //从缓存中获取项  
  11.     public static String GetReport()  
  12.     {  
  13.         lock (typeof(ReportManager))  
  14.         {  
  15.             if (HttpContext.Current.Cache["MyReport"] != null)  
  16.             {    //存在MyReport缓存项,返回缓存值  
  17.                 return (string)HttpRuntime.Cache["MyReport"];  
  18.             }  
  19.             else 
  20.             {   //MyReport缓存项不存在,则创建MyReport缓存项  
  21.                 CacheReport();  
  22.                 return (string)HttpRuntime.Cache["MyReport"];  
  23.             }  
  24.         }  
  25.     }  
  26.  
  27.     //将项以 MyReport 的名称添加到缓存中,并将该项设置为在添加到缓存中后一分钟过期。  
  28.     //并且该方法注册 ReportRemoveCallback 方法,以便在从缓存中删除项时进行调用。  
  29.     public static void CacheReport()  
  30.     {  
  31.         lock (typeof(ReportManager))  
  32.         {  
  33.             HttpContext.Current.Cache.Add("MyReport",  
  34.                 CreateReport(), null, DateTime.MaxValue,  
  35.                 new TimeSpan(0, 1, 0),   
  36.                 System.Web.Caching.CacheItemPriority.Default,  
  37.                 ReportRemovedCallback);  
  38.         }  
  39.     }  
  40.  
  41.     //创建报告,该报告时MyReport缓存项的值  
  42.     private static string CreateReport()  
  43.     {  
  44.         System.Text.StringBuilder myReport =   
  45.             new System.Text.StringBuilder();  
  46.         myReport.Append("Sales Report< br />");  
  47.         myReport.Append("2005 Q2 Figures< br />");  
  48.         myReport.Append("Sales NE Region - $2 million< br />");  
  49.         myReport.Append("Sales NW Region - $4.5 million< br />");  
  50.         myReport.Append("Report Generated: " + DateTime.Now.ToString()   
  51.             + "< br />");  
  52.         myReport.Append("Report Removed From Cache: " +   
  53.             _reportRemovedFromCache.ToString());  
  54.         return myReport.ToString();  
  55.     }  
  56.  
  57.     //当从缓存中删除项时调用该方法。  
  58.     public static void ReportRemovedCallback(String key, object value,   
  59.         CacheItemRemovedReason removedReason)  
  60.     {  
  61.         _reportRemovedFromCache = true;  
  62.         CacheReport();  
  63.     }  
  64. }  

不应在 ASP.NET 页中实现回调处理程序,因为在从缓存中删除项之前该页可能已被释放,因此用于处理回调的方法将不可用,应该在非ASP.NET的程序集中实现回调处理程序。为了确保从缓存中删除项时处理回调的方法仍然存在,请使用该方法的静态类。但是,静态类的缺点是需要保证所有静态方法都是线程安全的,所以使用lock关键字。

本文来自菩提屋:《缓存应用程序数据(二)》

【编辑推荐】

  1. ASP.NET缓存数据添加方法一览
  2. ASP.NET缓存机制基础概念
  3. 再谈ASP.NET缓存机制:开发效率与优化的平衡
  4. .NET分布式缓存之Memcached执行速度检测
  5. 如何避免ASP.NET缓存占用系统资源
责任编辑:yangsai 来源: 菩提屋
相关推荐

2009-07-30 08:49:58

ASP.NET中usi

2009-08-03 18:47:12

ASP.NET数据缓存

2009-08-03 18:35:51

ASP.NET数据缓存

2009-07-24 11:24:33

ASP.NET中文乱码

2009-07-31 10:23:44

缓存页面ASP.NET缓存

2009-07-24 12:14:17

asp.net技巧

2009-07-31 09:57:47

ASP.NET数据库缓

2009-07-29 10:35:51

ASP.NET缓存

2009-07-31 10:33:54

ASP.NET页面输出

2009-07-29 14:35:34

页面输出缓存ASP.NET

2009-08-04 15:22:33

ASP.NET缓存机制

2009-01-03 09:34:30

ASP.NET.NET性能优化

2009-07-29 10:52:09

数据采集程序ASP.NET技巧

2009-05-11 13:48:00

ASP.NET 2.0缓存效率

2009-08-17 16:59:36

ASP.NET缓存机制

2009-07-29 15:34:13

2009-07-29 13:32:06

ASP.NET控件使用

2009-07-29 13:42:25

ASP.NET注释

2009-07-23 13:47:46

ASP.NET数据缓存

2009-08-17 17:19:00

ASP.NET缓存数据
点赞
收藏

51CTO技术栈公众号