黄聪:Microsoft Enterprise Library 5.0 系列教程(一) : Caching Application Block (初级)
原文:黄聪: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,可以看到如下内容.
<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 (初级)的更多相关文章
- 黄聪:Microsoft Enterprise Library 5.0 系列教程(三) Validation Application Block (初级)
原文:黄聪:Microsoft Enterprise Library 5.0 系列教程(三) Validation Application Block (初级) 企业库提供了一个很强大的验证应用程序模 ...
- 黄聪:Microsoft Enterprise Library 5.0 系列教程(二) Cryptography Application Block (初级)
原文:黄聪:Microsoft Enterprise Library 5.0 系列教程(二) Cryptography Application Block (初级) 企业库加密应用程序模块提供了2种方 ...
- 黄聪:Microsoft Enterprise Library 5.0 系列教程(一) Caching Application Block (高级)
原文:黄聪:Microsoft Enterprise Library 5.0 系列教程(一) Caching Application Block (高级) Caching Application Bl ...
- 转: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 ...
- 黄聪:Microsoft Enterprise Library 5.0 系列教程(六) Security Application Block
原文:黄聪:Microsoft Enterprise Library 5.0 系列教程(六) Security Application Block 开发人员经常编写需要安全功能的应用程序.这些应用程序 ...
- 黄聪:Microsoft Enterprise Library 5.0 系列教程(四) Logging Application Block
原文:黄聪:Microsoft Enterprise Library 5.0 系列教程(四) Logging Application Block 企业库日志应用程序模块工作原理图: 从上图我们可以 ...
- 黄聪:Microsoft Enterprise Library 5.0 系列教程(三) Validation Application Block (高级)
原文:黄聪:Microsoft Enterprise Library 5.0 系列教程(三) Validation Application Block (高级) 企业库验证应用程序模块之配置文件模式: ...
- 黄聪:Microsoft Enterprise Library 5.0 系列教程(二) Cryptography Application Block (高级)
原文:黄聪:Microsoft Enterprise Library 5.0 系列教程(二) Cryptography Application Block (高级) 本章介绍的是企业库加密应用程序模块 ...
- 黄聪:Microsoft Enterprise Library 5.0 系列教程(十) Configuration Application Block
原文:黄聪:Microsoft Enterprise Library 5.0 系列教程(十) Configuration Application Block 到目前为止,我们使用的模块都是在同一个配置 ...
随机推荐
- Android颜色转换工具类ColorUtil
项目中需要根据ScrollView的滚动距离来动态设置Topbar的背景透明度,网上有类似的开源库FadingActionBar,使用的是ActionBar做的.而我的项目中并没有使用ActionBa ...
- [黑马程序员] I/O
---------------------- ASP.Net+Android+IO开发..Net培训.期待与您交流! ---------------------- 0. IO流概述: Java对数据的 ...
- flexbox语法(摘抄)
flex 容器上的属性: flex-direction: row | row-reverse | column | column-reverse (项目的排列方向) flex-wrap : nowra ...
- Firemonkey 自定义Button的Style
这篇文章模仿HTML中基于CSS的Button,通过Style实现自定义样式的Button. 前言 主要模仿的CSS代码如下: CSS Code 123456789101112131415161718 ...
- VC实现文件拖拽OnDropFiles
文章转自http://blog.csdn.net/zamaolangzi/article/details/5645284 使用过QQ的人都知道,只要把文件拖拽到消息框中就可以传送文件了.那么这种功能是 ...
- 页面提交错误,页面间参数传递java.lang.NumberFormatException: null
多次出现这样的错误,在点击一个按钮触发提交整个页面的事件时,总是报错,不止一次出现这样的错误了. 出现这种问题的分析: 1 我们从这个问题的本身来看,java.lang.NumberFormatExc ...
- 【PHP SDK for OpenStack/Rackspace APIs】身份验证
在你使用php-opencloud之前必须先取得云服务提供商的身份验证.这是整个过程中最简单也是最让人沮丧的部分. 说它最简单是因为你只需要两部分信息: 云服务提供商的身份验证端点 用来身份验证的证书 ...
- Swift - 加速传感器(CoreMotion)的用法,小球加速运动并反弹样例
1,加速传感器可以监听到x,y,z三个方向的加速度,使用步骤如下: (1)实例化CMMotionManager类 (2)向CMMotionManager的accelerometerUpdateInte ...
- Swift - 协议(protocol)
1,Swift中协议类似于别的语言里的接口,协议里只做方法的声明,包括方法名.返回值.参数等信息,而没有具体的方法实现. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 ...
- Linux下select, poll和epoll IO模型的详解
http://blog.csdn.net/tianmohust/article/details/6677985 一).Epoll 介绍 Epoll 可是当前在 Linux 下开发大规模并发网络程序的热 ...