config the Cache in the file of Web.Config

来源:互联网 发布:青柠娇喘恶搞软件 编辑:程序博客网 时间:2024/05/02 14:40

How to config the cache? Some people alway ask this issue.

now, please see below code and explain.
  1. <configuration>
  2.   <configSections>
  3.     <section name="cachingConfiguration" type="Microsoft.Practices.EnterpriseLibrary.Caching.Configuration.CacheManagerSettings, Microsoft.Practices.EnterpriseLibrary.Caching, Version=2.0.0.0, Culture=neutral, PublicKeyToken=null" />
  4.   </configSections>
  5.   <cachingConfiguration defaultCacheManager="Cache Manager">
  6.     <cacheManagers>
  7.       <add expirationPollFrequencyInSeconds="60" maximumElementsInCacheBeforeScavenging="1000"
  8.         numberToRemoveWhenScavenging="10" backingStoreName="inMemory"
  9.         name="Cache Manager" />
  10.     </cacheManagers>
  11.     <backingStores>
  12.       <add encryptionProviderName="" type="Microsoft.Practices.EnterpriseLibrary.Caching.BackingStoreImplementations.NullBackingStore, Microsoft.Practices.EnterpriseLibrary.Caching, Version=2.0.0.0, Culture=neutral, PublicKeyToken=null"
  13.         name="inMemory" />
  14.     </backingStores>
  15.   </cachingConfiguration>
  16. </configuration>

在配置文件中,我可以可以看到一个缓存Manager 具有以下配置信息:
1、expirationPollFrequencyInSeconds  多少秒进行一次过期检查,缺省值60秒
2、maximumElementsInCacheBeforeScavenging 最多可以缓存对象数,超过这个个数,则触发清除事件,缺省值 1000;
3、numberToRemoveWhenScavenging  每次清除缓存项时候,按照优先级,清除掉优先级低的多少项。
4、backingStoreName  备份缓存的数据(辅助存储器 Backing Storage),备份保存在那里?默认备份不缓存。
系统默认提供了三种备份缓存存储方式
4.1、不缓存 Microsoft.Practices.EnterpriseLibrary.Caching.BackingStoreImplementations.NullBackingStore

4.2、缓存在数据库中  Microsoft.Practices.EnterpriseLibrary.Caching.Database.DataBackingStore
注意,这个类在 microsoft.practices.enterpriselibrary.caching.database.dll 文件中

4.3、缓存在一个隔离新的独立空间 
Microsoft.Practices.EnterpriseLibrary.Caching.BackingStoreImplementations.IsolatedStorageBackingStore 

上面我们看到的配置文件,采用的都是默认的缓存设置。

至于代码部分,则更简单,参看下面实例代码:
4.1
private CacheManager primitivesCache;

primitivesCache = CacheFactory.GetCacheManager();
primitivesCache.Add("1", 234);
primitivesCache.Add("12", "guohongjun");
primitivesCache.Add("33","www.csdn.net",
    CacheItemPriority.Normal,
    null,
    new SlidingTime(TimeSpan.FromMinutes(1)));

object obj1 = primitivesCache.GetData("1");
object obj2 = primitivesCache.GetData("12");
object obj3 = primitivesCache.GetData("33");

primitivesCache.Flush();

其中
CacheManager.Add (String, Object, CacheItemPriority, ICacheItemRefreshAction, ICacheItemExpiration[])

方法,函数的参数依次是:
1、缓存的主键
2、缓存的对象
3、缓存的优先级,如果缓存数量溢出,则系统自动清除优先级低的;
4、实现 ICacheItemRefreshAction  接口的类,用于缓存项目被卸载时候,接受消息通知用。如果你想这时候更新缓存项,则需要接受并处理这个事件。
5、缓存项过期依赖项。系统默认提供了以下几种过期方式:

5.1  指定时间点过期
示例代码如下,AbsoluteTime 的构造函数参数为 DateTime 类型参数:
primitivesCache.Add(.......,new AbsoluteTime(this.AbsoluteTime));

5.2 比如,你可能需要定义每周六晚上9点半过期,这时候,你就需要这种格式了。
这种格式具体的写法,请查看源代码  Expirations/ExtendedFormat.cs
这里的注释中有详细描述
primitivesCache.Add(.......,new ExtendedFormatTime("0 0 * * *")

5.3 文件被修改,则过期
primitivesCache.Add(.......,new FileDependency("DependencyFile.txt")

5.4 指定多长时间段之后
primitivesCache.Add(.......,new SlidingTime(TimeSpan.FromMinutes(1)));

5.5 当然还有不过期
primitivesCache.Add(.......,new NeverExpired());

当然你也可以自己写自己的过期机制。

参考:http://rickie.cnblogs.com/archive/2005/02/17/105013.aspx

原创粉丝点击