在asp.net中管理缓存对象

时间:2022-02-02 03:54:15

I have a list of product that I have stored in asp.net cache but I have a problem in refreshing the cache. As per our requirement I want to refresh cache every 15 minutes but I want to know that if in the mean time when the cache is being refreshed if some user ask for the list of product then should he get error or the old list or he have to wait until the cache is refreshed.

我有一个在asp.net缓存中存储的产品列表,但是我在刷新缓存时遇到了问题。按照我们的要求我想每15分钟刷新缓存,但我想知道,如果同时在刷新缓存时如果一些用户要求的产品列表然后他应该得到错误或旧的列表或他不得不等到缓存刷新。

the sample code is below

示例代码如下

public class Product
{
    public int Id{get;set;}
    public string Name{get;set;}
}

we have a function which gives us list of Product in BLL

我们有一个函数,给出了BLL中的产品列表

public List<Product> Products()
{
       //////some code
}

Cache.Insert("Products", Products(), null, DateTime.Now.AddMinutes(15), TimeSpan.Zero);

I want to add one more situation here, Let say I use static object instead of cache object then what will happen and which approach is best if we are on a stand alone server and not on cluster

我想在这里添加一个情况,假设我使用静态对象而不是缓存对象,然后会发生什么,如果我们在独立服务器上而不是在集群上,哪种方法是最好的。

1 个解决方案

#1


3  

Sorry - this might be naive/obvious but just have a facade type class which does

对不起——这可能很幼稚/明显,但是只有facade类型的类可以做到这一点

if(Cache["Products"] == null)
{
        Cache.Insert("Products", Products(), null, DateTime.Now.AddMinutes(15), TimeSpan.Zero);
 }
 return Cache["Products"];

There is also a CacheItemRemoveCallback delegate which you could use to repopulate an expired cache. As an alternative

还有一个CacheItemRemoveCallback委托,您可以使用它来重新填充过期的缓存。作为一种替代方法

ALSO

use the cache object rather than static objects. More efficient apparently (Asp.net - Caching vs Static Variable for storing a Dictionary) and you get all your cache management methods (sliding expiration and so on)

使用缓存对象而不是静态对象。显然更有效(Asp.net -缓存vs静态变量,用于存储字典),您将获得所有的缓存管理方法(滑动过期等)

EDIT

编辑

If there is a concern about update times then consider two cache objects plus a controller e.g.

如果有对更新时间的关注,那么考虑两个缓存对象加上一个控制器。

  1. Active Cache
  2. 主动缓存
  3. Backup Cache - this is the one that will be updated
  4. 备份缓存——这是一个将被更新的缓存
  5. Cache controller (another cache object?) this will indicate which object is active
  6. 缓存控制器(另一个缓存对象?)将显示哪个对象是活动的。

So the process to update will be

所以更新的过程是

  1. Update backup cache
  2. 更新备份缓存
  3. Completes. Check is valid
  4. 完成。检查是有效的
  5. Backup becomes active and visa versa. The control now flags the Backup cache as being active
  6. 备份变为活动,反之亦然。控件现在将备份缓存标记为活动

There needs to be a method which will fire when the products cache object is populated. I would probably use the CacheItemRemoveCallback delegate to initiate the cache repopulation. Or do an async call in the facade type class - you wouldn't want it blocking the current thread

需要有一个方法在填充产品缓存对象时触发。我可能会使用CacheItemRemoveCallback委托来启动缓存重新填充。或者在facade类型类中执行异步调用——您不希望它阻塞当前线程

I'm sure there are many other variants of this

我肯定还有很多其他的版本

EDIT 2

编辑2

Actually thinking about this I would make the controller class something like this

实际上,我想让控制器类像这样

public class CacheController
{
    public StateEnum Cache1State {get;set;}
    public StateEnum Cache1State {get;set;}
    public bool IsUpdating {get;set;}
}

The state would be active, backup, updating and perhaps inactive and error. You would set the IsUpdating flag when the update is occurring and then back to false once complete to stop multiple threads trying to update at once - i.e. a race condition. The class is just a general principle and could/should be amended as required

状态将是活动的、备份的、更新的,也可能是不活动的和错误的。当更新发生时,您将设置IsUpdating标志,然后在完成后返回false,以停止多个线程试图同时更新——即竞争条件。这个类只是一个一般的原则,可以/应该根据需要进行修改

#1


3  

Sorry - this might be naive/obvious but just have a facade type class which does

对不起——这可能很幼稚/明显,但是只有facade类型的类可以做到这一点

if(Cache["Products"] == null)
{
        Cache.Insert("Products", Products(), null, DateTime.Now.AddMinutes(15), TimeSpan.Zero);
 }
 return Cache["Products"];

There is also a CacheItemRemoveCallback delegate which you could use to repopulate an expired cache. As an alternative

还有一个CacheItemRemoveCallback委托,您可以使用它来重新填充过期的缓存。作为一种替代方法

ALSO

use the cache object rather than static objects. More efficient apparently (Asp.net - Caching vs Static Variable for storing a Dictionary) and you get all your cache management methods (sliding expiration and so on)

使用缓存对象而不是静态对象。显然更有效(Asp.net -缓存vs静态变量,用于存储字典),您将获得所有的缓存管理方法(滑动过期等)

EDIT

编辑

If there is a concern about update times then consider two cache objects plus a controller e.g.

如果有对更新时间的关注,那么考虑两个缓存对象加上一个控制器。

  1. Active Cache
  2. 主动缓存
  3. Backup Cache - this is the one that will be updated
  4. 备份缓存——这是一个将被更新的缓存
  5. Cache controller (another cache object?) this will indicate which object is active
  6. 缓存控制器(另一个缓存对象?)将显示哪个对象是活动的。

So the process to update will be

所以更新的过程是

  1. Update backup cache
  2. 更新备份缓存
  3. Completes. Check is valid
  4. 完成。检查是有效的
  5. Backup becomes active and visa versa. The control now flags the Backup cache as being active
  6. 备份变为活动,反之亦然。控件现在将备份缓存标记为活动

There needs to be a method which will fire when the products cache object is populated. I would probably use the CacheItemRemoveCallback delegate to initiate the cache repopulation. Or do an async call in the facade type class - you wouldn't want it blocking the current thread

需要有一个方法在填充产品缓存对象时触发。我可能会使用CacheItemRemoveCallback委托来启动缓存重新填充。或者在facade类型类中执行异步调用——您不希望它阻塞当前线程

I'm sure there are many other variants of this

我肯定还有很多其他的版本

EDIT 2

编辑2

Actually thinking about this I would make the controller class something like this

实际上,我想让控制器类像这样

public class CacheController
{
    public StateEnum Cache1State {get;set;}
    public StateEnum Cache1State {get;set;}
    public bool IsUpdating {get;set;}
}

The state would be active, backup, updating and perhaps inactive and error. You would set the IsUpdating flag when the update is occurring and then back to false once complete to stop multiple threads trying to update at once - i.e. a race condition. The class is just a general principle and could/should be amended as required

状态将是活动的、备份的、更新的,也可能是不活动的和错误的。当更新发生时,您将设置IsUpdating标志,然后在完成后返回false,以停止多个线程试图同时更新——即竞争条件。这个类只是一个一般的原则,可以/应该根据需要进行修改