如何清除一个System.Runtime.Caching.MemoryCache [英] How do I clear a System.Runtime.Caching.MemoryCache

查看:1674
本文介绍了如何清除一个System.Runtime.Caching.MemoryCache的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用 System.Runtime.Caching.MemoryCache 持有其永不过期的项目。然而,有时我需要清除整个缓存的能力。我该怎么办呢?

我问过类似的问题这里关于我是否可以枚举缓存,但这是因为它需要枚举过程中要同步一个坏主意。

我已经试过用 .Trim(100)但是,这并不在所有的工作。

我试着让通过LINQ的所有键的列表,但后来我回来了,我开始,因为驱逐一个项目接一个,很容易导致竞争条件。

我想存储所有的键,然后执行卸下摆臂(键)为每一个,但有一个隐含的竞争条件也在那里,所以我ð需要锁定的访问键的列表,事情变得复杂了。

然后我想我应该能够调用 .Dispose()在整个高速缓存,但我不知道这是最好的方法,因为它的实现方式。

使用 ChangeMonitors 不是我设计的一个选项,是unnecassarily复杂的这样一个微不足道的要求。

那么,我怎么彻底清除缓存?

解决方案

您应该不叫处置的MemoryCache的默认成员。如果你希望能够使用它了:

  

缓存的状态被设置为指示高速缓存配置。   任何试图调用改变状态的公共缓存方法   高速缓存,例如方法是添加,删除,或检索缓存   项,可能会导致意外的行为。例如,如果你调用   设置方法缓存设置之后,会出现一个空操作错误。如果你   尝试从缓存中检索项目,Get方法总会   返回任何结果。   <一href="http://msdn.microsoft.com/en-us/library/system.runtime.caching.memorycache.dispose.aspx">http://msdn.microsoft.com/en-us/library/system.runtime.caching.memorycache.dispose.aspx

关于修剪,它应该工作:

  

裁剪属性首先移除已超过绝对或滑动过期条目。已注册的任何回调   对于被删除将被传递过期,已删除的原因,项目

     

如果删除过期条目不足以达到指定的百分比微调,更多的条目将被从缓存中删除   根据最近最少使用(LRU)算法,直到所请求的   修剪百分比达到了。

但是,其他两个用户报告它没有同一页上工作,所以我猜你被卡住删除()<一href="http://msdn.microsoft.com/en-us/library/system.runtime.caching.memorycache.trim.aspx">http://msdn.microsoft.com/en-us/library/system.runtime.caching.memorycache.trim.aspx

更新的 不过,我看没有提到它是单或其他不安全的有多个实例,所以你应该能够覆盖你参考。

但是,如果你需要释放从默认实例占用的内存,您将需要手动清除或通过处置永久破坏它(使之无法使用)。

- :根据你的问题,你可以让自己的单,气势类返回的MemoryCache您可以在内部处理随意..作为一个高速缓存的性质

I use a System.Runtime.Caching.MemoryCache to hold items which never expire. However, at times I need the ability to clear the entire cache. How do I do that?

I asked a similar question here concerning whether I could enumerate the cache, but that is a bad idea as it needs to be synchronised during enumeration.

I've tried using .Trim(100) but that doesn't work at all.

I've tried getting a list of all the keys via Linq, but then I'm back where I started because evicting items one-by-one can easily lead to race conditions.

I thought to store all the keys, and then issue a .Remove(key) for each one, but there is an implied race condition there too, so I'd need to lock access to the list of keys, and things get messy again.

I then thought that I should be able to call .Dispose() on the entire cache, but I'm not sure if this is the best approach, due to the way it's implemented.

Using ChangeMonitors is not an option for my design, and is unnecassarily complex for such a trivial requirement.

So, how do I completely clear the cache?

解决方案

You should not call dispose on the Default member of the MemoryCache if you want to be able to use it anymore:

The state of the cache is set to indicate that the cache is disposed. Any attempt to call public caching methods that change the state of the cache, such as methods that add, remove, or retrieve cache entries, might cause unexpected behavior. For example, if you call the Set method after the cache is disposed, a no-op error occurs. If you attempt to retrieve items from the cache, the Get method will always return Nothing. http://msdn.microsoft.com/en-us/library/system.runtime.caching.memorycache.dispose.aspx

About the Trim, it's supposed to work:

The Trim property first removes entries that have exceeded either an absolute or sliding expiration. Any callbacks that are registered for items that are removed will be passed a removed reason of Expired.

If removing expired entries is insufficient to reach the specified trim percentage, additional entries will be removed from the cache based on a least-recently used (LRU) algorithm until the requested trim percentage is reached.

But two other users reported it doesnt work on same page so I guess you are stuck with Remove() http://msdn.microsoft.com/en-us/library/system.runtime.caching.memorycache.trim.aspx

Update However I see no mention of it being singleton or otherwise unsafe to have multiple instances so you should be able to overwrite your reference.

But if you need to free the memory from the Default instance you will have to clear it manually or destroy it permanently via dispose (rendering it unusable).

Based on your question you could make your own singleton-imposing class returning a Memorycache you may internally dispose at will.. Being the nature of a cache :-)

这篇关于如何清除一个System.Runtime.Caching.MemoryCache的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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