c# - .net MemoryCache - 通知删除的项目

标签 c# caching memorycache

我在 .NET 4.0 和 c# 中使用 .net 内存缓存,我希望在删除项目时通知我的应用程序(这样我就可以将它已删除写入日志文件或通知 UI,该项目已被删除)。

有没有办法做到这一点。

我使用的是 System.Runtime.Caching.MemoryCache 而不是 System.Web.Caching

最佳答案

编辑:如果您使用的是 System.Runtime.Caching.MemoryCache,则有一个 callbackCacheItemPolicy 对象上删除,以及一个更新。

myMemoryCache.Set("key", null, new CacheItemPolicy() {RemovedCallback = new CacheEntryRemovedCallback(CacheRemovedCallback) /* your other parameters here */});

public void CacheRemovedCallback(CacheEntryRemovedArguments arguments)
{
    // do what's needed
}

初步回答

inserting data in the .net cache对于 System.Web.Caching 命名空间,您可以选择设置回调以通知删除

Cache.Insert("data", "", null, DateTime.Now.AddMinutes(1), System.Web.Caching.Cache.NoSlidingExpiration, CacheItemPriority.High, new CacheItemRemovedCallback(CacheRemovedCallback));

public string CacheRemovedCallback(String key, object value, System.Web.Caching.CacheItemRemovedReason removedReason)
{
    // here you can log, renew the value, etc...
}

Insert 方法还有一个签名,可让您将回调指定为 notified before the item is removed

关于c# - .net MemoryCache - 通知删除的项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22935868/

相关文章:

c# - 如何使用 NHibernate Criteria API 进行多重连接

php - Symfony Composer 安装而不清除缓存

C# 依赖注入(inject) - 身份验证

c# - 使用内存缓存时 Asp.Net MVC 中的 InvalidOperationException

c# - 使用 XElement 时如何输入文本

C# : Problem with SqlDataSourceEnumerator. 实例.GetDataSources()

c# - 重构参数和单元测试

javascript - 缓存数据量不累加

java - Apache Ignite JDBC 和具有第 3 方持久性的后写缓存策略

c# - 需要一个高效的内存缓存,每秒可以处理 4k 到 7k 的查找或写入