[转载]asp.net高性能之路:无缝切换HttpRuntime.Cache与Memcached,附代码 – Ray Wu – 博客园.
概述
之前网站一直使用ASP.NET自带的cache,也就是HttpRuntime.Cache。这个的优点是进程内cache,效率非常高,同时对于缓存的对象可以直接获得
引用,并进行修改,不需要再进行清空缓存。但是使用HttpRuntime.Cache,无法进行扩展,也无法使用web园等等。
方案
之前有看dudu写的关于northscale memcached的文章,觉得很不错,故进行了一下尝试。由于初次使用,出问题的时候要能随时切换回HttpRuntime.Cache,
故使用了策略模式,实现无缝切换缓存模式的功能。Memcached的封装类请在https://github.com/enyim/EnyimMemcached/downloads进行下载,我使用的是Northscale.Store.2.8
接口
using System; using System.Collections.Generic; using System.Web; /// <summary> /// 缓存策略接口 /// </summary> public interface ICacheStrategy { void AddObject(string objId, object o); void AddObjectWithTimeout(string objId, object o, int timeoutSec); void AddObjectWithFileChange(string objId, object o, string file); //void AddObjectWithDepend(string objId, object o, string[] dependKey); void RemoveObject(string objId); object RetrieveObject(string objId); int TimeOut { set; get; } }
HttpRuntime.Cache实现类
using System; using System.Collections.Generic; using System.Web; using System.Web.Caching; /// <summary> /// 默认的缓存策略,实现了缓存策略接口 /// </summary> public class DefaultCacheStrategy : ICacheStrategy { private static readonly DefaultCacheStrategy instance = new DefaultCacheStrategy(); protected static volatile System.Web.Caching.Cache webCache = System.Web.HttpRuntime.Cache; protected int _timeOut = 1; //默认缓存一分钟,也可以单独设置对象的超时时间 /// <summary> /// Initializes the <see cref="DefaultCacheStrategy"/> class. /// </summary> static DefaultCacheStrategy() { //lock (syncObj) //{ // //System.Web.HttpContext context = System.Web.HttpContext.Current; // //if(context != null) // // webCache = context.Cache; // //else // webCache = System.Web.HttpRuntime.Cache; //} } public int TimeOut { set { _timeOut = value > 0 ? value : 6000; } get { return _timeOut > 0 ? _timeOut : 6000; } } public static System.Web.Caching.Cache GetWebCacheObj { get { return webCache; } } public void AddObject(string objId, object o) { if (objId == null || objId.Length == 0 || o == null) { return; } CacheItemRemovedCallback callBack = new CacheItemRemovedCallback(onRemove); if (TimeOut == 6000) { webCache.Insert(objId, o, null, DateTime.MaxValue, TimeSpan.Zero, System.Web.Caching.CacheItemPriority.High, callBack); } else { webCache.Insert(objId, o, null, DateTime.Now.AddMinutes(TimeOut), System.Web.Caching.Cache.NoSlidingExpiration, System.Web.Caching.CacheItemPriority.High, callBack); } } public void AddObjectWithTimeout(string objId, object o, int timeoutSec) { if (objId == null || objId.Length == 0 || o == null || timeoutSec <= 0) { return; } CacheItemRemovedCallback callBack = new CacheItemRemovedCallback(onRemove); webCache.Insert(objId, o, null, System.DateTime.Now.AddSeconds(timeoutSec), System.Web.Caching.Cache.NoSlidingExpiration, System.Web.Caching.CacheItemPriority.High, callBack); } public void AddObjectWithFileChange(string objId, object o, string file) { if (objId == null || objId.Length == 0 || o == null) { return; } CacheItemRemovedCallback callBack = new CacheItemRemovedCallback(onRemove); CacheDependency dep = new CacheDependency(file); webCache.Insert(objId, o, dep, Cache.NoAbsoluteExpiration, TimeSpan.FromHours(1), System.Web.Caching.CacheItemPriority.High, callBack); } //public void AddObjectWithDepend(string objId, object o, string[] dependKey) //{ // if (objId == null || objId.Length == 0 || o == null) // { // return; // } // CacheItemRemovedCallback callBack = new CacheItemRemovedCallback(onRemove); // CacheDependency dep = new CacheDependency(null, dependKey, DateTime.Now); // webCache.Insert(objId, o, dep, System.DateTime.Now.AddMinutes(TimeOut), System.Web.Caching.Cache.NoSlidingExpiration, System.Web.Caching.CacheItemPriority.High, callBack); //} public void onRemove(string key, object val, CacheItemRemovedReason reason) { switch (reason) { case CacheItemRemovedReason.DependencyChanged: break; case CacheItemRemovedReason.Expired: { //CacheItemRemovedCallback callBack = new CacheItemRemovedCallback(this.onRemove); //webCache.Insert(key, val, null, System.DateTime.Now.AddMinutes(TimeOut), // System.Web.Caching.Cache.NoSlidingExpiration, // System.Web.Caching.CacheItemPriority.High, // callBack); break; } case CacheItemRemovedReason.Removed: { break; } case CacheItemRemovedReason.Underused: { break; } default: break; } //TODO: write log here } public void RemoveObject(string objId) { //objectTable.Remove(objId); if (objId == null || objId.Length == 0) { return; } webCache.Remove(objId); } public object RetrieveObject(string objId) { //return objectTable[objId]; if (objId == null || objId.Length == 0) { return null; } return webCache.Get(objId); } }
Memcached 实现类
using System; using System.Collections.Generic; using System.Web; using NorthScale.Store; using Enyim.Caching.Memcached; /// <summary> /// Summary description for EnyimMemcachedProvider /// </summary> public class EnyimMemcachedProvider { private static NorthScaleClient client; static EnyimMemcachedProvider() { try { client = new NorthScaleClient(); } catch (Exception ex) { log4net.LogManager.GetLogger("SojumpLog").Info("EnyimMemcachedProvider", ex); } } #region ICacheProvider Members public void Add(string key, object value) { if (client != null) { client.Store(StoreMode.Set, key, value); } } public void Add(string key, object value, int cacheSecond) { if (client != null) { client.Store(StoreMode.Set, key, value, new TimeSpan(0, 0, cacheSecond)); } } public object GetData(string key) { if (client == null) { return null; } return client.Get(key); } public void Remove(string key) { if (client != null) { client.Remove(key); } } #endregion }
策略类
using System; using System.Collections.Generic; using System.Web; using System.Configuration; /// <summary> /// The caching manager /// </summary> public class CachingManager { private static ICacheStrategy cs; private static volatile CachingManager instance = null; private static object lockHelper = new object(); //private static System.Timers.Timer cacheConfigTimer = new System.Timers.Timer(15000);//Interval in ms static CachingManager() { string type = ConfigurationManager.AppSettings["CacheStrategy"]; if (type == "2") cs = new MemCacheStrategy(); else cs = new DefaultCacheStrategy(); ////Set timer //cacheConfigTimer.AutoReset = true; //cacheConfigTimer.Enabled = true; //cacheConfigTimer.Elapsed += new System.Timers.ElapsedEventHandler(Timer_Elapsed); //cacheConfigTimer.Start(); } private static void Timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e) { //TODO: } public static CachingManager GetCachingService() { if (instance == null) { lock (lockHelper) { if (instance == null) { instance = new CachingManager(); } } } return instance; } public virtual void AddObject(string key, object o) { if (String.IsNullOrEmpty(key) || o == null) return; lock (lockHelper) { if (cs.TimeOut <= 0) return; cs.AddObject(key, o); } } public virtual void AddObject(string key, object o, int timeout) { if (String.IsNullOrEmpty(key) || o == null) return; lock (lockHelper) { if (cs.TimeOut <= 0) return; cs.AddObjectWithTimeout(key, o, timeout); } } public virtual void AddObject(string key, object o, string file) { if (String.IsNullOrEmpty(key) || o == null) return; lock (lockHelper) { if (cs.TimeOut <= 0) return; cs.AddObjectWithFileChange(key, o, file); } } public virtual object RetrieveObject(string objectId) { return cs.RetrieveObject(objectId); } public virtual void RemoveObject(string key) { lock (lockHelper) { cs.RemoveObject(key); } } public void LoadCacheStrategy(ICacheStrategy ics) { lock (lockHelper) { cs = ics; } } //public void LoadDefaultCacheStrategy() //{ // lock (lockHelper) // { // cs = new DefaultCacheStrategy(); // } //} }
调用代码
string key =”test”
CachingManager cm = new CachingManager();
cm.AddObject(key,new Object(),60);//缓存对象60秒。
不足
Memcached无法使用CacheDependency,需要自己去进行处理。如你的缓存对象依赖于文件,则在文件修改时要直接清空缓存。
Memcached也无法清空某一类的缓存对象,有时因为数据库做了修改,你要清空key以activity_开头的一系列对象的话,是做不到的。
变通的方案是先将key加入到List或Dictionary中,如下代码:
public void ClearCacheByPattern(string pattern) { if(client!=null) return; object c = client.Get("globel_cacheitems"); if (c==null) return; List<string> cacheitems = (List<string>) c; foreach (string cacheitem in cacheitems) { if (cacheitem.StartsWith(pattern)) { client.Remove(cacheitem); } } }
补充:感谢园友YLH对批量删除缓存的回复:
Memcached为了高性能而设计,功能少了很多。 如果内存不吃紧的话,批量删除缓存项,可以采用设置key分区,为分区加上版本号来解决。
要批量删除一个分区的缓存,只需要升级一下缓存分区的版本号即可。目前我们在项目中就是采用这种方案。
这个方案是目前批量删除缓存项的比较完美的方案了,多谢YLH!
如有什么问题,请在下面回复,大家一起讨论。