企業庫緩存依賴的實現-基于文件依賴
最近在做項目的時候,采用用Codesmith和Nettiers生成的框架來實現,生成的代碼核心是基于企業庫的。所以最近在惡補企業庫,對于緩存的學習當然是必不可少的,尤其是經常要用到得緩存依賴,這里我用到的是文件依賴來舉例子,其他的都大同小異,主要就是要實現ICacheItemExpiration中的返回值類型為bool類型的HasExpired方法,來控制到期與否,實現此方法是關鍵所在。下面是程序清單,歡迎大家指正:
step1 實現緩存到期接口,此類就為緩存項依賴的類,為緩存依賴的核心,尤其是其中HasExpired方法的定義,此類的核心就是使用lastCount是否變化來判斷緩存是否到期;如果有變化則HasExpired方法返回true,否則返回false。
Code using System; using System.Web; using Microsoft.Practices.EnterpriseLibrary.Caching; /// <summary> ///CacheItemDependency 的摘要說明 /// </summary> public class CacheItemDependency : ICacheItemExpiration { //依賴緩存項鍵 private readonly string dependencyCacheKey; //依賴緩存項值 private System.Int32 lastCount; #region Constructor /// <summary> /// 初始化依賴緩存項,如果此緩存管理對象存在,則取出緩存的數據;若不存在,就要對此緩存管理賦值 /// </summary> /// <param name="cacheKey">依賴緩存項的鍵</param> public CacheItemDependency(string cacheKey) { dependencyCacheKey = cacheKey; ICacheManager cacheManager = CacheFactory.GetCacheManager(); lastCount = Int32.MinValue; if (cacheManager != null) { if (cacheManager.Contains(cacheKey)) { object o = cacheManager.GetData(cacheKey); if (o != null) { this.lastCount = (int)o; } lastCount = (int)cacheManager.GetData(cacheKey); } else { cacheManager.Add(cacheKey, lastCount); } } } #endregion #region Properties public string DependencyCacheKey { get { return dependencyCacheKey; } } public System.Int32 LastCount { get { return lastCount; } } #endregion #region ICacheItemExpiration Members public bool HasExpired() { ICacheManager cacheManager = CacheFactory.GetCacheManager(); if (cacheManager == null) { return true; } System.Int32 currentCount = (int)cacheManager.GetData(dependencyCacheKey); if (currentCount != lastCount) { return true; } else { return false; } } public void Notify() { } public void Initialize(CacheItem owningCacheItem) { } #endregion }
step2 定義修改依賴項緩存的方法
Code using System; using System.Web; using Microsoft.Practices.EnterpriseLibrary.Caching; /// <summary> ///DataAccessUtil 的摘要說明 /// </summary> public class DataAccessUtil { public DataAccessUtil() { // //TODO: 在此處添加構造函數邏輯 // } /// <summary> /// 更新所有以cacheKeys中元素為key的緩存項 /// </summary> /// <param name="cacheKeys">緩存項的key的數組</param> public static void UpdateCacheDependency(string[] cacheKeys) { ICacheManager cacheManager = CacheFactory.GetCacheManager(); foreach (string cacheKey in cacheKeys) { if (cacheManager != null && cacheManager.Contains(cacheKey)) { int lastCount = (int)cacheManager.GetData(cacheKey); if (lastCount < Int32.MaxValue) { lastCount++; } else { lastCount = Int32.MinValue; } // 這一句的作用在于更新以cacheKey為key的緩存項,從而使依賴于此緩存項的緩存項失效. cacheManager.Add(cacheKey, lastCount); } } } }
step3 測試實體,下面只是個簡單的測試,大家可以發散一下,寫出更加有復用性的方法。
Code using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using Microsoft.Practices.EnterpriseLibrary.Caching; public partial class Test : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { ICacheManager cacheManager = CacheFactory.GetCacheManager(); cacheManager.Add("s", TextBox1.Text, CacheItemPriority.Normal, null, new CacheItemDependency("s1")); this.Label1.Text = cacheManager.GetData("s") as string; } } protected void Button1_Click(object sender, EventArgs e) { ICacheManager cacheManager = CacheFactory.GetCacheManager(); DataAccessUtil.UpdateCacheDependency(new string[] { "s1" }); if (cacheManager.GetData("s") == null) { cacheManager.Add("s", TextBox1.Text, CacheItemPriority.Normal, null, new CacheItemDependency("s1")); } this.Label1.Text = cacheManager.GetData("s") as string; } }
全站熱搜