缓存过期事件asp.net企业库

时间:2011-01-27 09:48:35

标签: asp.net

我想知道每次缓存项过期时是否会发生任何事件,或者当缓存项过期时引发的任何事件。我在asp.net中使用企业库进行缓存。我想在缓存即将过期而不是调用函数或调用事件时将一些数据存储在我的数据库中我自己正在考虑使用已经管理的缓存库事件,该事件用于刷新或处理缓存项。 / p>

1 个答案:

答案 0 :(得分:2)

假设您正在使用缓存应用程序块,那么在向缓存添加项目时指定一个回调委托。

  

接收项目从缓存中删除的通知要求您在调用Add方法时指定实现ICacheItemRefreshAction的类

您需要提供一个实现ICacheItemRefreshAction的类:

[Serializable]
public class ProductCacheRefreshAction : ICacheItemRefreshAction
{
  public void Refresh(string key, object expiredValue, CacheItemRemovedReason removalReason)
  {
    // Item has been removed from cache. Perform desired actions here, based on
    // the removal reason (for example, refresh the cache with the item).
  }
}

正如您所看到的,当删除某个项目时,会使用键调用此Refresh()方法,该对象将被删除。您只需要将expiredObject参数强制转换为正确的类型,并安排将其存储到数据库中。

如此处所述:http://msdn.microsoft.com/en-us/library/ff664621(v=PandP.50).aspx