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

创新互联是专业的玄武网站建设公司,玄武接单;提供网站设计、网站建设,网页设计,网站设计,建网站,PHP网站建设等专业做网站服务;采用PHP框架,可快速的进行玄武网站开发网页制作和功能扩展;专业做搜索引擎喜爱的网站,专业的做网站团队,希望更多企业前来合作!
上图大家可以看出来对应那些产品了吗?
下面我给大家介绍一个实现这样一个架构的代码示例,代码的核心就是ObjectCache:
定义一个抽象的Provider接口:
- public interface ICacheBuilder
 - {
 - ObjectCache GetInstance();
 - string DefaultRegionName { get; }
 - }
 
In-memory提供者的实现使用MemoryCache:
- public class MemoryCacheBuilder : ICacheBuilder
 - {
 - public MemoryCacheBuilder() { }
 - public ObjectCache GetInstance()
 - {
 - return MemoryCache.Default;
 - }
 - public string DefaultRegionName
 - {
 - get { return null; }
 - }
 - }
 
分布式缓存提供者Memcached:
- public class MemcachedCache : ObjectCache, ICacheBuilder
 - {
 - private long _lDefaultExpireTime = 3600; // default Expire Time
 - private MemcachedClient _client = null;
 - #region ICache Members
 - public MemcachedCache()
 - {
 - this._client = MemcachedClientService.Instance.Client;
 - }
 - public override void Set(string key, object value, System.DateTimeOffset absoluteExpiration, string regionName = null)
 - {
 - Enforce.NotNull(key, "key");
 - CacheItem item = new CacheItem(key, value, regionName);
 - CacheItemPolicy policy = new CacheItemPolicy();
 - policy.AbsoluteExpiration = absoluteExpiration;
 - Set(item, policy);
 - }
 - public override void Set(CacheItem item, CacheItemPolicy policy)
 - {
 - if (item == null || item.Value == null)
 - return;
 - item.Key = item.Key.ToLower();
 - if (policy != null && policy.ChangeMonitors != null && policy.ChangeMonitors.Count > 0)
 - throw new NotSupportedException("Change monitors are not supported");
 - // max timeout in scaleout = 65535
 - TimeSpan expire = (policy.AbsoluteExpiration.Equals(null)) ?
 - policy.SlidingExpiration :
 - (policy.AbsoluteExpiration - DateTimeOffset.Now);
 - double timeout = expire.TotalMinutes;
 - if (timeout > 65535)
 - timeout = 65535;
 - else if (timeout > 0 && timeout < 1)
 - timeout = 1;
 - this._client.Store(Enyim.Caching.Memcached.StoreMode.Set, item.Key.ToString(), item.Value);
 - }
 - public override object this[string key]
 - {
 - get
 - {
 - return Get(key);
 - }
 - set
 - {
 - Set(key, value, null);
 - }
 - }
 - public override object AddOrGetExisting(string key, object value, CacheItemPolicy policy, string regionName = null)
 - {
 - CacheItem item = GetCacheItem(key, regionName);
 - if (item == null)
 - {
 - Set(new CacheItem(key, value, regionName), policy);
 - return value;
 - }
 - return item.Value;
 - }
 - public override CacheItem AddOrGetExisting(CacheItem value, CacheItemPolicy policy)
 - {
 - CacheItem item = GetCacheItem(value.Key, value.RegionName);
 - if (item == null)
 - {
 - Set(value, policy);
 - return value;
 - }
 - return item;
 - }
 - public override object AddOrGetExisting(string key, object value, System.DateTimeOffset absoluteExpiration, string regionName = null)
 - {
 - CacheItem item = new CacheItem(key, value, regionName);
 - CacheItemPolicy policy = new CacheItemPolicy();
 - policy.AbsoluteExpiration = absoluteExpiration;
 - return AddOrGetExisting(item, policy);
 - }
 - public override bool Contains(string key, string regionName = null)
 - {
 - return false;
 - }
 - public override CacheEntryChangeMonitor CreateCacheEntryChangeMonitor(System.Collections.Generic.IEnumerable
 keys, string regionName = null) - {
 - throw new System.NotImplementedException();
 - }
 - public override DefaultCacheCapabilities DefaultCacheCapabilities
 - {
 - get
 - {
 - return
 - DefaultCacheCapabilities.OutOfProcessProvider |
 - DefaultCacheCapabilities.AbsoluteExpirations |
 - DefaultCacheCapabilities.SlidingExpirations |
 - DefaultCacheCapabilities.CacheRegions;
 - }
 - }
 - public override object Get(string key, string regionName = null)
 - {
 - key = key.ToLower();
 - return this._client.Get(key);
 - }
 - public override CacheItem GetCacheItem(string key, string regionName = null)
 - {
 - object value = Get(key, regionName);
 - if (value != null)
 - return new CacheItem(key, value, regionName);
 - return null;
 - }
 - public override long GetCount(string regionName = null)
 - {
 - return -1;
 - }
 - protected override System.Collections.Generic.IEnumerator
 > GetEnumerator() - {
 - throw new System.NotImplementedException();
 - }
 - public override System.Collections.Generic.IDictionary
 GetValues(System.Collections.Generic.IEnumerable keys, string regionName = null) - {
 - throw new System.NotImplementedException();
 - }
 - public override string Name
 - {
 - get { return "MemcachedProvider"; }
 - }
 - public override object Remove(string key, string regionName = null)
 - {
 - key = key.ToLower();
 - return this._client.Remove(key);
 - }
 - public override void Set(string key, object value, CacheItemPolicy policy, string regionName = null)
 - {
 - Set(new CacheItem(key, value, regionName), policy);
 - }
 - #endregion
 - #region ICacheBuilder Members
 - public ObjectCache GetInstance()
 - {
 - return this;
 - }
 - public string DefaultRegionName
 - {
 - get { throw new NotImplementedException(); }
 - }
 - #endregion
 - }
 
分布式缓存提供者Windows Server AppFabric Caching:
- public class AppFabricCacheProvider : ObjectCache, ICacheBuilder
 - {
 - public static DataCache factory = null;
 - public static object syncObj = new object();
 - public override object AddOrGetExisting(string key, object value, CacheItemPolicy policy, string regionName = null)
 - {
 - CacheItem item = GetCacheItem(key, regionName);
 - if (item == null)
 - {
 - Set(new CacheItem(key, value, regionName), policy);
 - return value;
 - }
 - return item.Value;
 - }
 - public override CacheItem AddOrGetExisting(CacheItem value, CacheItemPolicy policy)
 - {
 - CacheItem item = GetCacheItem(value.Key, value.RegionName);
 - if (item == null)
 - {
 - Set(value, policy);
 - return value;
 - }
 - return item;
 - }
 - public override object AddOrGetExisting(string key, object value, System.DateTimeOffset absoluteExpiration, string regionName = null)
 - {
 - CacheItem item = new CacheItem(key, value, regionName);
 - CacheItemPolicy policy = new CacheItemPolicy();
 - policy.AbsoluteExpiration = absoluteExpiration;
 - return AddOrGetExisting(item, policy);
 - }
 - public override bool Contains(string key, string regionName = null)
 - {
 - return Get(key, regionName) != null;
 - }
 - public override CacheEntryChangeMonitor CreateCacheEntryChangeMonitor(System.Collections.Generic.IEnumerable
 keys, string regionName = null) - {
 - throw new NotImplementedException();
 - }
 - public override DefaultCacheCapabilities DefaultCacheCapabilities
 - {
 - get
 - {
 - return
 - DefaultCacheCapabilities.OutOfProcessProvider |
 - DefaultCacheCapabilities.AbsoluteExpirations |
 - DefaultCacheCapabilities.SlidingExpirations |
 - DefaultCacheCapabilities.CacheRegions;
 - }
 - }
 - public override object Get(string key, string regionName = null)
 - {
 - key = key.ToLower();
 - CreateRegionIfNeeded();
 - return (regionName == null) ?
 - CacheFactory.Get(key) :
 - CacheFactory.Get(key, regionName);
 - }
 - public override CacheItem GetCacheItem(string key, string regionName = null)
 - {
 - object value = Get(key, regionName);
 - if (value != null)
 - return new CacheItem(key, value, regionName);
 - return null;
 - }
 - public override long GetCount(string regionName = null)
 - {
 - if (string.IsNullOrEmpty(regionName))
 - throw new NotSupportedException();
 - return CacheFactory.GetObjectsInRegion(regionName).LongCount();
 - }
 - protected override System.Collections.Generic.IEnumerator
 > GetEnumerator() - {
 - throw new NotSupportedException();
 - }
 - public override System.Collections.Generic.IDictionary
 GetValues(System.Collections.Generic.IEnumerable keys, string regionName = null) - {
 - if (string.IsNullOrEmpty(regionName))
 - throw new NotSupportedException();
 - return CacheFactory.GetObjectsInRegion(regionName).ToDictionary(x => x.Key, x => x.Value);
 - }
 - public override string Name
 - {
 - get { return "AppFabric"; }
 - }
 - public override object Remove(string key, string regionName = null)
 - {
 - key = key.ToLower();
 - CreateRegionIfNeeded();
 - return (regionName == null) ?
 - CacheFactory.Remove(key) :
 - CacheFactory.Remove(key, regionName);
 - }
 - public override void Set(string key, object value, CacheItemPolicy policy, string regionName = null)
 - {
 - Set(new CacheItem(key, value, regionName), policy);
 - }
 - public override void Set(CacheItem item, CacheItemPolicy policy)
 - {
 - if (item == null || item.Value == null)
 - return;
 - if (policy != null && policy.ChangeMonitors != null && policy.ChangeMonitors.Count > 0)
 - throw new NotSupportedException("Change monitors are not supported");
 - item.Key = item.Key.ToLower();
 - CreateRegionIfNeeded();
 - TimeSpan expire = (policy.AbsoluteExpiration.Equals(null)) ?
 - policy.SlidingExpiration :
 - (policy.AbsoluteExpiration - DateTimeOffset.Now);
 - if (string.IsNullOrEmpty(item.RegionName))
 - CacheFactory.Put(item.Key, item.Value, expire);
 - else
 - CacheFactory.Put(item.Key, item.Value, expire, item.RegionName);
 - }
 - private static DataCache CacheFactory
 - {
 - get
 - {
 - if (factory == null)
 - {
 - lock (syncObj)
 - {
 - if (factory == null)
 - {
 - DataCacheFactory cacheFactory = new DataCacheFactory();
 - factory = cacheFactory.GetDefaultCache();
 - }
 - }
 - }
 - return factory;
 - }
 - }
 - private void CreateRegionIfNeeded() 163: {
 - try
 - {
 - CacheFactory.CreateRegion(DefaultRegionName);
 - }
 - catch (DataCacheException ex)
 - {
 - if (!ex.ErrorCode.Equals(DataCacheErrorCode.RegionAlreadyExists))
 - throw ex;
 - }
 - }
 - public override void Set(string key, object value, System.DateTimeOffset absoluteExpiration, string regionName = null)
 - {
 - CacheItem item = new CacheItem(key, value, regionName);
 - CacheItemPolicy policy = new CacheItemPolicy();
 - policy.AbsoluteExpiration = absoluteExpiration;
 - Set(item, policy);
 - }
 - public override object this[string key]
 - {
 - get
 - {
 - return Get(key, DefaultRegionName);
 - }
 - set
 - {
 - Set(key, value, null, DefaultRegionName);
 - }
 - }
 - public ObjectCache GetInstance()
 - {
 - return this;
 - }
 - public string DefaultRegionName
 - {
 - get
 - {
 - string defaultRegion= FrameworkConfiguationManager.GetConfiguration().GetAppVariable("AppFabricCacheDefaultRegion");
 - if (string.IsNullOrEmpty(defaultRegion))
 - {
 - defaultRegion = "Default";
 - }
 - return defaultRegion;
 - }
 - }
 - }
 
输出缓存对于改善性能有很大好处,在ASP.NET 4.0中可以自定义输出缓存的策略,比如把输出保存在磁盘中,外部的memcached服务中等等。甚至还可以定义一些高级规则,比如为A页面使用A输出缓存策略来把数据保存于内存中,为B页面使用B输出缓存策略来把数据保存于磁盘中。
代码例子可以参看文章http://www.buraksenyurt.com/post/AspNet-40-Custom-Cache-Provider.aspx,在web.config中配置
在ASP.NET 4 的默认输出缓存策略中。所有的HTTP响应、所呈现的页面和控件缓存均使用上例所示的默认输出缓存提供程序(其中defaultProvider属性值为AspNetInternalProvider)。通过为defaultProvider指定不同的提供程序。就可以更改web应用程序的默认输出缓存提供程序。
另外,还可以针对每个用户控件和各个请求选择不同的输出缓存提供程序。要为不同的Web用户控件选择不同的输出缓存提供程序,最简便的方法是设置页面或控件指令中新增加的providerName属性,如下面的示例所示:
- <%@ OutputCache Duration="60" VaryByParam="None"
 - providerName="DiskBasedCacheProvider" %>
 
若要为某个HTTP请求指定不同的输出缓存提供程序,可以覆盖Global.asax文件中新增加的GetOutputCacheProviderName方法,以编程的方式指定要用于特定请求的提供程序。
原文地址:.NET4.0的可扩展缓存系统
链接:http://www.cnblogs.com/shanyou/archive/2010/07/01/1769547.html
【编辑推荐】
                新闻标题:详解.NET 4.0可扩展缓存框架
                
                分享链接:http://www.csdahua.cn/qtweb/news49/307999.html
            
网站建设、网络推广公司-快上网,是专注品牌与效果的网站制作,网络营销seo公司;服务项目有等
声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 快上网