原文:黄聪: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. poj1011Sticks

    传说中的poj必做50题之中的一个-- 这是个传说中的搜索, 一開始以为, 仅仅要棒子加起来等于如果的原始长度, 那么这几根选择的棒子就不用管了, 结果卡在第一个例子-- 看了一下,发现, 代码把1, ...

  2. 《转》OpenStack Live Migration

    This post is based assumption that KVM as hypervisor, and Openstack is running in Grizzly on top of ...

  3. Mac OS X将CSV格式转换为Excel文档格式,Excel转CSV中文乱码问题

    一:在Mac上假设你使用Excel打开windows导出的CSV格式文档.你会发现表格中全部的的内容都显示在A列. 那么,怎样恢复正常呢,你能够将CSV格式的文档导入到Excel文档中,这样就正常显示 ...

  4. 实现 select中指定option选中触发事件

    我们在用到下拉列表框select时,需要对选中的<option>选项触发事件,其实<option>本身没有触发事件方法,我们只有在select里的onchange方法里触发. ...

  5. Codeforces Round #309 (Div. 2) C

    题意: 就是给出总共同拥有k种颜色.每种颜色有ki种,排列必须满足第i+1种的最后一种颜色必须在第i种最后一种颜色的后面,其它颜色任意.总共同拥有多少种排列点的方法. 分析: 如果d[i]表示前i种的 ...

  6. ThinkPHP连接数据库出现的错误:Undefined class constant 'MYSQL_ATTR_INIT_COMMAND'

    最近看了看ThinkPHP.在连接mysql数据库时出现了错误:Undefined class constant 'MYSQL_ATTR_INIT_COMMAND'.意思就是没有PDO(PHP数据对象 ...

  7. C++ STL copy函数效率分析

    在C++编程中,经常会配到数据的拷贝,如数组之间元素的拷贝,一般的人可能都会用for循环逐个元素进行拷贝,在数据量不大的情况下还可以,如果数据量比较大,那么效率会比较地下.而STL中就提供了一个专门用 ...

  8. 使用Runtime.getRuntime().exec()方法的几个陷阱 (转)

    Process 子类的一个实例,该实例可用来控制进程并获得相关信息.Process 类提供了执行从进程输入.执行输出到进程.等待进程完成.检查进程的退出状态以及销毁(杀掉)进程的方法. 创建进程的方法 ...

  9. &lt;xliff:g&gt;标签

    摘要: 这是Android4.3Mms源代码中的strings.xml的一段代码: <!--Settings item desciption for integer auto-delete sm ...

  10. python语言学习5——输入和输出

    输出 用print()在括号中加上字符串,就可以向屏幕上输出指定的文字. 注意点: 字符串用的是单引号 碰到逗号输出时就会输出一个空格 输入 python提供了一个input(),可以让用户输入一个字 ...