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

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

1. Null backing store 存储策略   : 默认的存储策略,存储的数据只存储在内存的缓存中,并不持久保存, 它可用于所有支持的应用类型.适合于保存一些临时的数据,或者用于保存当你重启程序时不想要保存的一些数据.
2. Isolated storage 存储策略
     : 隔离存储策略适用于以下情况:
      1.需要持久性的保存数据,访问用户较少.
      2.没有数据库设备.
      3.不想使用数据库这类开销较大的资源.
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    {        staticvoid Main(string[] args)        {            //创建CacheManager            CacheManager cacheManager = (CacheManager)CacheFactory.GetCacheManager();			 //添加缓存项            cacheManager.Add("MyDataReader", "123");			 //获取缓存项string str = (String)cacheManager.GetData("MyDataReader");			 //打印            Console.WriteLine(str);        }    }}

运行结果:

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

  1. 转: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 ...

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

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

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

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

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

    原文:黄聪:Microsoft Enterprise Library 5.0 系列教程(一) : Caching Application Block (初级) 本篇文章具体官方解释请参照以下链接: h ...

  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 (初级) 企业库加密应用程序模块提供了2种方 ...

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

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

随机推荐

  1. HDOJ 2442 -bricks 状态压缩DP 一直TLE.打表过的..

    有5个砖块..加上一个空着不放..那么有6种状态..所以很明显的可以用6进制的状态DP... 不过这么做..我觉得我已经能优化的都优化了...还是超时..一看数据范围是100*6..打表先AC了.. ...

  2. 移动无边框窗体(设置标志位更流畅,或者发送WM_SYSCOMMAND和SC_MOVE + HTCAPTION消息)

    移动无边框窗体的代码网上很多,其原理都是一样的,但是是有问题的,我这里只是对其修正一下 网上的代码仅仅实现了两个事件 void EditDialog::mousePressEvent(QMouseEv ...

  3. gcc manual

    $ gcc --helpUsage: gcc [options] file...Options:  -pass-exit-codes         Exit with highest error c ...

  4. Cloud Foundry中通用service的集成

    目前,CloudFoundry已经集成了很多第三方的中间件服务,并且提供了用户添加自定义服务的接口.随着Cloud Foundry的发展,开发者势必会将更多的服务集成进Cloud Foundry,以供 ...

  5. vc笔记六

    通知消息(Notification message)是指这样一种消息,一个窗口内的子控件发生了一些 事情,需要通 知父窗口.通知消息只适用于标准的窗口控件如按钮.列表框.组合框.编辑框,以及 Wind ...

  6. sqlplus登录、连接命令

    经常使用: sqlplus username/password  如:普通用户登录  sqlplus scott/tiger sqlplus username/password@net_service ...

  7. POJ 2318 TOYS(计算几何)

    跨产品的利用率推断点线段向左或向右,然后你可以2分钟 代码: #include <cstdio> #include <cstring> #include <algorit ...

  8. JAVA进阶----主线程等待子线程各种方案比较(转)

    创建线程以及管理线程池基本理解 参考原文链接:http://www.oschina.net/question/12_11255?sort=time 一.创建一个简单的java线程 在 Java 语言中 ...

  9. UVA1450-Airport

    题目链接 题意:有一个飞机场.有两条待飞跑到w和e.一条起飞跑道.每一时刻仅仅能起飞一架飞机,然后有w[i]和e[i]架飞机进入w和e跑道.飞机编号从0開始,问说怎样安排起飞能够使得飞机编号的最大值最 ...

  10. 【STL】关联容器 — hash_set

    容器hash_set是以hash table为底层机制的,差点儿所有的操作都是转调用hash table提供的接口.因为插入无法存储同样的键值,所以hash_set的插入操作所有都使用hash tab ...