详解.NET 4.0可扩展缓存框架

开发 后端
.NET 4.0可扩展缓存框架也可以说是一套系统,本文将带领大家走进.NET的这套框架,并配以相关实例。

.NET Framework中,叫做System.Runtime.Caching,这不仅是个缓存库,还是个框架,可以在上面开发自己的库。ObjectCache定义了所有缓存都要实现的通用操作。与之搭配的是个内存缓存实现,叫做MemoryCache。这个缓存系统的结构如下:

image

上图大家可以看出来对应那些产品了吗?

下面我给大家介绍一个实现这样一个架构的代码示例,代码的核心就是ObjectCache:

定义一个抽象的Provider接口:

  1. public interface ICacheBuilder    
  2. {     
  3. ObjectCache GetInstance();  
  4. string DefaultRegionName { get; }  

In-memory提供者的实现使用MemoryCache:

  1. public class MemoryCacheBuilder : ICacheBuilder  
  2. {  
  3.  public MemoryCacheBuilder() { }  
  4.  public ObjectCache GetInstance()  
  5.  {  
  6.  return MemoryCache.Default;  
  7.  }  
  8.  public string DefaultRegionName  
  9.   {  
  10.    get { return null; }  
  11.   }  
  12.  }  

分布式缓存提供者Memcached:

 

  1. public class MemcachedCache : ObjectCache, ICacheBuilder  
  2.  {  
  3. private long _lDefaultExpireTime = 3600; // default Expire Time  
  4.    private MemcachedClient _client = null;  
  5.     #region ICache Members  
  6.      
  7.         public MemcachedCache()  
  8.         {  
  9.             this._client = MemcachedClientService.Instance.Client;  
  10.        }  
  11.       
  12. public override void Set(string key, object value, System.DateTimeOffset absoluteExpiration, string regionName = null)  
  13.        {  
  14.            Enforce.NotNull(key, "key");  
  15.            CacheItem item = new CacheItem(key, value, regionName);  
  16.     CacheItemPolicy policy = new CacheItemPolicy();  
  17.            policy.AbsoluteExpiration = absoluteExpiration;  
  18.            Set(item, policy);  
  19.        }    
  20.        public override void Set(CacheItem item, CacheItemPolicy policy)  
  21.       {  
  22.            if (item == null || item.Value == null)  
  23.                return;  
  24.            item.Key = item.Key.ToLower();  
  25.             if (policy != null && policy.ChangeMonitors != null && policy.ChangeMonitors.Count > 0)  
  26.                throw new NotSupportedException("Change monitors are not supported");  
  27.      
  28.            // max timeout in scaleout = 65535  
  29.            TimeSpan expire = (policy.AbsoluteExpiration.Equals(null)) ?  
  30.         policy.SlidingExpiration :  
  31.                              (policy.AbsoluteExpiration - DateTimeOffset.Now);  
  32.         double timeout = expire.TotalMinutes;  
  33.            if (timeout > 65535)  
  34.              timeout = 65535;  
  35.            else if (timeout > 0 && timeout < 1)  
  36.               timeout = 1;  
  37.           this._client.Store(Enyim.Caching.Memcached.StoreMode.Set, item.Key.ToString(), item.Value);  
  38.      }  
  39.       public override object this[string key]  
  40.        {  
  41.           get 
  42.           {  
  43.               return Get(key);  
  44.           }  
  45.           set 
  46.            {  
  47.                Set(key, value, null);  
  48.           }  
  49.        }  
  50. public override object AddOrGetExisting(string key, object value, CacheItemPolicy policy, string regionName = null)  
  51.        {  
  52.            CacheItem item = GetCacheItem(key, regionName);  
  53.            if (item == null)  
  54.           {  
  55.                Set(new CacheItem(key, value, regionName), policy);  
  56.                return value;  
  57.           }  
  58.          return item.Value;  
  59.        }  
  60.      public override CacheItem AddOrGetExisting(CacheItem value, CacheItemPolicy policy)  
  61.      {  
  62.            CacheItem item = GetCacheItem(value.Key, value.RegionName);  
  63.           if (item == null)  
  64.           {  
  65.               Set(value, policy);  
  66.               return value;  
  67.           }  
  68.          return item;  
  69.       }  
  70.  public override object AddOrGetExisting(string key, object value, System.DateTimeOffset absoluteExpiration, string regionName = null)  
  71.       {  
  72.           CacheItem item = new CacheItem(key, value, regionName);  
  73.            CacheItemPolicy policy = new CacheItemPolicy();  
  74.            policy.AbsoluteExpiration = absoluteExpiration;  
  75.      return AddOrGetExisting(item, policy);  
  76.        }  
  77.       public override bool Contains(string key, string regionName = null)  
  78.        {  
  79.           return false;  
  80.       }  
  81. public override CacheEntryChangeMonitor CreateCacheEntryChangeMonitor(System.Collections.Generic.IEnumerable<string> keys, string regionName = null)  
  82.        {  
  83.          throw new System.NotImplementedException();  
  84.     }  
  85.    public override DefaultCacheCapabilities DefaultCacheCapabilities  
  86.     {  
  87.       get 
  88.          {  
  89.            return 
  90.                DefaultCacheCapabilities.OutOfProcessProvider |  
  91.                DefaultCacheCapabilities.AbsoluteExpirations |  
  92.                 DefaultCacheCapabilities.SlidingExpirations |  
  93. DefaultCacheCapabilities.CacheRegions;  
  94.         }  
  95.    }  
  96.   public override object Get(string key, string regionName = null)  
  97.      {  
  98.         key = key.ToLower();  
  99.        return this._client.Get(key);  
  100.       }  
  101.     public override CacheItem GetCacheItem(string key, string regionName = null)  
  102.       {  
  103.          object value = Get(key, regionName);  
  104.          if (value != null)  
  105.              return new CacheItem(key, value, regionName);  
  106.         return null;  
  107.       }  
  108.     public override long GetCount(string regionName = null)  
  109.      {  
  110.         return -1;  
  111.     }  
  112.   protected override System.Collections.Generic.IEnumerator<System.Collections.Generic.KeyValuePair<stringobject>> GetEnumerator()  
  113.     {  
  114.           throw new System.NotImplementedException();  
  115.       }  
  116. public override System.Collections.Generic.IDictionary<stringobject> GetValues(System.Collections.Generic.IEnumerable<string> keys, string regionName = null)  
  117.      {  
  118.           throw new System.NotImplementedException();  
  119.       }  
  120.      
  121.      public override string Name  
  122.       {  
  123.           get { return "MemcachedProvider"; }  
  124.       }  
  125.      
  126.       public override object Remove(string key, string regionName = null)  
  127.     {  
  128.         key = key.ToLower();  
  129.           return this._client.Remove(key);  
  130.     }  
  131.      public override void Set(string key, object value, CacheItemPolicy policy, string regionName = null)  
  132.     {  
  133.         Set(new CacheItem(key, value, regionName), policy);  
  134.      }  
  135.     #endregion  
  136.     #region ICacheBuilder Members  
  137.      public ObjectCache GetInstance()  
  138.      {  
  139.          return this;  
  140.      }  
  141.      public string DefaultRegionName  
  142.      {  
  143.        get { throw new NotImplementedException(); }  
  144.       }  
  145.      #endregion  

分布式缓存提供者Windows Server AppFabric Caching:

  1. public class AppFabricCacheProvider : ObjectCache, ICacheBuilder    
  2. {     
  3.   public static DataCache factory = null;    
  4.  public static object syncObj = new object();    
  5. public override object AddOrGetExisting(string key, object value, CacheItemPolicy policy, string regionName = null)     
  6.  {     
  7.         CacheItem item = GetCacheItem(key, regionName);   
  8.          if (item == null)   
  9.          {    
  10.              Set(new CacheItem(key, value, regionName), policy);    
  11.          return value;    
  12.          }    
  13.          return item.Value;    
  14.      }    
  15. public override CacheItem AddOrGetExisting(CacheItem value, CacheItemPolicy policy)   
  16.     {    
  17.        CacheItem item = GetCacheItem(value.Key, value.RegionName);   
  18.        if (item == null)   
  19.          {   
  20.             Set(value, policy);   
  21.             return value;    
  22.        }   
  23.       return item;   
  24.    }    
  25. public override object AddOrGetExisting(string key, object value, System.DateTimeOffset absoluteExpiration, string regionName = null)   
  26.     {    
  27.         CacheItem item = new CacheItem(key, value, regionName);    
  28.        CacheItemPolicy policy = new CacheItemPolicy();    
  29.        policy.AbsoluteExpiration = absoluteExpiration;    
  30.         return AddOrGetExisting(item, policy);    
  31.   }    
  32.     public override bool Contains(string key, string regionName = null)   
  33.   {   
  34.      return Get(key, regionName) != null;    
  35.   }    
  36.  public override CacheEntryChangeMonitor CreateCacheEntryChangeMonitor(System.Collections.Generic.IEnumerable<string> keys, string regionName = null)    
  37.    {    
  38.          throw new NotImplementedException();    
  39.    }    
  40.     public override DefaultCacheCapabilities DefaultCacheCapabilities    
  41.   {   
  42.         get    
  43.          {    
  44.             return    
  45.              
  46. DefaultCacheCapabilities.OutOfProcessProvider |    
  47.                DefaultCacheCapabilities.AbsoluteExpirations |    
  48.                  DefaultCacheCapabilities.SlidingExpirations |    
  49.                DefaultCacheCapabilities.CacheRegions;    
  50.         }    
  51.      }    
  52.     public override object Get(string key, string regionName = null)    
  53.      {    
  54.          key = key.ToLower();   
  55.          CreateRegionIfNeeded();    
  56.         return (regionName == null) ?  
  57.            CacheFactory.Get(key) :   
  58.            CacheFactory.Get(key, regionName);    
  59.      }    
  60.      public override CacheItem GetCacheItem(string key, string regionName = null)   
  61.      {   
  62.          object value = Get(key, regionName);    
  63.          if (value != null)   
  64.            return new CacheItem(key, value, regionName);    
  65.          return null;   
  66.     }    
  67.      public override long GetCount(string regionName = null)    
  68.    {    
  69.         if (string.IsNullOrEmpty(regionName))    
  70.              throw new NotSupportedException();   
  71.          return CacheFactory.GetObjectsInRegion(regionName).LongCount();    
  72.  }    
  73.      protected override System.Collections.Generic.IEnumerator<System.Collections.Generic.KeyValuePair<stringobject>> GetEnumerator()    
  74.    {    
  75.        throw new NotSupportedException();    
  76.      }    
  77.      public override System.Collections.Generic.IDictionary<stringobject> GetValues(System.Collections.Generic.IEnumerable<string> keys, string regionName = null)   
  78.     {    
  79.         if (string.IsNullOrEmpty(regionName))    
  80.          throw new NotSupportedException();   
  81.         return CacheFactory.GetObjectsInRegion(regionName).ToDictionary(x => x.Key, x => x.Value);    
  82.    }   
  83.    public override string Name   
  84.      {   
  85.       get { return "AppFabric"; }   
  86.      }  
  87. public override object Remove(string key, string regionName = null)  
  88.     {   
  89.          key = key.ToLower();   
  90.         CreateRegionIfNeeded();   
  91.         return (regionName == null) ?   
  92.             CacheFactory.Remove(key) :   
  93.              CacheFactory.Remove(key, regionName);   
  94.    }   
  95.     public override void Set(string key, object value, CacheItemPolicy policy, string regionName = null)   
  96.    {  
  97.      Set(new CacheItem(key, value, regionName), policy);   
  98.    }  
  99.     public override void Set(CacheItem item, CacheItemPolicy policy)  
  100.     {   
  101.          if (item == null || item.Value == null)   
  102.              return;   
  103.        if (policy != null && policy.ChangeMonitors != null && policy.ChangeMonitors.Count > 0)   
  104.            throw new NotSupportedException("Change monitors are not supported");   
  105.          item.Key = item.Key.ToLower();   
  106.         CreateRegionIfNeeded();   
  107.       TimeSpan expire = (policy.AbsoluteExpiration.Equals(null)) ?   
  108.            policy.SlidingExpiration :   
  109.            (policy.AbsoluteExpiration - DateTimeOffset.Now);   
  110.          if (string.IsNullOrEmpty(item.RegionName))   
  111.              CacheFactory.Put(item.Key, item.Value, expire);   
  112.         else   
  113.              CacheFactory.Put(item.Key, item.Value, expire, item.RegionName);   
  114.      }   
  115.      private static DataCache CacheFactory   
  116.     {   
  117.          get   
  118.        {   
  119.             if (factory == null)   
  120.             {  
  121.                 lock (syncObj)  
  122.                  {   
  123.                      if (factory == null)   
  124.                   {   
  125.       DataCacheFactory cacheFactory = new DataCacheFactory();  
  126.                        factory = cacheFactory.GetDefaultCache();   
  127.                  }   
  128.             }   
  129.             }   
  130.              return factory;   
  131.          }   
  132.    }   
  133.    private void CreateRegionIfNeeded() 163:     {   
  134.          try   
  135.        {   
  136.        CacheFactory.CreateRegion(DefaultRegionName);   
  137.       }   
  138.       catch (DataCacheException ex)  
  139.         {   
  140.            if (!ex.ErrorCode.Equals(DataCacheErrorCode.RegionAlreadyExists))   
  141.                  throw ex;   
  142.         }  
  143.     }   
  144. public override void Set(string key, object value, System.DateTimeOffset absoluteExpiration, string regionName = null)   
  145.      {   
  146.          CacheItem item = new CacheItem(key, value, regionName);  
  147.          CacheItemPolicy policy = new CacheItemPolicy();   
  148.         policy.AbsoluteExpiration = absoluteExpiration;   
  149.        Set(item, policy);   
  150.     }   
  151.   public override object this[string key]   
  152.      {   
  153.         get   
  154.         {   
  155.             return Get(key, DefaultRegionName);   
  156.          }   
  157.          set   
  158.         {   
  159.              Set(key, value, null, DefaultRegionName);   
  160.          }   
  161.     }   
  162. public ObjectCache GetInstance()   
  163.     {   
  164.          return this;   
  165.      }   
  166. public string DefaultRegionName   
  167.   {  
  168.    get 
  169.  {   
  170.  string defaultRegion= FrameworkConfiguationManager.GetConfiguration().GetAppVariable("AppFabricCacheDefaultRegion");   
  171.         if (string.IsNullOrEmpty(defaultRegion))   
  172.      {   
  173.                 defaultRegion = "Default";   
  174.       }   
  175.          return defaultRegion;   
  176.     }   
  177.   }   
  178.  } 
输出缓存对于改善性能有很大好处,在ASP.NET 4.0中可以自定义输出缓存的策略,比如把输出保存在磁盘中,外部的memcached服务中等等。甚至还可以定义一些高级规则,比如为A页面使用A输出缓存策略来把数据保存于内存中,为B页面使用B输出缓存策略来把数据保存于磁盘中。image

代码例子可以参看文章http://www.buraksenyurt.com/post/AspNet-40-Custom-Cache-Provider.aspx,在web.config中配置

  1. <caching> 
  2.       <outputCache defaultProvider="AspNetInternalProvider"> 
  3.        <providers> 
  4. <add name="DiskBasedCacheProvider" type="CustomCaching.DiskCacheProvider,CustomCaching"/> 
  5.     </providers> 
  6.    </outputCache> 
  7.    </caching> 

在ASP.NET 4 的默认输出缓存策略中。所有的HTTP响应、所呈现的页面和控件缓存均使用上例所示的默认输出缓存提供程序(其中defaultProvider属性值为AspNetInternalProvider)。通过为defaultProvider指定不同的提供程序。就可以更改web应用程序的默认输出缓存提供程序。

另外,还可以针对每个用户控件和各个请求选择不同的输出缓存提供程序。要为不同的Web用户控件选择不同的输出缓存提供程序,最简便的方法是设置页面或控件指令中新增加的providerName属性,如下面的示例所示:

  1. <%@ OutputCache Duration="60" VaryByParam="None" 
  2.  providerName="DiskBasedCacheProvider" %> 

若要为某个HTTP请求指定不同的输出缓存提供程序,可以覆盖Global.asax文件中新增加的GetOutputCacheProviderName方法,以编程的方式指定要用于特定请求的提供程序。

原文地址:.NET4.0的可扩展缓存系统

链接:http://www.cnblogs.com/shanyou/archive/2010/07/01/1769547.html

【编辑推荐】

  1. 使用ASP.NET 4的自动启动特性
  2. 详解.NET 4.0并行计算支持历史
  3. 详读.NET 4.0环境配置
  4. 详解.NET 4.0中异常处理方面的新特性
  5. 三方面诠释.NET 4.0的新特性
责任编辑:彭凡 来源: 博客园
相关推荐

2009-03-16 09:16:13

行为扩展WCF.NET

2009-07-24 10:00:38

.NET 4.0内存映

2010-07-19 09:01:54

.NET 4.0缓存

2009-12-10 17:11:14

.NET Framew

2009-12-28 10:04:59

.NET 4.0数组

2009-12-04 09:14:05

.NET 4.0

2009-03-04 09:52:35

代码契约组件接口

2009-12-30 10:21:36

.NET 4.0

2009-12-18 09:38:27

.NET 4.0并行计

2009-08-11 17:29:53

.NET远程处理框架

2009-02-20 08:51:22

.NET框架CLR组件

2010-01-21 10:48:18

VB.NET扩展方法

2009-03-23 10:54:12

.NET契约式编程编程思想

2023-10-11 13:46:26

缓存Web应用程序

2009-10-19 15:14:48

aspx扩展

2010-01-05 09:26:13

.NET 4.0

2009-11-09 08:53:21

ASP.NET缓存

2009-11-04 13:38:34

.NET缓存

2009-09-02 18:28:55

.NET框架ESBasic

2010-01-12 09:10:31

Java EE 6Servlet 3.0Web分片
点赞
收藏

51CTO技术栈公众号