缓存的加载策略--Proactive 和Reactive
proactive的策略就是一开始就将所有backing store中的数据加载到进程内存中,这样做的好处是在数据量相对不大的时候会显得很有效率,无需频繁的访问backing store调出数据,并且也不用再代码中判断缓存中是否缓存有数据,是否要从backing store中加载。
reactive策略是“按需加载”,在程序初始化阶段仅加载必要的数据到内存缓存起来,其余数据只有在需要时才从数据库中调出再缓存。这种策略比较保守,缺点是在数据量比较大且频繁访问之初由于要多次频繁的向backing store获取数据,但通常我们使用这种的就是这种策略。
下面是两种方案的示例代码比较:
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);
}
}
{
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);
}
}