原文:黄聪:Microsoft Enterprise Library 5.0 系列教程(一) : Caching Application Block (初级)

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

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

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

下面介绍如何使用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    {        staticvoid Main(string[] args)        {            //创建CacheManager            CacheManager cacheManager = (CacheManager)CacheFactory.GetCacheManager();            //添加缓存项            cacheManager.Add("MyDataReader", "");            //获取缓存项string str = (String)cacheManager.GetData("MyDataReader");            //打印            Console.WriteLine(str);        }    }}

运行结果:

7.  移除项目.

//移除缓存项cacheManager.Remove("MyDataReader");

接下来还要写缓存应用程序模块的高级应用,请大家关注,如有错误的地方也希望大家指出.

黄聪:Microsoft Enterprise Library 5.0 系列教程(一) : Caching Application Block (初级)的更多相关文章

  1. 黄聪:Microsoft Enterprise Library 5.0 系列教程(三) Validation Application Block (初级)

    原文:黄聪:Microsoft Enterprise Library 5.0 系列教程(三) Validation Application Block (初级) 企业库提供了一个很强大的验证应用程序模 ...

  2. 黄聪:Microsoft Enterprise Library 5.0 系列教程(二) Cryptography Application Block (初级)

    原文:黄聪:Microsoft Enterprise Library 5.0 系列教程(二) Cryptography Application Block (初级) 企业库加密应用程序模块提供了2种方 ...

  3. 黄聪:Microsoft Enterprise Library 5.0 系列教程(一) Caching Application Block (高级)

    原文:黄聪:Microsoft Enterprise Library 5.0 系列教程(一) Caching Application Block (高级) Caching Application Bl ...

  4. 转:Microsoft Enterprise Library 5.0 系列教程(一) Caching Application Block (高级)

    http://www.360doc.com/content/13/0918/22/15643_315482318.shtml http://www.360doc.com/content/13/0918 ...

  5. 黄聪:Microsoft Enterprise Library 5.0 系列教程(六) Security Application Block

    原文:黄聪:Microsoft Enterprise Library 5.0 系列教程(六) Security Application Block 开发人员经常编写需要安全功能的应用程序.这些应用程序 ...

  6. 黄聪:Microsoft Enterprise Library 5.0 系列教程(四) Logging Application Block

    原文:黄聪:Microsoft Enterprise Library 5.0 系列教程(四) Logging Application Block 企业库日志应用程序模块工作原理图:   从上图我们可以 ...

  7. 黄聪:Microsoft Enterprise Library 5.0 系列教程(三) Validation Application Block (高级)

    原文:黄聪:Microsoft Enterprise Library 5.0 系列教程(三) Validation Application Block (高级) 企业库验证应用程序模块之配置文件模式: ...

  8. 黄聪:Microsoft Enterprise Library 5.0 系列教程(二) Cryptography Application Block (高级)

    原文:黄聪:Microsoft Enterprise Library 5.0 系列教程(二) Cryptography Application Block (高级) 本章介绍的是企业库加密应用程序模块 ...

  9. 黄聪:Microsoft Enterprise Library 5.0 系列教程(十) Configuration Application Block

    原文:黄聪:Microsoft Enterprise Library 5.0 系列教程(十) Configuration Application Block 到目前为止,我们使用的模块都是在同一个配置 ...

随机推荐

  1. javascript (九)注释

    单行注释,采用双斜杠  // 多行注释,采用 /* */

  2. 七个你无法忽视的Git使用技巧(转)

    与其他技术相比,Git应该拯救了更多开发人员的饭碗.只要你经常使用Git保存自己的工作,你就一直有机会可以将代码退回到之前的状态,因此就可以挽回那些你深夜里迷迷糊糊犯下的错误. 尽管这么说,Git的命 ...

  3. c++ anonymous namespace -- 匿名空间

    c++ anonymous namespace -- 匿名空间 匿名空间,匿名类,匿名联合体,匿名结构体.   匿名空间   #include <stdio.h> namespace A ...

  4. JQuery选择器操作

    !DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"><head runat="se ...

  5. 在防火墙的例外中注册程序(Windows7和XP),改写注册表

    在写程序的时候,经常遇到被防火墙拦截的情况,尤其是一些网络程序,不管是对外访问还是外部连接,都会被拦截. 在大多情况下,Windows会静默拦截外部对内的连接访问,而内部对外的访问会提示用户信息. 现 ...

  6. C++著名类库和C++标准库介绍

    C++著名类库 1.C++各大有名库的介绍——C++标准库 2.C++各大有名库的介绍——准标准库Boost 3.C++各大有名库的介绍——GUI 4.C++各大有名库的介绍——网络通信 5.C++各 ...

  7. [初探iOS开发]storyboard的使用

    storyboard的目的是为了方便的设计程序view之间的关系,使得程序员把精力都放到核心业务逻辑之上.

  8. Enthought科学计算,数据分析

    Enthought Canopy: Easy Python Deployment Plus Integrated Analysis Environment for Scientific Computi ...

  9. PyMOTW: heapq¶

    PyMOTW: heapq — PyMOTW Document v1.6 documentation PyMOTW: heapq¶ 模块: heapq 目的: 就地堆排序算法 python版本:New ...

  10. SilkTest高级进阶系列9 – 异步执行命令

    我们常常会使用sys_execute函数执行一些外部的程序或者命令来做一些事情,但是由于sys_execute是一个同步的函数,它会等待执行的命令完成后才会返回.在大多数情况下,这个函数足够用了. 但 ...