本篇文章具體官方解釋請參照以下連結: http://msdn.microsoft.com/en-us/library/ff664753%28v=PandP.50%29.aspx
MicrosoftEnterprise Library 5.0: http://www.microsoft.com/downloads/details.aspx?FamilyId=bcb166f7-dd16-448b-a152-9845760d9b4c&displaylang=en
MicrosoftEnterprise Library 5.0 Documentation : http://entlib.codeplex.com/releases/view/43135
CodeDemo Download:
企業庫緩衝應用程式模組包括了以下特點:
- 可以使用圖形介面對企業庫緩衝應用程式模組進行配置設定.
- 您可以配置一個持久的儲存單元,或者使用隔離儲存區 (Isolated Storage)或企業庫資料訪問應用程式模組, 其狀態與記憶體中的緩衝同步.
- 管理員可以管理的配置使用組策略工具.
- 可以通過建立自訂擴充的到期策略和儲存單元的模組.
- 可以保證安全執行緒.
下面介紹如何使用Microsoft Enterprise Library 5.0中的緩衝應用程式模組.
1.下載安裝好MicrosoftEnterprise Library 5.0,然後在運行EntLibConfig.exe
2. 選擇Blocks菜單 ,單擊 Add CachingSettings .
配置屬性說明:
3. 點擊 File 菜單,單擊 Save,儲存為一個App.config檔案,可以先儲存到案頭,之後要用到它. 用記事本開啟App.config,可以看到如下內容.
代碼
<configuration>
<configSections>
<sectionname="cachingConfiguration"type="Microsoft.Practices.EnterpriseLibrary.Caching.Configuration.CacheManagerSettings,Microsoft.Practices.EnterpriseLibrary.Caching, Version=5.0.414.0,Culture=neutral, PublicKeyToken=31bf3856ad364e35" requirePermission="true"/>
</configSections>
<cachingConfigurationdefaultCacheManager="Cache Manager">
<cacheManagers>
<addname="Cache Manager"type="Microsoft.Practices.EnterpriseLibrary.Caching.CacheManager,Microsoft.Practices.EnterpriseLibrary.Caching, Version=5.0.414.0,Culture=neutral, PublicKeyToken=31bf3856ad364e35"
expirationPollFrequencyInSeconds="60"maximumElementsInCacheBeforeScavenging="1000"
numberToRemoveWhenScavenging="10" backingStoreName="NullBackingStore"/>
</cacheManagers>
<backingStores>
<addtype="Microsoft.Practices.EnterpriseLibrary.Caching.BackingStoreImplementations.NullBackingStore,Microsoft.Practices.EnterpriseLibrary.Caching, Version=5.0.414.0,Culture=neutral, PublicKeyToken=31bf3856ad364e35"
name="NullBackingStore" />
</backingStores>
</cachingConfiguration>
</configuration>
4. 接著可以建立一個應用程式來使用我們配置好的緩衝應用程式模組了,在此我建立了一個名為test的控制台應用程式,並將剛才儲存的App.config檔案拷貝到工程檔案夾之下:
5. 要使用緩衝應用程式模組, 需要匯入相應的Dll檔案,在此我們要匯入的是Microsoft.Practices.EnterpriseLibrary.Caching.dll ,將App.config檔案添加到項目中,並添加Microsoft.Practices.EnterpriseLibrary.Caching和using Microsoft.Practices.EnterpriseLibrary.Caching.Expirations引用:
添加引用:
using Microsoft.Practices.EnterpriseLibrary.Caching;
using Microsoft.Practices.EnterpriseLibrary.Caching.Expirations;
6. 添加和讀取快取項目,下方示範的是將一個string對象儲存到緩衝中,在實際應用中我們常常是用於儲存資料庫中讀取出的DataSet的.要注意的是:
(1) 讀取出來的資料要進行類型轉換為你需要的類型.
(2) 在讀取的時候最好檢查一下是否為空白值.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Practices.EnterpriseLibrary.Caching;
using Microsoft.Practices.EnterpriseLibrary.Caching.Expirations;
namespace test
{
class Program
{
static void Main(string[] args)
{
//建立CacheManager
CacheManager cacheManager = (CacheManager)CacheFactory.GetCacheManager();
//添加快取項目
cacheManager.Add("MyDataReader", "123");
//擷取快取項目
string str = (String)cacheManager.GetData("MyDataReader");
//列印
Console.WriteLine(str);
}
}
}
運行結果:
7. 移除項目.
//移除快取項目
cacheManager.Remove("MyDataReader");