System.Web.Caching.Cache是.net为我们提供的一个轻量级的缓存组件,它提供了最基础的增,增过期,删除,获取等操作,下面是对Cache进行的一个封装,方便以后统一调用和扩展。
/// <summary> /// 缓o存?相à关?的?操ù作÷类à /// 占?占? /// </summary> public class DataCache { /// <summary> /// 获?取?当±前°应|用?程ì序ò指?定¨CacheKey的?Cache值μ /// </summary> /// <param name="CacheKey"></param> /// <returns></returns> public static object GetCache(string CacheKey) { System.Web.Caching.Cache objCache = HttpRuntime.Cache; return objCache[CacheKey]; } /// <summary> /// 设è置?当±前°应|用?程ì序ò指?定¨CacheKey的?Cache值μ /// 调÷用?:oDataCache.SetCache("name", "zzl"); /// </summary> /// <param name="CacheKey"></param> /// <param name="objObject"></param> public static void SetCache(string CacheKey, object objObject) { System.Web.Caching.Cache objCache = HttpRuntime.Cache; objCache.Insert(CacheKey, objObject); } /// <summary> /// 设è置?当±前°应|用?程ì序ò指?定¨CacheKey的?Cache值μ /// 调÷用?:oDataCache.SetCache("name", "zzl", DateTime.Now.AddMinutes(1), TimeSpan.Zero); /// </summary> /// <param name="CacheKey"></param> /// <param name="objObject"></param> public static void SetCache(string CacheKey, object objObject, DateTime absoluteExpiration, TimeSpan slidingExpiration) { System.Web.Caching.Cache objCache = HttpRuntime.Cache; objCache.Insert(CacheKey, objObject, null, absoluteExpiration, slidingExpiration); } /// <summary> /// 移?除y指?定¨Cache /// </summary> /// <param name="CacheKey"></param> public static void RemoveCache(string CacheKey) { System.Web.Caching.Cache objCache = HttpRuntime.Cache; objCache.Remove(CacheKey); } #region Events /// <summary> /// 缓o存?删?除y事?件t /// </summary> public static event CacheEventHandler CacheDeleted; #endregion #region Methods /// <summary> /// 触¥发¢缓o存?删?除y事?件t /// </summary> public static void OnCacheDeleted(string key) { if ((CacheDeleted != null)) { CacheDeleted(null, new CacheEventArgs(key)); } Remove(key); } #endregion } #region Delegates /// <summary> /// 缓o存?委ˉ托D /// </summary> /// <param name="sender"></param> /// <param name="e"></param> public delegate void CacheEventHandler(object sender, CacheEventArgs e); #endregion /// <summary> /// 缓o存?事?件t源′ /// </summary> public class CacheEventArgs { public CacheEventArgs() { } public CacheEventArgs(string cacheKey) { this.CacheKey = cacheKey; } public string CacheKey { get; set; } }
在这个类中,有一个关于删除键时的事件,它的作用是在删除某个键时,进行与它关联的其它操作,例如:在用户登出时,希望将它的cache记录清除,并删除一些记录性的操作,但记录性的操作是不固定的,为了为破坏程序的完整性,我们可以在登出时,先去将其需要的操作进行有订阅,如果以后有其它删除cache键需要的关联操作,只要订阅就可以了
代码如下:
static CommonMethod() { iUserRepository = new UserRepository(); //订?阅?了?CacheDeleted这a个?事?件t,?当±它ü被?触¥发¢时±,?将?自?动ˉ执′行D被?绑ó定¨的?方?法¨ DataCache.CacheDeleted += new CacheEventHandler(ClearUserRecord); DataCache.CacheDeleted += new CacheEventHandler(ClearProductRecord); }
#region 删?除yCache后ó,?需è要a做?的?其?它ü事?情é static void ClearUserRecord(object sender, CacheEventArgs e) { DataCache.SetCache("msg1", e.CacheKey + "被?删?除y了?"); } static void ClearProductRecord(object sender, CacheEventArgs e) { DataCache.SetCache("msg2", e.CacheKey + "真?的?被?删?除y了?"); } #endregion
在登出操作后,可以去触发删除cache键的操作:
public static ActionResult UserLogOutModule(Controller controller) { string returnUrl = controller.Request.QueryString["returnUrl"]; Standard.ClientHelper.UserLogout(); DataCache.OnCacheDeleted("name");