How do I clear a System.Runtime.Caching.MemoryCache

后端 未结 7 2262
隐瞒了意图╮
隐瞒了意图╮ 2020-12-09 07:16

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?

相关标签:
7条回答
  • 2020-12-09 07:56

    I was struggling with this at first. MemoryCache.Default.Trim(100) does not work (as discussed). Trim is a best attempt, so if there are 100 items in the cache, and you call Trim(100) it will remove the ones least used.

    Trim returns the count of items removed, and most people expect that to remove all items.

    This code removes all items from MemoryCache for me in my xUnit tests with MemoryCache.Default. MemoryCache.Default is the default Region.

    foreach (var element in MemoryCache.Default)
    {
        MemoryCache.Default.Remove(element.Key);
    }
    
    0 讨论(0)
  • 2020-12-09 07:59

    Here's is what I had made for something I was working on...

    public void Flush()
    {
        List<string> cacheKeys = MemoryCache.Default.Select(kvp => kvp.Key).ToList();
        foreach (string cacheKey in cacheKeys)
        {
            MemoryCache.Default.Remove(cacheKey);
        }
    }
    
    0 讨论(0)
  • 2020-12-09 08:02

    I know this is an old question but the best option I've come across is to

    Dispose the existing MemoryCache and create a new MemoryCache object. https://stackoverflow.com/a/4183319/880642

    The answer doesn't really provide the code to do this in a thread safe way. But this can be achieved using Interlocked.Exchange

    var oldCache = Interlocked.Exchange(ref _existingCache, new MemoryCache("newCacheName"));
    oldCache.Dispose();
    

    This will swap the existing cache with a new one and allow you to safely call Dispose on the original cache. This avoids needing to enumerate the items in the cache and race conditions caused by disposing a cache while it is in use.

    0 讨论(0)
  • 2020-12-09 08:09

    Check out this post, and specifically, the answer that Thomas F. Abraham posted. It has a solution that enables you to clear the entire cache or a named subset.

    The key thing here is:

    // Cache objects are obligated to remove entry upon change notification.
    base.OnChanged(null);
    

    I've implemented this myself, and everything seems to work just fine.

    0 讨论(0)
  • 2020-12-09 08:16

    The details in @stefan's answer detail the principle; here's how I'd do it.

    One should synchronise access to the cache whilst recreating it, to avoid the race condition of client code accessing the cache after it is disposed, but before it is recreated.

    To avoid this synchronisation, do this in your adapter class (which wraps the MemoryCache):

    public void clearCache() {
      var oldCache = TheCache;
      TheCache = new MemoryCache("NewCacheName", ...);
      oldCache.Dispose();
      GC.Collect();
    }
    

    This way, TheCache is always in a non-disposed state, and no synchronisation is needed.

    0 讨论(0)
  • 2020-12-09 08:20

    I ran into this problem too. .Dispose() did something quite different than what I expected.

    Instead, I added a static field to my controller class. I did not use the default cache, to get around this behavior, but created a private one (if you want to call it that). So my implementation looked a bit like this:

    public class MyController : Controller
    {
    
        static MemoryCache s_cache = new MemoryCache("myCache");
    
        public ActionResult Index()
        {
    
            if (conditionThatInvalidatesCache)
            {
                s_cache = new MemoryCache("myCache");
            }
    
            String s = s_cache["key"] as String;
    
            if (s == null)
            {
                //do work
                //add to s_cache["key"]
            }
    
            //do whatever next
        }
    }
    
    0 讨论(0)
提交回复
热议问题