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

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 " );
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值