现在的位置: 首页 > 综合 > 正文

Microsoft Enterprise Library 5.0 系列(一) Caching Application Block (高级)

2014年02月14日 ⁄ 综合 ⁄ 共 5147字 ⁄ 字号 评论关闭

Caching Application Block 的基本架构如下所示,图中很清楚的写出了Cache Manager可以使用3中方式对数据进行缓存:

1. Null backing store 存储策略   : 默认的存储策略,存储的数据只存储在内存的缓存中,并不持久保存, 它可用于所有支持的应用类型.适合于保存一些临时的数据,或者用于保存当你重启程序时想要恢复的一些数据.

2. Isolated storage 存储策略     : 隔离存储策略适用于以下情况:

  • 需要持久性的保存数据,访问用户较少.
  • 没有数据库设备.
  • 不想使用数据库这类开销较大的资源.

3.   Database Cache storage 存储策略  : 数据库存储策略,该数据库可以运行在使用缓存的或在不同的服务器应用程序相同的服务器,申请数目使用缓存,数据库可以支持只依赖于数据库的存储限制,使用起来比较麻烦,需要自己写一些存储过程.

 

 

 

  默认的Null backing store 存储策略我们已经在上一章使用过了,Database Cache storage没研究透,先暂时不讲,如果有了解的朋友可以留言帮助完善,下面我们来看看Isolated storage 存储策略的实现:

 

1. 运行EntLibConfig.exe,选择Blocks菜单 ,单击 Add CachingSettings .接着点击Backing Stores 右上角的加号按钮,选择Add Backing Stores ,单击 Add IsolatedStorage Cache Store.

 

 

2. 如果要对缓存中的数据进行加密,还可以添加一个加密对象,你可以单击Encryption Providers 区块右上角的加号按钮,选择Add EncryptionProviders ,点击 Add Symmetric Crypto Provider.为了简单起见,我们只做一个简单的密钥加密对象,关于加密对象,我们在之后再详细讲解. 我们点击在弹出的Symmetric Cryptography Providers 区块右上角的加号按钮,选择Add SymmetricCryptography Providers, 点击Add DPAPI Symmetric Crypto Provider. 添加一个简单的密钥加密对象(关于企业库加密模块请点击这里了解):

3.  点击 File 菜单,单击 Save,保存为一个App.config文件,可以先保存到桌面,之后要用到它. 用记事本打开App.config,可以看到如下内容.

代码

<configuration>
<configSections>
<section name="securityCryptographyConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Security.Cryptography.Configuration.CryptographySettings, Microsoft.Practices.EnterpriseLibrary.Security.Cryptography, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" requirePermission="true" />
<section name="cachingConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Caching.Configuration.CacheManagerSettings, Microsoft.Practices.EnterpriseLibrary.Caching, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" requirePermission="true" />
</configSections>
<securityCryptographyConfiguration defaultHashInstance="MD5Cng"
defaultSymmetricCryptoInstance
="DPAPI Symmetric Crypto Provider">
<hashProviders>
<add name="MD5Cng" type="Microsoft.Practices.EnterpriseLibrary.Security.Cryptography.HashAlgorithmProvider, Microsoft.Practices.EnterpriseLibrary.Security.Cryptography, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
algorithmType
="System.Security.Cryptography.MD5Cng, System.Core, Version=3.5.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
saltEnabled
="true" />
</hashProviders>
<symmetricCryptoProviders>
<add type="Microsoft.Practices.EnterpriseLibrary.Security.Cryptography.DpapiSymmetricCryptoProvider, Microsoft.Practices.EnterpriseLibrary.Security.Cryptography, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
scope
="LocalMachine" name="DPAPI Symmetric Crypto Provider" />
</symmetricCryptoProviders>
</securityCryptographyConfiguration>
<cachingConfiguration defaultCacheManager="CacheManager">
<cacheManagers>
<add name="CacheManager" 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>
<add name="Isolated Storage Cache Store" type="Microsoft.Practices.EnterpriseLibrary.Caching.BackingStoreImplementations.IsolatedStorageBackingStore, Microsoft.Practices.EnterpriseLibrary.Caching, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
encryptionProviderName
="" partitionName="Isolated Storage Cache Store" />
<add type="Microsoft.Practices.EnterpriseLibrary.Caching.BackingStoreImplementations.NullBackingStore, Microsoft.Practices.EnterpriseLibrary.Caching, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
name
="NullBackingStore" />
</backingStores>
<encryptionProviders>
<add name="Symmetric Crypto Provider" type="Microsoft.Practices.EnterpriseLibrary.Caching.Cryptography.SymmetricStorageEncryptionProvider, Microsoft.Practices.EnterpriseLibrary.Caching.Cryptography, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
symmetricInstance
="DPAPI Symmetric Crypto Provider" />
</encryptionProviders>
</cachingConfiguration>
</configuration>

 

 

4.  接着可以创建一个应用程序来使用我们配置好的缓存应用程序模块了,在此我创建了一个名为test的控制台应用程序,并将刚才保存的App.config文件拷贝到工程文件夹之下:

 

 

5.  要使用缓存应用程序模块, 需要导入相应的Dll文件,在此我们要导入的是Microsoft.Practices.EnterpriseLibrary.Caching.dll Microsoft.Practices.EnterpriseLibrary.Caching.Cryptography.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);
}
}
}

 

运行结果:

出处:http://www.cnblogs.com/huangcong/archive/2010/05/28/1745909.html

抱歉!评论已关闭.