小编给大家分享一下.NET 4.0可扩展缓存框架的示例分析,希望大家阅读完这篇文章之后都有所收获,下面让我们一起去探讨吧!
.NET Framework中,叫做System.Runtime.Caching,这不仅是个缓存库,还是个框架,可以在上面开发自己的库。ObjectCache定义了所有缓存都要实现的通用操作。与之搭配的是个内存缓存实现,叫做MemoryCache。这个缓存系统的结构如下:
上图大家可以看出来对应那些产品了吗?
下面我给大家介绍一个实现这样一个架构的代码示例,代码的核心就是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<string> 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<System.Collections.Generic.KeyValuePair<string, object>> GetEnumerator() { throw new System.NotImplementedException(); } public override System.Collections.Generic.IDictionary<string, object> GetValues(System.Collections.Generic.IEnumerable<string> 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<string> 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<System.Collections.Generic.KeyValuePair<string, object>> GetEnumerator() { throw new NotSupportedException(); } public override System.Collections.Generic.IDictionary<string, object> GetValues(System.Collections.Generic.IEnumerable<string> 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中配置
<caching> <outputCache defaultProvider="AspNetInternalProvider"> <providers> <add name="DiskBasedCacheProvider" type="CustomCaching.DiskCacheProvider,CustomCaching"/> </providers> </outputCache> </caching>
在ASP.NET 4 的默认输出缓存策略中。所有的HTTP响应、所呈现的页面和控件缓存均使用上例所示的默认输出缓存提供程序(其中defaultProvider属性值为AspNetInternalProvider)。通过为defaultProvider指定不同的提供程序。就可以更改web应用程序的默认输出缓存提供程序。
另外,还可以针对每个用户控件和各个请求选择不同的输出缓存提供程序。要为不同的Web用户控件选择不同的输出缓存提供程序,最简便的方法是设置页面或控件指令中新增加的providerName属性,如下面的示例所示:
<%@ OutputCache Duration="60" VaryByParam="None" providerName="DiskBasedCacheProvider" %>
若要为某个HTTP请求指定不同的输出缓存提供程序,可以覆盖Global.asax文件中新增加的GetOutputCacheProviderName方法,以编程的方式指定要用于特定请求的提供程序。
看完了这篇文章,相信你对“.NET 4.0可扩展缓存框架的示例分析”有了一定的了解,如果想了解更多相关知识,欢迎关注编程网行业资讯频道,感谢各位的阅读!