原文:黄聪: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. Oracle安装配置流程

    Oracle安装流程 第一次自己动手安装oracle,之前对oracle安装配置一窍不通,最后最终弄好.总结下. 1.  安装oracle10gserver端 2.  安装oracle10gclien ...

  2. CF 258B Little Elephant and Elections [dp+组合]

    给出1,2,3...m 任取7个互不同样的数a1,a2,a3,a4,a5,a6,a7 一个数的幸运度是数位上4或7的个数 比方244.470幸运度是2. 44434,7276727.4747,7474 ...

  3. 在iPhoneApp中加载PDF

    原文: http://ios.biomsoft.com/2012/02/17/load-a-pdf-file-in-the-iphone-app-smoothly/ 本节将学习如何从服务器加载 pdf ...

  4. [Android学习笔记]view的layout过程学习

    View从创建到显示到屏幕需要经历几个过程: measure -> layout -> draw measure过程:计算view所占屏幕大小layout过程:设置view在屏幕的位置dr ...

  5. 登录RMAN 报告ORA-12162:TNS:net service name is incorrectly specified错

    登录RMAN 报告ORA-12162:TNS:net service name is incorrectly specified错 [oracle@localhost admin]$ date Tue ...

  6. JavaScript2谁刚开始学习应该知道4最佳实践文章(翻译)

    原版的:24 JavaScript Best Practices for Beginners (注:阅读原文的时候没有注意公布日期,觉得不错就翻译了,翻译到JSON.parse那一节觉得有点不正确路才 ...

  7. VSTO之旅系列(二):创建Excel解决方案

    原文:VSTO之旅系列(二):创建Excel解决方案 本专题概要 引言 创建VSTO项目 Excel对象模型 创建Excel外接程序 创建Excel文档级自定义项 小结 一.引言 也许很多朋友都没有听 ...

  8. c++中volatile详解

    1. 为什么用volatile? C/C++ 中的 volatile 关键字和 const 对应,用来修饰变量,通常用于建立语言级别的 memory barrier.这是 BS 在 "The ...

  9. Perl中检测标准输入使用的字符集

    #Perl中检测标准输入使用的字符集 sub locale_encode{ my $lang = $ENV{'LANG'}; my $dot_pos = rindex($lang, '.'); my ...

  10. 使用cacti监控服务器

    一.cacti安装 cacti需要lamp环境,因此需要首先编译安装lamp环境,安装完成后,下载cacti的zip包,解压到/www/htdocs目录下,创建cacti需要的数据库. 完成上述后,在 ...