缓存的加载策略--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);
  }
}

相关文章:

  • 2021-12-03
  • 2022-12-23
  • 2021-07-09
  • 2021-08-14
  • 2021-09-19
  • 2021-12-20
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2021-10-25
  • 2021-10-31
  • 2022-01-30
  • 2021-08-10
  • 2022-03-04
  • 2021-09-05
相关资源
相似解决方案