企業庫配置緩衝詳解

來源:互聯網
上載者:User

<configuration>
<configSections>
    <section name="cachingConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Caching.Configuration.CacheManagerSettings, Microsoft.Practices.EnterpriseLibrary.Caching, Version=2.0.0.0, Culture=neutral, PublicKeyToken=null" />
</configSections>
<cachingConfiguration defaultCacheManager="Cache Manager">
    <cacheManagers>
      <add expirationPollFrequencyInSeconds="60" maximumElementsInCacheBeforeScavenging="1000"
        numberToRemoveWhenScavenging="10" backingStoreName="inMemory"
        name="Cache Manager" />
    </cacheManagers>
    <backingStores>
      <add encryptionProviderName="" type="Microsoft.Practices.EnterpriseLibrary.Caching.BackingStoreImplementations.NullBackingStore, Microsoft.Practices.EnterpriseLibrary.Caching, Version=2.0.0.0, Culture=neutral, PublicKeyToken=null"
        name="inMemory" />
    </backingStores>
</cachingConfiguration>
</configuration>

在設定檔中,我可以可以看到一個緩衝Manager 具有以下配置資訊:
1、expirationPollFrequencyInSeconds 多少秒進行一次到期檢查,預設值60秒
2、maximumElementsInCacheBeforeScavenging 最多可以緩衝對象數,超過這個個數,則觸發清除事件,預設值 1000;
3、numberToRemoveWhenScavenging 每次清除快取項目時候,按照優先順序,清除掉優先順序低的多少項。
4、backingStoreName 備份緩衝的資料(輔助儲存空間 Backing Storage),備份儲存在那裡?預設備份不緩衝。
系統預設提供了三種備份緩衝儲存方式
4.1、不緩衝 Microsoft.Practices.EnterpriseLibrary.Caching.BackingStoreImplementations.NullBackingStore

4.2、緩衝在資料庫中 Microsoft.Practices.EnterpriseLibrary.Caching.Database.DataBackingStore
注意,這個類在 microsoft.practices.enterpriselibrary.caching.database.DLL 檔案中

4.3、緩衝在一個隔離新的獨立空間
Microsoft.Practices.EnterpriseLibrary.Caching.BackingStoreImplementations.IsolatedStorageBackingStore

上面我們看到的設定檔,採用的都是預設的緩衝設定。

至於代碼部分,則更簡單,參看下面執行個體代碼:
4.1
private CacheManager primitivesCache;

primitivesCache = CacheFactory.GetCacheManager();
primitivesCache.Add("1", 234);
primitivesCache.Add("12", "guohongjun");
primitivesCache.Add("33","www.csdn.net",
    CacheItemPriority.Normal,
    null,
    new SlidingTime(TimeSpan.FromMinutes(1)));

object obj1 = primitivesCache.GetData("1");
object obj2 = primitivesCache.GetData("12");
object obj3 = primitivesCache.GetData("33");

primitivesCache.Flush();

其中
CacheManager.Add (String, Object, CacheItemPriority, ICacheItemRefreshAction, ICacheItemExpiration[])

方法,函數的參數依次是:
1、緩衝的主鍵
2、緩衝的對象
3、緩衝的優先順序,如果緩衝數量溢出,則系統自動清除優先順序低的;
4、實現 ICacheItemRefreshAction 介面的類,用於快取項目被卸載時候,接受訊息通知用。如果你想這時候更新快取項目,則需要接受並處理這個事件。
5、快取項目到期依賴項。系統預設提供了以下幾種到期方式:

5.1 指定時間點到期
範例程式碼如下,AbsoluteTime 的建構函式參數為 DateTime 型別參數:
primitivesCache.Add(.......,new AbsoluteTime(this.AbsoluteTime));

5.2 比如,你可能需要定義每周六晚上9點半到期,這時候,你就需要這種格式了。
這種格式具體的寫法,請查看原始碼 Expirations/ExtendedFormat.cs
這裡的注釋中有詳細描述
primitivesCache.Add(.......,new ExtendedFormatTime("0 0 * * *")

5.3 檔案被修改,則到期
primitivesCache.Add(.......,new FileDependency("DependencyFile.txt")

5.4 指定多長時間段之後
primitivesCache.Add(.......,new SlidingTime(TimeSpan.FromMinutes(1)));

5.5 當然還有不到期
primitivesCache.Add(.......,new NeverExpired());

當然你也可以自己寫自己的到期機制。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.