.net MemoryCache-关于项目的通知已删除 [英] .net MemoryCache - notify on item removed

查看:87
本文介绍了.net MemoryCache-关于项目的通知已删除的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在.NET 4.0和c#中使用了.net内存缓存,我希望在删除某个项目时通知我的应用程序(因此我可以将其删除的内容写到日志文件中或通知UI,将该项目删除).

I'm using a .net Memory Cache with .NET 4.0 and c#, I want my application to be notified when an item is removed (so I can write that it has been removed to a log file or notify the UI, that the item is removed).

反正有这样做吗?

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

I'm using System.Runtime.Caching.MemoryCache not System.Web.Caching

推荐答案

编辑:如果您使用的是System.Runtime.Caching.MemoryCache,则有

EDIT: If you're using the System.Runtime.Caching.MemoryCache there is a callback on the CacheItemPolicy object for deletion, as well as one for update.

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

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

初始答案

在.net缓存中插入数据 System.Web.Caching名称空间,您可以选择设置一个回调,以通知其删除操作

When inserting data in the .net cache for the System.Web.Caching namespace you have the option to set a callback to be notified of removal

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方法的签名,该签名使您可以将回调指定为

There is also a signature for the Insert method that lets you specify a callback to be notified before the item is removed

这篇关于.net MemoryCache-关于项目的通知已删除的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆