reactive策略是“按需加載”,在程序初始化階段僅加載必要的數(shù)據(jù)到內(nèi)存緩存起來,其余數(shù)據(jù)只有在需要時才從數(shù)據(jù)庫中調(diào)出再緩存。這種策略比較保守,缺點是在數(shù)據(jù)量比較大且頻繁訪問之初由于要多次頻繁的向backing store獲取數(shù)據(jù),但通常我們使用這種的就是這種策略。
下面是兩種方案的示例代碼比較:
proactive的方式
public List<Product> GetProductList()
{
return Respository<Product>.ResolveAll();
}
public void LoadAllProducts(ICacheManager cache)
{
List<Product>list = GetProductList();
for (int i = 0; i < list.Count; i++)
{
Product newProduct = list[i];
cache.Add(newProduct.ProductID, newProduct);
}
}
reactive的方式
public List<Product> GetProductList()
{
return Respository<Product>.ResolveAll();
}
public Product ReadProductByID(ICacheManager cache, string productID)
{
Product newProduct = (Product)cache.GetData(productID);
// Does our cache already have the requested object?
if (newProduct == null)
{
// The requested object is not cached, so retrieve it from
// the data provider and cache it for further requests.
newProduct = this.dataProvider.GetProductByID(productID);
if (newProduct != null)
{
cache.Add(newProductID, newProduct);
}
}
return newProduct;
}
緩存隔離:Partitioned Caches
一個caching block中CacheManager不能被多個進程或是一個進程的不同應用程序域所共享。但多個進程或是同一進程的多個應用程序域共享一個緩存的需求還是很必要的。關鍵在于如何設計合理。
三種情形:
A.Partitioned Caches:每個進程/同一進程的不同應用程序域使用不同的backing storage。這是最簡單的情況,也不需要我們進行額外的同步處理。每個進程/應用程序域你用你的我用我的大家互不干涉。但這無法是多個進程共享一個backing storage。比如isolatedstorage作為backing storage,如果是不同用戶會自動分配不同的隔離存儲空間,對于同一用戶的話只需將partition配置為不同值即可。

B.Shared Partition:n個進程中只有1個進程能夠修改backing storage,所有進程都能夠讀取backing storage的數(shù)據(jù)。這種情況是最理想的,但需要邏輯上的支持。
C.Single Writer:n個進程都能夠修改和讀取backing storage,這是最糟糕的情況,很容易產(chǎn)生錯亂,會導致不同步的現(xiàn)象。