天天看點

艾偉_轉載:企業庫緩存依賴的實作-基于檔案依賴

最近在做項目的時候,采用用Codesmith和Nettiers生成的架構來實作,生成的代碼核心是基于企業庫的。是以最近在惡補企業庫,對于緩存的學習當然是必不可少的,尤其是經常要用到得緩存依賴,這裡我用到的是檔案依賴來舉例子,其他的都大同小異,主要就是要實作ICacheItemExpiration中的傳回值類型為bool類型的HasExpired方法,來控制到期與否,實作此方法是關鍵所在。下面是程式清單,歡迎大家指正:

step1 實作緩存到期接口,此類就為緩存項依賴的類,為緩存依賴的核心,尤其是其中HasExpired方法的定義,此類的核心就是使用lastCount是否變化來判斷緩存是否到期;如果有變化則HasExpired方法傳回true,否則傳回false。

using System;

using System.Web;

using Microsoft.Practices.EnterpriseLibrary.Caching;

/// 

///CacheItemDependency 的摘要說明

public class CacheItemDependency : ICacheItemExpiration

{

    //依賴緩存項鍵

    private readonly string dependencyCacheKey;

    //依賴緩存項值

    private System.Int32 lastCount;

    #region Constructor

    /// 

    /// 初始化依賴緩存項,如果此緩存管理對象存在,則取出緩存的資料;若不存在,就要對此緩存管理指派

    /// 依賴緩存項的鍵

    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; }

    #region ICacheItemExpiration Members

    public bool HasExpired()

        if (cacheManager == null)

            return true;

        System.Int32 currentCount = (int)cacheManager.GetData(dependencyCacheKey);

        if (currentCount != lastCount)

        else

            return false;

    public void Notify()

    public void Initialize(CacheItem owningCacheItem)

}

step2  定義修改依賴項緩存的方法

///DataAccessUtil 的摘要說明

public class DataAccessUtil

    public DataAccessUtil()

        //

        //TODO: 在此處添加構造函數邏輯

    /// 更新所有以cacheKeys中元素為key的緩存項

    /// 緩存項的key的數組

    public static void UpdateCacheDependency(string[] cacheKeys)

        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的緩存項,進而使依賴于此緩存項的緩存項失效.

step3  測試實體,下面隻是個簡單的測試,大家可以發散一下,寫出更加有複用性的方法。

using System.Collections.Generic;

using System.Linq;

using System.Web.UI;

using System.Web.UI.WebControls;

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)

        DataAccessUtil.UpdateCacheDependency(new string[] { "s1" });

        if (cacheManager.GetData("s") == null)

        this.Label1.Text = cacheManager.GetData("s") as string;

繼續閱讀