Fork me on GitHub
微软企业库5.0学习笔记(五)

本篇文章具体官方解释请参照以下链接: 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:

 

企业库缓存应用程序模块包括了以下特点:

  • 可以使用图形界面对企业库缓存应用程序模块进行配置设置.
  • 您可以配置一个持久的存储单元,或者使用独立存储或企业库数据访问应用程序模块, 其状态与内存中的缓存同步.
  • 管理员可以管理的配置使用组策略工具.
  • 可以通过创建自定义扩展的过期策略和存储单元的模块.
  • 可以保证线程安全.

下面介绍如何使用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");

                 


                http://www.cnblogs.com/huangcong/archive/2010/05/26/1744843.html

                posted on 2010-06-01 22:08  HackerVirus  阅读(1274)  评论(0编辑  收藏  举报