Enterprise Library5.0 缓存使用篇

网上有很多资料讲解怎么使用,主要是在是使用时碰到一个很郁闷的错误,一下内容有摘抄的博友的有自己写的,大家将就看吧。

创建 cachingConfiguration 的配置节处理程序时出错: 未能加载文件或程序集“Microsoft.Practices.EnterpriseLibrary.Caching, Version=5.0.414.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35”或它的某一个依赖项。找到的程序集清单定义与程序集引用不匹配。 (异常来自 HRESULT:0x80131040)

这个错误刚开始是以为没有装.net3.5 SP1的事呢,装上也不行,最后发现用EntLibConfig.exe打开他自己生成的config配置文件业报同样的错误,

初步怀疑是生成的config配置文件有问题,在生成一个还是报同样的错误,等生成第三遍的时候在打开没有报错,服务到应用程序目录,一切正常。

简单使用代码:

 1        public Form1()
 2         {
 3             InitializeComponent();
 4         }
 5 
 6         ICacheManager cacheManager;
 7 
 8         private void button1_Click(object sender, EventArgs e)
 9         {
10             AbsoluteTime _ExpireTime = new AbsoluteTime(DateTime.Now.AddSeconds(30));//指定30秒后过期
11 
12             cacheManager.Add("aaa1""aaa1_value", CacheItemPriority.Normal, null, _ExpireTime);//加入缓存
13         }
14 
15         private void Form1_Load(object sender, EventArgs e)
16         {
17             cacheManager = CacheFactory.GetCacheManager("Cache Manager");//实例化ICachemanager
18         }
19 
20         private void button2_Click(object sender, EventArgs e)
21         {
22             this.richTextBox1.Text = cacheManager.GetData("aaa1").ToString();
23         }

下面介绍如何使用Microsoft Enterprise Library 5.0中的缓存应用程序模块.

1.下载安装好MicrosoftEnterprise Library 5.0,然后在运行EntLibConfig.exe

    2.  选择Blocks菜单 ,单击 Add CachingSettings .

        配置属性说明:

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

      <configuration>
        
      <configSections>
          
      <section name="cachingConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Caching.Configuration.CacheManagerSettings, Microsoft.Practices.EnterpriseLibrary.Caching, Version=5.0.414.0, Culture=neutral, PublicKeyToken=null" requirePermission="true" />
        
      </configSections>
        
      <cachingConfiguration defaultCacheManager="Cache Manager">
          
      <cacheManagers>
            
      <add name="Cache Manager" type="Microsoft.Practices.EnterpriseLibrary.Caching.CacheManager, Microsoft.Practices.EnterpriseLibrary.Caching, Version=5.0.414.0, Culture=neutral, PublicKeyToken=null"
                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=null"
                encryptionProviderName
      ="" partitionName="asdfsdf" />
            
      <add type="Microsoft.Practices.EnterpriseLibrary.Caching.BackingStoreImplementations.NullBackingStore, Microsoft.Practices.EnterpriseLibrary.Caching, Version=5.0.414.0, Culture=neutral, PublicKeyToken=null"
                name
      ="NullBackingStore" />
          
      </backingStores>
        
      </cachingConfiguration>
      </configuration>

      EnterpriseLibrary的Cache提供了非常强大的支持,可以设置绝对时间、间隔时间、自定义格式以及文件过期时间来进行相应的更新操作。

              1. 绝对时间过期的缓存:AbsoluteTime

              2. 相对时间过期的缓存:SlidingTime

              3. 自定义格式过期的缓存:ExtendedFormatTime

                 自定义格式为:<Minute> <Hour> <Day of month> <Month> <Day of week>

                 Minute            0-59
                 Hour              0-23
                 Day of month     1-31
                 Month             1-12
                 Day of week       0-6 (Sunday is 0)
                 如:
                 * * * * *    - expires every minute
                 5 * * * *    - expire 5th minute of every hour 
                 * 21 * * *   - expire every minute of the 21st hour of every day
                 31 15 * * *  - expire 3:31 PM every day
                 7 4 * * 6    - expire Saturday 4:07 AM

              4.文件依赖:FileDependency    指定缓存依赖于某一文件
                 FileDependency _fileDep = new FileDependency("R:\\1.txt");//指定缓存依赖于某一文件
                
      写入缓存数据时如果使用了FileDependency方式,最终的效果会让缓存是否过期依赖于某一个具体的文件,只要这个文件没有修改,缓存一直

                 有效,反之如果这个文件被修改过了,则缓存立即过期。

              5.永不过期:NeverExpired

      使用如:

      //绝对时间过期的缓存
       AbsoluteTime _ExpireTime = new AbsoluteTime(DateTime.Now.AddSeconds(30));//指定30秒后过期

      //自定义格式过期的缓存
      ExtendedFormatTime expireTime = new ExtendedFormatTime("41 11 * * *");

              Cache以配置文件的方式供用户进行缓存的轮询过期数据的频率、缓存中数据项的多少、清除数据项的多少以及缓存备份的位置。

       1.    expirationPollFrequencyInSeconds: 设置控制后台调度程序多久检查过期条目的定时器。此属性必须是正整数,且是必要的。
       2.    maximumElementsInCacheBeforeScavenging: 设置在清除开始前可以在缓存中的条目的最大数量。此属性必须是正整数,且是必要的。 
       3.    numberToRemoveWhenScavenging: 设置在清除开始时移除的条目的数量,此属性必须是正整数,且是必要的。 
       4.    backingStoreName: 缓存备份的位置

              值得一提的是,expirationPollFrequencyInSeconds属性是控制后台调度程序多久检查过期条目的配置,单位为秒,如果系统经常需要更新数据则可以将此值设置的小一点;ICacheItemExpiration的时间是以UTC的时间来作为标准时间来比较的,北京时间比UTC早8个小时,比如你需要在每天的十二点半让缓存过期,则必须这样设置ExtendedFormatTime("30 4 * * *"), 关于这个问题我本来不知道,以为是Library的Bug,上网也没有找到相关的例子,最后在看了原程序才知道Library是用的UTC来进行比较的。

      posted @ 2011-07-13 18:03  金江  阅读(1473)  评论(1编辑  收藏  举报