熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> .NET編程 >> 正文

企業庫緩存依賴的實現-基於文件依賴

2022-06-13   來源: .NET編程 

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

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

  Codeusing System;

  using SystemWeb;

  using MicrosoftPracticesEnterpriseLibraryCaching;

  /// <summary>

  ///CacheItemDependency 的摘要說明

  /// </summary>public class CacheItemDependency : ICacheItemExpiration

  {

  //依賴緩存項鍵 private readonly string dependencyCacheKey;

  //依賴緩存項值 private SystemInt lastCount; #region Constructor /// <summary>

  /// 初始化依賴緩存項如果此緩存管理對象存在則取出緩存的數據若不存在就要對此緩存管理賦值

  /// </summary>

  /// <param name=cacheKey>依賴緩存項的鍵</param>

  public CacheItemDependency(string cacheKey)

  {

  dependencyCacheKey = cacheKey; ICacheManager cacheManager = CacheFactoryGetCacheManager();

  lastCount = IntMinValue;

  if (cacheManager != null)

  {

  if (cacheManagerContains(cacheKey))

  {

  object o = cacheManagerGetData(cacheKey);

  if (o != null)

  {

  thislastCount = (int)o;

  }

  lastCount = (int)cacheManagerGetData(cacheKey);

  }

  else

  {

  cacheManagerAdd(cacheKey lastCount);

  }

  }

  }

  #endregion #region Properties public string DependencyCacheKey

  {

  get

  {

  return dependencyCacheKey;

  }

  }

  public SystemInt LastCount

  {

  get

  { r

  eturn lastCount;

  }

  }

  #endregion #region ICacheItemExpiration Members public bool HasExpired()

  {

  ICacheManager cacheManager = CacheFactoryGetCacheManager();

  if (cacheManager == null)

  {

  return true;

  }

  SystemInt currentCount = (int)cacheManagerGetData(dependencyCacheKey);

  if (currentCount != lastCount)

  { return true; }

  else

  {

  return false;

  }

  }

  public void Notify()

  { }

  public void Initialize(CacheItem owningCacheItem) { }

  #endregion}

  using System;

  using SystemWeb;

  using MicrosoftPracticesEnterpriseLibraryCaching;

  ///

  ///CacheItemDependency 的摘要說明

  ///

  public class CacheItemDependency : ICacheItemExpiration

  {

  //依賴緩存項鍵

  private readonly string dependencyCacheKey;

  //依賴緩存項值

  private SystemInt lastCount;

  #region Constructor

  ///

  /// 初始化依賴緩存項如果此緩存管理對象存在則取出緩存的數據若不存在就要對此緩存管理賦值

  ///

  /// 依賴緩存項的鍵

  public CacheItemDependency(string cacheKey)

  {

  dependencyCacheKey = cacheKey;

  ICacheManager cacheManager = CacheFactoryGetCacheManager();

  lastCount = IntMinValue;

  if (cacheManager != null)

  {

  if (cacheManagerContains(cacheKey))

  {

  object o = cacheManagerGetData(cacheKey);

  if (o != null)

  {

  thislastCount = (int)o;

  }

  lastCount = (int)cacheManagerGetData(cacheKey);

  }

  else

  {

  cacheManagerAdd(cacheKey lastCount);

  }

  }

  }

  #endregion

  #region Properties

  public string DependencyCacheKey

  {

  get { return dependencyCacheKey; }

  }

  public SystemInt LastCount

  {

  get { return lastCount; }

  }

  #endregion

  #region ICacheItemExpiration Members

  public bool HasExpired()

  {

  ICacheManager cacheManager = CacheFactoryGetCacheManager();

  if (cacheManager == null)

  {

  return true;

  }

  SystemInt currentCount = (int)cacheManagerGetData(dependencyCacheKey);

  if (currentCount != lastCount)

  {

  return true;

  }

  else

  {

  return false;

  }

  }

  public void Notify()

  {

  }

  public void Initialize(CacheItem owningCacheItem)

  {

  }

  #endregion

  }

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

  Codeusing System;using SystemWeb;using MicrosoftPracticesEnterpriseLibraryCaching;

  /// <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 = CacheFactoryGetCacheManager();

  foreach (string cacheKey in cacheKeys)

  {

  if (cacheManager != null && cacheManagerContains(cacheKey))

  {

  int lastCount = (int)cacheManagerGetData(cacheKey); if (lastCount < IntMaxValue) { lastCount++;

  }

  else

  {

  lastCount = IntMinValue;

  } // 這一句的作用在於更新以cacheKey為key的緩存項從而使依賴於此緩存項的緩存項失效 cacheManagerAdd(cacheKey lastCount);

  }

  }

  }

  }

  using System;

  using SystemWeb;

  using MicrosoftPracticesEnterpriseLibraryCaching;

  ///

  ///DataAccessUtil 的摘要說明

  ///

  public class DataAccessUtil

  {

  public DataAccessUtil()

  {

  //

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

  //

  }

  ///

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

  ///

  /// 緩存項的key的數組

  public static void UpdateCacheDependency(string[] cacheKeys)

  {

  ICacheManager cacheManager = CacheFactoryGetCacheManager();

  foreach (string cacheKey in cacheKeys)

  {

  if (cacheManager != null && cacheManagerContains(cacheKey))

  {

  int lastCount = (int)cacheManagerGetData(cacheKey);

  if (lastCount < IntMaxValue)

  {

  lastCount++;

  }

  else

  {

  lastCount = IntMinValue;

  }

  // 這一句的作用在於更新以cacheKey為key的緩存項從而使依賴於此緩存項的緩存項失效

  cacheManagerAdd(cacheKey lastCount);

  }

  }

  }

  }

  step  測試實體下面只是個簡單的測試大家可以發散一下寫出更加有復用性的方法

  Codeusing System;

  using SystemCollectionsGeneric;

  using SystemLinq;

  using SystemWeb;

  using SystemWebUI;

  using SystemWebUIWebControls;

  using MicrosoftPracticesEnterpriseLibraryCaching;

  public partial class Test : SystemWebUIPage

  {

  protected void Page_Load(object sender EventArgs e)

  {

  if (!IsPostBack)

  {

  ICacheManager cacheManager = CacheFactoryGetCacheManager();

  cacheManagerAdd(s TextBoxText CacheItemPriorityNormal null new CacheItemDependency(s));

  thisLabelText = cacheManagerGetData(s) as string;

  }

  } protected void Button_Click(object sender EventArgs e)

  {

  ICacheManager cacheManager = CacheFactoryGetCacheManager();

  DataAccessUtilUpdateCacheDependency(new string[] { s });

  if (cacheManagerGetData(s) == null)

  {

  cacheManagerAdd(s TextBoxText CacheItemPriorityNormal null new CacheItemDependency(s));

  } thisLabelText = cacheManagerGetData(s) as string;

  }

  }


From:http://tw.wingwit.com/Article/program/net/201311/13594.html
    推薦文章
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.