本篇文章具體官方解釋請(qǐng)參照以下鏈接: 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
企業(yè)庫(kù)緩存應(yīng)用程序模塊包括了以下特點(diǎn):
- 可以使用圖形界面對(duì)企業(yè)庫(kù)緩存應(yīng)用程序模塊進(jìn)行配置設(shè)置.
- 您可以配置一個(gè)持久的存儲(chǔ)單元,或者使用獨(dú)立存儲(chǔ)或企業(yè)庫(kù)數(shù)據(jù)訪問(wèn)應(yīng)用程序模塊, 其狀態(tài)與內(nèi)存中的緩存同步.
- 管理員可以管理的配置使用組策略工具.
- 可以通過(guò)創(chuàng)建自定義擴(kuò)展的過(guò)期策略和存儲(chǔ)單元的模塊.
- 可以保證線程安全.
下面介紹如何使用Microsoft Enterprise Library 5.0中的緩存應(yīng)用程序模塊.
1.下載安裝好MicrosoftEnterprise Library 5.0,然后在運(yùn)行EntLibConfig.exe
2. 選擇Blocks菜單 ,單擊 Add CachingSettings .
配置屬性說(shuō)明:
3. 點(diǎn)擊 File 菜單,單擊 Save,保存為一個(gè)App.config文件,可以先保存到桌面,之后要用到它. 用記事本打開(kāi)App.config,可以看到如下內(nèi)容.
代碼<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. 接著可以創(chuàng)建一個(gè)應(yīng)用程序來(lái)使用我們配置好的緩存應(yīng)用程序模塊了,在此我創(chuàng)建了一個(gè)名為test的控制臺(tái)應(yīng)用程序,并將剛才保存的App.config文件拷貝到工程文件夾之下:
5. 要使用緩存應(yīng)用程序模塊, 需要導(dǎo)入相應(yīng)的Dll文件,在此我們要導(dǎo)入的是Microsoft.Practices.EnterpriseLibrary.Caching.dll ,將App.config文件添加到項(xiàng)目中,并添加Microsoft.Practices.EnterpriseLibrary.Caching和using Microsoft.Practices.EnterpriseLibrary.Caching.Expirations引用:
添加引用:
using Microsoft.Practices.EnterpriseLibrary.Caching;
using Microsoft.Practices.EnterpriseLibrary.Caching.Expirations;
6. 添加和讀取緩存項(xiàng),下方演示的是將一個(gè)string對(duì)象保存到緩存中,在實(shí)際應(yīng)用中我們常常是用于存儲(chǔ)數(shù)據(jù)庫(kù)中讀取出的DataSet的.要注意的是:
(1) 讀取出來(lái)的數(shù)據(jù)要進(jìn)行類型轉(zhuǎn)換為你需要的類型.
(2) 在讀取的時(shí)候最好檢查一下是否為空值.
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
{
staticvoid Main(string[] args)
{
//創(chuàng)建CacheManager
CacheManager cacheManager = (CacheManager)CacheFactory.GetCacheManager();
//添加緩存項(xiàng)
cacheManager.Add("MyDataReader", "123");
//獲取緩存項(xiàng)
string str = (String)cacheManager.GetData("MyDataReader");
//打印
Console.WriteLine(str);
}
}
}
運(yùn)行結(jié)果:
7. 移除項(xiàng)目.
//移除緩存項(xiàng)
cacheManager.Remove("MyDataReader");
接下來(lái)還要寫緩存應(yīng)用程序模塊的高級(jí)應(yīng)用,請(qǐng)大家關(guān)注,如有錯(cuò)誤的地方也希望大家指出.