经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 程序设计 » ASP.net » 查看文章
MemoryCache 如何清除全部缓存?
来源:cnblogs  作者:Agile.Zhou  时间:2021/12/24 9:11:12  对本文有异议

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

自定义 CacheWrapper 包装类

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

  1. public class CacheWrapper
  2. {
  3. private readonly IMemoryCache _memoryCache;
  4. private CancellationTokenSource _resetCacheToken = new();
  5. public CacheWrapper(IMemoryCache memoryCache)
  6. {
  7. _memoryCache = memoryCache;
  8. }
  9. public void Add(object key, object value, MemoryCacheEntryOptions memoryCacheEntryOptions)
  10. {
  11. using var entry = _memoryCache.CreateEntry(key);
  12. entry.SetOptions(memoryCacheEntryOptions);
  13. entry.Value = value;
  14. // add an expiration token that allows us to clear the entire cache with a single method call
  15. entry.AddExpirationToken(new CancellationChangeToken(_resetCacheToken.Token));
  16. }
  17. public object Get(object key)
  18. {
  19. return _memoryCache.Get(key);
  20. }
  21. public void Remove(object key)
  22. {
  23. _memoryCache.Remove(key);
  24. }
  25. public void Clear()
  26. {
  27. _resetCacheToken.Cancel(); // this triggers the CancellationChangeToken to expire every item from cache
  28. _resetCacheToken.Dispose(); // dispose the current cancellation token source and create a new one
  29. _resetCacheToken = new CancellationTokenSource();
  30. }
  31. }

然后单元测试测试一下:

  1. [TestMethod()]
  2. public void ClearTest()
  3. {
  4. var memCache = new MemoryCache(new MemoryCacheOptions());
  5. var wrapper = new CacheWrapper(memCache);
  6. for (int i = 0; i < 10; i++)
  7. {
  8. wrapper.Add(i.ToString(), new object(), new MemoryCacheEntryOptions());
  9. }
  10. Assert.IsNotNull(wrapper.Get("1"));
  11. Assert.IsNotNull(wrapper.Get("9"));
  12. wrapper.Clear();
  13. for (int i = 0; i < 10; i++)
  14. {
  15. Assert.IsNull(wrapper.Get(i.ToString()));
  16. }
  17. for (int i = 0; i < 10; i++)
  18. {
  19. wrapper.Add(i.ToString(), new object(), new MemoryCacheEntryOptions());
  20. }
  21. Assert.IsNotNull(wrapper.Get("1"));
  22. Assert.IsNotNull(wrapper.Get("9"));
  23. wrapper.Clear();
  24. for (int i = 0; i < 10; i++)
  25. {
  26. Assert.IsNull(wrapper.Get(i.ToString()));
  27. }
  28. }

测试通过。

Compact 方法

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

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

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

  1. [TestMethod()]
  2. public void CompactTest()
  3. {
  4. var memCache = new MemoryCache(new MemoryCacheOptions());
  5. for (int i = 0; i < 10; i++)
  6. {
  7. memCache.Set(i.ToString(), new object(), new MemoryCacheEntryOptions());
  8. }
  9. Assert.IsNotNull(memCache.Get("1"));
  10. Assert.IsNotNull(memCache.Get("9"));
  11. memCache.Compact(1);
  12. for (int i = 0; i < 10; i++)
  13. {
  14. Assert.IsNull(memCache.Get(i.ToString()));
  15. }
  16. for (int i = 0; i < 10; i++)
  17. {
  18. memCache.Set(i.ToString(), new object(), new MemoryCacheEntryOptions());
  19. }
  20. Assert.IsNotNull(memCache.Get("1"));
  21. Assert.IsNotNull(memCache.Get("9"));
  22. memCache.Compact(1);
  23. for (int i = 0; i < 10; i++)
  24. {
  25. Assert.IsNull(memCache.Get(i.ToString()));
  26. }
  27. }

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

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

PS

  1. 评论区有人提到 main 分支的代码上微软已经加上了 Clear 方法。https://github.com/dotnet/runtime/blob/main/src/libraries/Microsoft.Extensions.Caching.Memory/src/MemoryCache.cs#L272 感谢 @yi念之间 兄弟指出。
  2. Compact 方法有个问题,如果 CacheItemPriority 设置为 NeverRemove 那么就删不掉了。感谢 @有态度的马甲 兄弟指出。
  3. 这里说的是 Microsoft.Extensions.Caching 命名空间下的 MemoryCache , 不是 System.Runtime下的那个 。

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

QQ群:1022985150 VX:kklldog 一起探讨学习.NET技术
作者:Agile.Zhou(kklldog)
出处:http://www.cnblogs.com/kklldog/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。

原文链接:http://www.cnblogs.com/kklldog/p/how-clear-all-memory-cache.html

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号