一.基于时间的过期策略
允许您定义一个缓冲项的生命周期,我们可以指定一个单一的时间作为过期,或者通过表达式来设置。
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); 允许您定义针对条目的被调用的两次之间的间隔,定义条目的生命周期
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); 1
///依赖于文件DependencyFile.txt2
///当文件改变时过期 3
FileDependency expireNotice = new FileDependency( " DependencyFile.txt " ); 4
5
///添加缓冲项 6
myCacheManager.Add( " FileKey " , " String: Test Cache Item Dependency " , CacheItemPriority.Normal, null , expireNotice); 可以创建自己的过期类,需要实现 ICacheItemExpiration 接口
• Caching Application Block 提供了项目移除的提醒,并在一下情况下被激活
• 需要实现 ICacheItemRefreshAction接口
• 一个类实现了 ICacheItemRefreshAction 接口,同时如果需要后端存储时,还必须被标识为 Serializable (Especially for persistent backing store)
1
[Serializable] 2
public class ProductCacheRefreshAction : ICacheItemRefreshAction 3
{ 4
public void Refresh(string key, object expiredValue, CacheItemRemovedReason removalReason)5
{ 6
//……7
}8
} 1 .缓冲的前期装载( Proactive loading ):应用启动时装载
• 为了提升启动性能而进行的——基于不同线程的装载,有造成了应用结构的复杂性
( 3 )何时使用主动装载 (Proactive caching)
在一些情况下中,他们自己有更新周期。当装载到缓冲将导致状态过期的出现
1
CacheManager productsCache = CacheManager.GetCacheManager(); 2
/// 获取数据 3
ArrayList list = dataProvider.GetProductList(); 4
5
/// 添加缓冲项for (int i = 0; i < list.Count; i++) 6
{ 7
Product product = (Product) list[i]; 8
productsCache.Add( product.ProductID, product ); 9
} 10
2 .缓冲的被动装载( Reactive loading ):按需装载
希望利用缓冲机制,但是在应用程序的初始化时,希望不使用缓冲,而是根据用户输入等条件,进行缓冲
1
CacheManager productsCache = CacheManager.GetCacheManager(); 2
Product product = (Product) productsCache.GetData(productID); 3
if (product == null ) 4
{ 5
/// 6
product = dataProvider.GetProductByID(productID); 7
if (product != null) 8
{ 9
productsCache.Add(productID, product); 10
} 11
} 12
五.刷新缓冲( Explicit flushing ):
使用代码 ——Initiated by application code
全部移除 ——Removes all items from the cache
使用应用程序块的功能 ——Initiated by application block
基于优先级和最后访问的时间 ——Based upon priority and last access time
控制移除的精确度 ——Configuration settings control size of cache and number removed
MaximumElementsLnCacheBeforeScavenging :缓冲中的最大元素数量。。
NumberToRemoveWhenScavenging :一次移除的数量。
1
/// <summary> 2
/// 缓冲的优先级有以下几个值: 3
/// --Low 4
/// --Normal 5
/// --High 6
///--NotRemovable 7
/// </summary> 8
/// <param name="sender"></param> 9
/// <param name="e"></param> 10
private void button2_Click( object sender, System.EventArgs e) 11
{ 12
for(int intCount=0; intCount<8; intCount++)13
{ 14
string strKeyName = "Key"+System.Convert.ToString(intCount);15
16
if (intCount%2 == 0)17
{ 18
myCacheManager.Add(strKeyName, "High", CacheItemPriority.High, null, null);19
}20
else21
{ 22
myCacheManager.Add(strKeyName, "Low", CacheItemPriority.Low, null, null);23
}24
}25
} 本文转自lihuijun51CTO博客,原文链接: http://blog.51cto.com/terrylee/67612
,如需转载请自行联系原作者