Enterprise Library Step By Step系列(十):緩沖應用程序塊——進階篇
作者:Terrylee
一.基于時間的過期策略
基于時間的過期策略,支持兩種相對時間和絕對時間。
1.絕對時間(Absolute):
允許您定義一個緩沖項的生命周期,我們可以指定一個單一的時間作為過期,或者通過表達式來設置。
指定單一的時間作為過期:
///讀取數據2
Database db = DatabaseFactory.CreateDatabase("Database Instance");3
DataSet ds = db.ExecuteDataSet(CommandType.Text,"Select * from Products");4
5
///創建CacheManager6
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
///添加緩沖項,優先級為Normal15
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
“7 4 * * 6” expire Saturday
“15 21 4 7 *” expire
///讀取數據2
Database db = DatabaseFactory.CreateDatabase("Database Instance");3
DataSet ds = db.ExecuteDataSet(CommandType.Text,"Select * from Products");4
5
///創建CacheManager6
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.變化的時間:
允許您定義針對條目的被調用的兩次之間的間隔,定義條目的生命周期
///讀取數據2
Database db = DatabaseFactory.CreateDatabase("Database Instance");3
DataSet ds = db.ExecuteDataSet(CommandType.Text,"Select * from Products");4
5
///創建CacheManager6
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);二.基于提醒機制的過期策略:
下面以文件依賴為例
///依賴于文件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)
[Serializable]2
public class ProductCacheRefreshAction : ICacheItemRefreshAction3
{4
public void Refresh(string key, object expiredValue, CacheItemRemovedReason removalReason)5
{6
//……7
}8
}四.裝載緩沖:
1.緩沖的前期裝載(Proactive loading):應用啟動時裝載
(1)優點
? 全部裝載后,應用運行性能提升明顯
(2)缺點
? 啟動時間長
? 可能帶來不必要的資源浪費
? 為了提升啟動性能而進行的——基于不同線程的裝載,有造成了應用結構的復雜性
(3)何時使用主動裝載(Proactive caching)
在一些情況下中,他們自己有更新周期。當裝載到緩沖將導致狀態過期的出現
此時,您需要清楚的知道被緩沖的對象的生命周期
您還需要提前知道占用資源的程度
使用不穩定的資源時,盡量多使用主動裝載緩沖
(4)主動裝載實例:
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)優點
? 只有在需要的時候才裝載,對資源的需求小
(2)缺點
? 但是在首次裝載的時候,速度慢
(3)何時使用被動裝載
需要緩沖的對象狀態過多或系統資源不足的情況
資源的可靠性和性能良好,此時被動裝載的又是更明顯
希望利用緩沖機制,但是在應用程序的初始化時,希望不使用緩沖,而是根據用戶輸入等條件,進行緩沖
(4)被動裝載實例:
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):
1.精確刷新:
使用代碼——Initiated by application code
全部移除——Removes all items from the cache
2.自我移除(Scavenging):
使用應用程序塊的功能——Initiated by application block
基于優先級和最后訪問的時間——Based upon priority and last access time
控制移除的精確度——Configuration settings control size of cache and number removed
自我清除的配置:
MaximumElementsLnCacheBeforeScavenging:緩沖中的最大元素數量。。
NumberToRemoveWhenScavenging:一次移除的數量。
為緩沖建立優先級
/// <summary>2
/// 緩沖的優先級有以下幾個值:3
/// --Low4
/// --Normal5
/// --High6
///--NotRemovable7
/// </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
}
Worktile,新一代簡單好用、體驗極致的團隊協同、項目管理工具,讓你和你的團隊隨時隨地一起工作。完全免費,現在就去了解一下吧。
https://worktile.com



浙公網安備 33010602011771號