微軟企業庫5.0—緩衝模組

來源:互聯網
上載者:User

本篇文章具體官方解釋請參照以下連結: 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");

                聯繫我們

                該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.