| 本篇文章具體官方解釋請參照以下鏈接: 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./releases/view/43135 企業(yè)庫緩存應(yīng)用程序模塊包括了以下特點: 
 下面介紹如何使用Microsoft Enterprise Library 5.0中的緩存應(yīng)用程序模塊. 1.下載安裝好MicrosoftEnterprise Library 5.0,然后在運行EntLibConfig.exe 
 
 2. 選擇Blocks菜單 ,單擊 Add CachingSettings . 
 配置屬性說明: 
 3. 點擊 File 菜單,單擊 Save,保存為一個App.config文件,可以先保存到桌面,之后要用到它. 用記事本打開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)建一個應(yīng)用程序來使用我們配置好的緩存應(yīng)用程序模塊了,在此我創(chuàng)建了一個名為test的控制臺應(yīng)用程序,并將剛才保存的App.config文件拷貝到工程文件夾之下: 
 5. 要使用緩存應(yīng)用程序模塊, 需要導(dǎo)入相應(yīng)的Dll文件,在此我們要導(dǎo)入的是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對象保存到緩存中,在實際應(yīng)用中我們常常是用于存儲數(shù)據(jù)庫中讀取出的DataSet的.要注意的是: (1) 讀取出來的數(shù)據(jù)要進(jìn)行類型轉(zhuǎn)換為你需要的類型. (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 { staticvoid Main(string[] args) { //創(chuàng)建CacheManager CacheManager cacheManager = (CacheManager)CacheFactory.GetCacheManager(); //添加緩存項 cacheManager.Add("MyDataReader", "123"); //獲取緩存項 string str = (String)cacheManager.GetData("MyDataReader"); //打印 Console.WriteLine(str); } } } 運行結(jié)果: 
 7. 移除項目. //移除緩存項 cacheManager.Remove("MyDataReader"); 接下來還要寫緩存應(yīng)用程序模塊的高級應(yīng)用,請大家關(guān)注,如有錯誤的地方也希望大家指出. | 
|  |