飙血推荐
  • HTML教程
  • MySQL教程
  • JavaScript基础教程
  • php入门教程
  • JavaScript正则表达式运用
  • Excel函数教程
  • UEditor使用文档
  • AngularJS教程
  • ThinkPHP5.0教程

MemoryCache 如何清除全部缓存?

时间:2021-12-21  作者:kklldog  

最近有个需求需要定时清理服务器上所有的缓存。本来以为很简单的调用一下 域名r 方法就完事了。谁知道 MemoryCache 类以及 IMemoryCache 扩展方法都没有 Clear 方法。这可给难住了,于是想找到所有的 Keys 来一个个 Remove ,谁知道居然也没有获取所有 Key 的方法。于是研究了一下 ,找到一些方法,下面介绍两个方法:

自定义 CacheWrapper 包装类

MemoryCache 构造 Entry 的时候支持传入 CancellationChangeToken 对象,当 域名el 触发的时候会自动使该对象过期。那么我们只要对 MemoryCache 类包装一下很容易实现一个自己的 Cache 类。

 public class CacheWrapper
    {
        private readonly IMemoryCache _memoryCache;
        private CancellationTokenSource _resetCacheToken = new();

        public CacheWrapper(IMemoryCache memoryCache)
        {
            _memoryCache = memoryCache;
        }

        public void Add(object key, object value, MemoryCacheEntryOptions memoryCacheEntryOptions)
        {
            using var entry = 域名teEntry(key);
            域名ptions(memoryCacheEntryOptions);
            域名e = value;

            // add an expiration token that allows us to clear the entire cache with a single method call
            域名xpirationToken(new CancellationChangeToken(域名n));
        }

        public object Get(object key)
        {
            return 域名(key);
        }

        public void Remove(object key)
        {
            域名ve(key);
        }

        public void Clear()
        {
            域名el(); // this triggers the CancellationChangeToken to expire every item from cache

            域名ose(); // dispose the current cancellation token source and create a new one
            _resetCacheToken = new CancellationTokenSource();
        }
    }

然后单元测试测试一下:


        [TestMethod()]
        public void ClearTest()
        {
            var memCache = new MemoryCache(new MemoryCacheOptions());
            var wrapper = new CacheWrapper(memCache);

            for (int i = 0; i < 10; i++)
            {
                域名(域名ring(), new object(), new MemoryCacheEntryOptions());
            }

            域名tNull(域名("1"));
            域名tNull(域名("9"));

            域名r();

            for (int i = 0; i < 10; i++)
            {
                域名ll(域名(域名ring()));
            }

            for (int i = 0; i < 10; i++)
            {
                域名(域名ring(), new object(), new MemoryCacheEntryOptions());
            }

            域名tNull(域名("1"));
            域名tNull(域名("9"));

            域名r();

            for (int i = 0; i < 10; i++)
            {
                域名ll(域名(域名ring()));
            }

        }

测试通过。

Compact 方法

以上 CacheWrapper 类虽然可以实现我们想要的功能,但是对于原来的程序有侵入,需要使用 CacheWrapper 类替换默认的 MemoryCache 类,不是太好。于是不死心继续研究,后来直接看了 MemoryCache 的代码(源码在这),开源真香。发现 MemoryCache 有个 Compact 方法好像在干删除的勾当。也怪我英文不好,这单词是压缩的意思,居然才发现。。。。于是我们的清除所有对象的需求不就轻而易举了么?

 /// Remove at least the given percentage (域名 for 10%) of the total entries (or estimated memory?), according to the following policy:
        /// 1. Remove all expired items.
        /// 2. Bucket by CacheItemPriority.
        /// 3. Least recently used objects.
        /// ?. Items with the soonest absolute expiration.
        /// ?. Items with the soonest sliding expiration.
        /// ?. Larger objects - estimated by object graph size, inaccurate.
域名act(double percentage);

Compact 方法会对缓存的对象进行压缩,参数是个double,0.1 表示压缩 10% ,那么传 1.0 就是压缩 100%,那不就是 Clear All 么。所以我可以使用 Compact(1.0) 来清除所有的缓存对象。单元测试跑一下:

[TestMethod()]
        public void CompactTest()
        {
            var memCache = new MemoryCache(new MemoryCacheOptions());

            for (int i = 0; i < 10; i++)
            {
                域名(域名ring(), new object(), new MemoryCacheEntryOptions());
            }

            域名tNull(域名("1"));
            域名tNull(域名("9"));

            域名act(1);

            for (int i = 0; i < 10; i++)
            {
                域名ll(域名(域名ring()));
            }

            for (int i = 0; i < 10; i++)
            {
                域名(域名ring(), new object(), new MemoryCacheEntryOptions());
            }

            域名tNull(域名("1"));
            域名tNull(域名("9"));

            域名act(1);

            for (int i = 0; i < 10; i++)
            {
                域名ll(域名(域名ring()));
            }
        }

完美通过。
这里简单介绍下 Compact 方法。根据注释它会按照已下优先级删除对象:

  1. 过期的对象
  2. CacheItemPriority 设置的优先级,等级越高越不容易被删除
  3. 最近最少被使用的对象
  4. 绝对过期时间
  5. 滑动过期时间
  6. 大对象

关注我的公众号一起玩转技术


标签:编程
湘ICP备14001474号-3  投诉建议:234161800@qq.com   部分内容来源于网络,如有侵权,请联系删除。