Caching Application Block 深入理解

 Caching Application Block定义的两种缓存类型

它们分别是 :

1. 内存驻留型缓存

2. 磁盘驻留型缓存

 

顾名思义,这两种类型的缓存是以存贮位置来命名的,功能上则以是否能将缓存数据持久化来区别使用。

 

在Caching Application Block中,

具体提供以下四种保存缓存数据的途径,分别是:

 

1. 内存存储(默认)

2. 独立存储(Isolated Storage)

3. 数据库存储(DataBase Cache Storage)

4. 自定义存储(Custom Cache Storage)。

要解决负载均衡时缓存数据保存到哪里的问题,首先我们先详细了解一下这些上面的这四种缓存途径。

 

 

1、  内存存储:内存存储缓存是以上四种方式中唯一的内存驻留型缓存,也是我们开发中最常用到的一种途径,其响应速度快的优势是其它方式无法匹敌的,但单一得采用这种方式的话会有如下弊端:1、缓存数据不能持久化,服务器重起后缓存数据会全部丢失。2、服务器采用负载均衡时采用内存缓存的话,一定要保证多台服务器间的内存缓存状态同步,但这样做会对IO造成较大压力,容易造成系统瓶颈,故,从系统性能和开发成本的角度讲,负载均衡的环境下不易采用内存缓存。

 

2 、独立缓存(Isolated Storage):Isolated Storage是缓存数据持久化的一种选择方式,它是磁盘驻留型缓存,如果您足够细心的话会在每一台机器上找到一个IsolatedStorage文件夹;采用独立缓存的话,我们的缓存信息就会以二进制文件的形式就保存在这个文件夹中

Caching Application Block没有为我们提供向指定机器读、写独立缓存的功能,因此,这种方式只适合需要缓存持久化和大数据量缓存的场合,并不适用于负载均衡的环境。

 

 

3、数据库存储(DataBase Cache Storage):如若想采用数据库存储缓存信息,首先第一步要建立缓存数据库Caching,在安装Enterprise Library后并未默认安装此数据库,若想安装,我们首先找到Enterprise Library的安装文件夹,会发现一个建立数据库的CreateCachingDb.cmd文件,执行该文件后您就会在您的SQL-Server上新建一个名为Caching的数据库,该文件在2.0版中详细地址为:安装盘符:/Program Files/Microsoft Enterprise Library January 2006/src/Caching/Database/Scripts,在Caching数据库中只有一个CacheData表,这个表就保存了我们所读写得缓存信息。

 

4. 自定义缓存存储

 

 

 

 

缓存的使用方式:

 

ICacheManager cache = CacheFactory.GetCacheManager("Loading Scenario Cache Manager");

 

注意:这里的“Loading Scenario Cache Manager”是在缓存配置中配置的字符

 

添加缓存有两个重载版本

void Add(string key, object value);

void Add(string key, object value, CacheItemPriority scavengingPriority, ICacheItemRefreshAction refreshAction, params ICacheItemExpiration[] expirations);

 

如果使用第一个方法的时候,将采用,配置文件中的配置。

 

如果采用第二个方法的话:

 

方法的参数是:

 

1、缓存的主键
2、缓存的对象
3、缓存的优先级,如果缓存数量溢出,则系统自动清除优先级低的;
4、实现 ICacheItemRefreshAction  接口的类,用于缓存项目被卸载时候,接受消息通知用。如果你想这时候更新缓存项,则需要接受并处理这个事件。

5、缓存项过期依赖项。系统默认提供了以下几种过期方式:

 

5.1  绝对时间缓存过期

示例代码如下,AbsoluteTime 的构造函数参数为 DateTime 类型参数:
primitivesCache.Add(.......,new AbsoluteTime(this.AbsoluteTime));

 

5.2 指定时间表达式缓存过期,

比如,你可能需要定义每周六晚上9点半过期,这时候,你就需要这种格式了。
primitivesCache.Add(.......,new ExtendedFormatTime("0 0 * * *")

 

这里的具体表达式详细,如下:

 

原文:

 

/// <summary>
    /// Represents the extended format for the cache.
    /// </summary>   
    /// <remarks>
    /// Extended format syntax : <br/><br/>
    ///
    /// Minute       - 0-59 <br/>
    /// Hour         - 0-23 <br/>
    /// Day of month - 1-31 <br/>
    /// Month        - 1-12 <br/>
    /// Day of week  - 0-6 (Sunday is 0) <br/>
    /// Wildcards    - * means run every <br/>
    /// Examples: <br/>
    /// * * * * *    - expires every minute<br/>
    /// 5 * * * *    - expire 5th minute of every hour <br/>
    /// * 21 * * *   - expire every minute of the 21st hour of every day <br/>
    /// 31 15 * * *  - expire 3:31 PM every day <br/>
    /// 7 4 * * 6    - expire Saturday 4:07 AM <br/>
    /// 15 21 4 7 *  - expire 9:15 PM on 4 July <br/>
    /// Therefore 6 6 6 6 1 means:
    /// • have we crossed/entered the 6th minute AND
    /// • have we crossed/entered the 6th hour AND
    /// • have we crossed/entered the 6th day AND
    /// • have we crossed/entered the 6th month AND
    /// • have we crossed/entered A MONDAY?
    ///
    /// Therefore these cases should exhibit these behaviors:
    ///
    /// getTime = DateTime.Parse( "02/20/2003 04:06:55 AM" );
    /// nowTime = DateTime.Parse( "06/07/2003 07:07:00 AM" );
    /// isExpired = ExtendedFormatHelper.IsExtendedExpired( "6 6 6 6 1", getTime, nowTime );
    /// TRUE, ALL CROSSED/ENTERED
    ///   
    /// getTime = DateTime.Parse( "02/20/2003 04:06:55 AM" );
    /// nowTime = DateTime.Parse( "06/07/2003 07:07:00 AM" );
    /// isExpired = ExtendedFormatHelper.IsExtendedExpired( "6 6 6 6 5", getTime, nowTime );
    /// TRUE
    ///   
    /// getTime = DateTime.Parse( "02/20/2003 04:06:55 AM" );
    /// nowTime = DateTime.Parse( "06/06/2003 06:06:00 AM" );
    /// isExpired = ExtendedFormatHelper.IsExtendedExpired( "6 6 6 6 *", getTime, nowTime );
    /// TRUE
    /// 
    ///   
    /// getTime = DateTime.Parse( "06/05/2003 04:06:55 AM" );
    /// nowTime = DateTime.Parse( "06/06/2003 06:06:00 AM" );
    /// isExpired = ExtendedFormatHelper.IsExtendedExpired( "6 6 6 6 5", getTime, nowTime );
    /// TRUE
    ///      
    /// getTime = DateTime.Parse( "06/05/2003 04:06:55 AM" );
    /// nowTime = DateTime.Parse( "06/06/2005 05:06:00 AM" );
    /// isExpired = ExtendedFormatHelper.IsExtendedExpired( "6 6 6 6 1", getTime, nowTime );
    /// TRUE
    ///      
    /// getTime = DateTime.Parse( "06/05/2003 04:06:55 AM" );
    /// nowTime = DateTime.Parse( "06/06/2003 05:06:00 AM" );
    /// isExpired = ExtendedFormatHelper.IsExtendedExpired( "6 6 6 6 1", getTime, nowTime );
    /// FALSE:  we did not cross 6th hour, nor did we cross Monday
    ///      
    /// getTime = DateTime.Parse( "06/05/2003 04:06:55 AM" );
    /// nowTime = DateTime.Parse( "06/06/2003 06:06:00 AM" );
    /// isExpired = ExtendedFormatHelper.IsExtendedExpired( "6 6 6 6 5", getTime, nowTime );
    /// TRUE, we cross/enter Friday
    ///
    ///
    /// getTime = DateTime.Parse( "06/05/2003 04:06:55 AM" );
    /// nowTime = DateTime.Parse( "06/06/2003 06:06:00 AM" );
    /// isExpired = ExtendedFormatHelper.IsExtendedExpired( "6 6 6 6 1", getTime, nowTime );
    /// FALSE:  we don’t cross Monday but all other conditions satisfied
    /// </remarks>

 

 

 

翻译文:

 

代表缓存扩展格式。

 

拓展格式语法:

Minute                     -      0-59

Hour                        -      0-23

Day of month          -      1-31

Month                      -     1-12

Day of week            -      0-6 (Sunday is 0)

通配符  * 是没运行的时候

Examples:

 

 * * * * *  每分钟过期

5 * * * *   每小时5分钟

* 21 * * *  每一小时到期,每天21分钟

 

 

.......

 

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

 

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

 

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

 

5.6自定义过期机制

 

需要实现接口Microsoft.Practices.EnterpriseLibrary.Caching.ICacheItemExpiration

 

 

 

不同的缓存方式,都是通过配置文件来实现,

系统默认提供了三种备份缓存存储方式
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 

 

 

 

其他更深入的可以参看这个

 

 

 

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值