NET下三种缓存机制(Winform里面的缓存使用 )

原文(http://www.cnblogs.com/wuhuacong/p/3526335.html)很是感谢伍华聪做者的分享!html

缓存在不少状况下须要用到,合理利用缓存能够一方面能够提升程序的响应速度,同时能够减小对特定资源访问的压力。本文主要针对本身在Winform方面的缓存使用作一个引导性的介绍,但愿你们可以从中了解一些缓存的使用场景和使用方法。缓存是一个中大型系统所必须考虑的问题。为了不每次请求都去访问后台的资源(例如数据库),咱们通常会考虑将一些更新不是很频繁的,能够重用的数据,经过必定的方式临时地保存起来,后续的请求根据状况能够直接访问这些保存起来的数据。这种机制就是所谓的缓存机制。数据库

.NET 4.0的缓存功能主要由三部分组成:System.Runtime.Caching,System.Web.Caching.Cache和Output Cache。缓存

System.Runtime.Caching这是在.NET 4.0中新增的缓存框架,主要是使用MemoryCache对象,该对象存在于程序集System.Runtime.Caching.dll。框架

System.Web.Caching.Cache这个则是在.NET2.0开始就一直存在的缓存对象,通常主要用在Web中,固然也能够用于Winform里面,不过要引用System.Web.dll。ide

Output Cache则是Asp.NET里面使用的,在ASP.NET 4.0以前的版本都是直接使用System.Web.Caching.Cache来缓存HTML片断。在ASP.NET 4.0中对它进行了从新设计,提供了一个OutputCacheProvider供开发人员进行扩展,可是它默认状况下,仍然使用System.Web.Caching.Cache来作作缓存。函数

一、自定义Hastable的缓存处理。

除了上面三种的缓存机制,通常咱们还能够在静态对象里面经过HashTable或者Dictionary的方式进行自定义的缓存存储和使用。优化

例如我在我本身所开发的程序里面,都使用了工厂类来建立业务对象,因为建立业务对象以及数据访问层对象,是一个在界面或者中间层反复调用的操做,所以须要把常常调用的对象把它存储起来,下载调用的时候,直接从内存中取出来便可。以下面的BLLFactory类,就是一个基于泛型对象的业务类的建立操做,使用了基于Hashtable的静态对象进行缓存处理。spa

 1 /// <summary>
 2 /// 对业务类进行构造的工厂类
 3 /// </summary>
 4 /// <typeparam name="T">业务对象类型</typeparam>
 5 public class BLLFactory<T> where T : class
 6 {
 7 private static Hashtable objCache = new Hashtable();
 8 private static object syncRoot = new Object();
 9 
10 /// <summary>
11 /// 建立或者从缓存中获取对应业务类的实例
12 /// </summary>
13 public static T Instance
14 {
15 get
16 {
17 string CacheKey = typeof(T).FullName;
18 T bll = (T)objCache[CacheKey];  //从缓存读取 
19 if (bll == null)
20 {
21 lock (syncRoot)
22 {
23 if (bll == null)
24 {
25 bll = Reflect<T>.Create(typeof(T).FullName, typeof(T).Assembly.GetName().Name); //反射建立,并缓存
26 objCache.Add(typeof(T).FullName, bll);
27 }
28 }
29 }
30 return bll;
31 }
32 }
33 }

二、使用.NET4.0的MemoryCache对象实现缓存设计

MemoryCache的使用网上介绍的很少,不过这个是.NET4.0新引入的缓存对象,估计主要是替换原来企业库的缓存模块,使得.NET的缓存能够无处不在,而不用基于特定的Windows版本上使用。代理

首先咱们使用来建立一个基于MemoryCache的辅助类MemoryCacheHelper,方便调用进行缓存处理。

 1 /// <summary>
 2     /// 基于MemoryCache的缓存辅助类
 3     /// </summary>
 4     public static class MemoryCacheHelper
 5     {
 6         private static readonly Object _locker = new object();
 7      
 8         public static T GetCacheItem<T>(String key, Func<T> cachePopulate, TimeSpan? slidingExpiration = null, DateTime? absoluteExpiration = null)
 9         {
10             if(String.IsNullOrWhiteSpace(key)) throw new ArgumentException("Invalid cache key");
11             if(cachePopulate == null) throw new ArgumentNullException("cachePopulate");
12             if(slidingExpiration == null && absoluteExpiration == null) throw new ArgumentException("Either a sliding expiration or absolute must be provided");
13      
14             if(MemoryCache.Default[key] == null)
15             {
16                 lock(_locker)
17                 {
18                     if(MemoryCache.Default[key] == null)
19                     {
20                         var item = new CacheItem(key, cachePopulate());
21                         var policy = CreatePolicy(slidingExpiration, absoluteExpiration);
22      
23                         MemoryCache.Default.Add(item, policy);
24                     }
25                 }
26             }
27      
28             return (T)MemoryCache.Default[key];
29         }
30      
31         private static CacheItemPolicy CreatePolicy(TimeSpan? slidingExpiration, DateTime? absoluteExpiration)
32         {
33             var policy = new CacheItemPolicy();
34      
35             if(absoluteExpiration.HasValue)
36             {
37                 policy.AbsoluteExpiration = absoluteExpiration.Value;
38             }
39             else if(slidingExpiration.HasValue)
40             {
41                 policy.SlidingExpiration = slidingExpiration.Value;
42             }
43      
44             policy.Priority = CacheItemPriority.Default;
45      
46             return policy;
47         }
48     }

这个辅助类只有一个public方法,就是GetCacheItem,使用的时候,须要指定key和获取数据的处理代理,还有缓存的过时时间,是基于TimeSpan的仍是基于绝对时间的,选择其一。

上面的辅助类,咱们在什么状况下会使用到呢?

假如在一个工做流模块中用到了人员ID,而人员ID须要进行人员名称的转义,人员信息咱们通常知道放在权限系统模块里面,那么若是在工做流里面须要频繁对人员ID进行转义,那么就须要方法调用权限系统的接口模块,这样处理就可使用缓存模块进行优化处理的了。

 1 void gridView1_CustomColumnDisplayText(object sender, DevExpress.XtraGrid.Views.Base.CustomColumnDisplayTextEventArgs e)
 2         {
 3             if (e.Column.FieldName.Equals("ProcUser") || e.Column.FieldName.Equals("ProcUid") || e.Column.FieldName.Equals("UserId"))
 4             {
 5                 if (e.Value != null)
 6                 {
 7                     e.DisplayText = SecurityHelper.GetUserFullName(e.Value.ToString());
 8                 }
 9             }
10         }

其中的SecurityHelper.GetUserFullName是我对调用进行基于缓存的二次封装,具体逻辑以下所示。

 1 /// <summary>
 2         /// 根据用户的ID,获取用户的全名,并放到缓存里面
 3         /// </summary>
 4         /// <param name="userId">用户的ID</param>
 5         /// <returns></returns>
 6         public static string GetUserFullName(string userId)
 7         {            
 8             string key = "Security_UserFullName" + userId;
 9             string fullName = MemoryCacheHelper.GetCacheItem<string>(key,
10                 delegate() { return BLLFactory<User>.Instance.GetFullNameByID(userId.ToInt32()); },
11                 new TimeSpan(0, 30, 0));//30分钟过时
12             return fullName;
13         }

MemoryCacheHelper的方法GetCacheItem里面的Func<T>我使用了一个匿名函数用来获取缓存的值。

1 delegate() { return BLLFactory<User>.Instance.GetFullNameByID(userId.ToInt32()); }

而调用BLLFactory<User>.Instance.GetFullNameByID则是从数据库里面获取对应的数据了。

这样在第一次或者缓存过时的时候,自动调用业务对象类的方法来获取数据了。

最后,在界面上调用GetUserFullName的方法便可实现基于缓存方式的调用,程序第一次使用的,碰到指定的键没有数据,就去数据库里面获取,之后碰到该键,则直接获取缓存的数据了。

下面图形是程序具体的实现效果。

固然,以上两种方式都还能够经过AOP的注入方式实现代码的简化操做,不过因为对AOP的引入,会涉及到更多的知识点,并且熟悉程序还不够,因此依然采用较为经常使用的方式来处理缓存的数据。

相关文章
相关标签/搜索