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

后端 未结 7 2291
隐瞒了意图╮
隐瞒了意图╮ 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:59

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

    public void Flush()
    {
        List cacheKeys = MemoryCache.Default.Select(kvp => kvp.Key).ToList();
        foreach (string cacheKey in cacheKeys)
        {
            MemoryCache.Default.Remove(cacheKey);
        }
    }
    

提交回复
热议问题