Welcome

首页 / 软件开发 / .NET编程技术 / Enterprise Library Step By Step系列(十) 缓冲应用程序块 - 进阶篇

Enterprise Library Step By Step系列(十) 缓冲应用程序块 - 进阶篇2011-01-28 cnblogs terrylee一.基于时间的过期策略

基于时间的过期策略,支持两种相对时间和绝对时间。

1.绝对时间(Absolute):

允许您定义一个缓冲项的生命周期,我们可以指定一个单一的时间作为过期,或者通过表达式来设置。

指定单一的时间作为过期:

1/**////读取数据
2 Database db = DatabaseFactory.CreateDatabase("Database Instance");
3 DataSet ds = db.ExecuteDataSet(CommandType.Text,"Select * from Products");
4
5 /**////创建CacheManager
6 IsolatedCacheManager = CacheFactory.GetCacheManager("Isolated Cache Manager");
7
8 /**////创建一个单一的时间
9 DateTime refreshTime = new DateTime(2005, 11, 12, 12, 51, 30);
10
11 /**////指定为绝对过期时间
12 AbsoluteTime expireTime = new AbsoluteTime(refreshTime);
13
14 /**////添加缓冲项,优先级为Normal
15 IsolatedCacheManager.Add("MyDataSet",ds, CacheItemPriority.Normal, null,expireTime);

用表达式来设置:

表达式的格式:<Minute> <Hour> <Day of month> <Month> <Day of week>

例子:

“* * * * *” 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

“15 21 4 7 *” expire 9:15 PM on 4 July

1/**////读取数据
2 Database db = DatabaseFactory.CreateDatabase("Database Instance");
3 DataSet ds = db.ExecuteDataSet(CommandType.Text,"Select * from Products");
4
5 /**////创建CacheManager
6 IsolatedCacheManager = CacheFactory.GetCacheManager("Isolated Cache Manager");
7
8 /**////创建基于表达式
9 ExtendedFormatTime expireTime = new ExtendedFormatTime("0 0 * * 6");
10
11 /**////添加缓冲项,优先级为Normal,过期时间
12 IsolatedCacheManager.Add("Key1", "Cache Item1", CacheItemPriority.Normal,null, expireTime);

2.变化的时间:

允许您定义针对条目的被调用的两次之间的间隔,定义条目的生命周期

1/**////读取数据
2 Database db = DatabaseFactory.CreateDatabase("Database Instance");
3 DataSet ds = db.ExecuteDataSet(CommandType.Text,"Select * from Products");
4
5 /**////创建CacheManager
6 IsolatedCacheManager = CacheFactory.GetCacheManager("Isolated Cache Manager");
7
8 /**////访问5分钟后过期,变化的时间
9 TimeSpan refreshTime = new TimeSpan(0, 5, 0);
10 SlidingTime expireTime = new SlidingTime(refreshTime);
11
12 /**////添加缓冲项
13 IsolatedCacheManager.Add("Key1", "Cache Item1", CacheItemPriority.Normal,null, expireTime);