MemoryCache.cs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. 
  2. using System;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Reflection;
  7. using System.Text.RegularExpressions;
  8. using System.Threading.Tasks;
  9. using Microsoft.Extensions.Caching.Memory;
  10. using Admin.Core.Common.Helpers;
  11. namespace Admin.Core.Common.Cache
  12. {
  13. /// <summary>
  14. /// 内存缓存
  15. /// </summary>
  16. public class MemoryCache : ICache
  17. {
  18. private readonly IMemoryCache _memoryCache;
  19. public MemoryCache(IMemoryCache memoryCache)
  20. {
  21. _memoryCache = memoryCache;
  22. }
  23. public long Del(params string[] key)
  24. {
  25. foreach(var k in key)
  26. {
  27. _memoryCache.Remove(k);
  28. }
  29. return key.Length;
  30. }
  31. public Task<long> DelAsync(params string[] key)
  32. {
  33. foreach (var k in key)
  34. {
  35. _memoryCache.Remove(k);
  36. }
  37. return Task.FromResult(key.Length.ToLong());
  38. }
  39. public async Task<long> DelByPatternAsync(string pattern)
  40. {
  41. if (pattern.IsNull())
  42. return default;
  43. pattern = Regex.Replace(pattern, @"\{.*\}", "(.*)");
  44. var keys = GetAllKeys().Where(k => Regex.IsMatch(k, pattern));
  45. if(keys != null && keys.Count() > 0)
  46. {
  47. return await DelAsync(keys.ToArray());
  48. }
  49. return default;
  50. }
  51. public bool Exists(string key)
  52. {
  53. return _memoryCache.TryGetValue(key, out _);
  54. }
  55. public Task<bool> ExistsAsync(string key)
  56. {
  57. return Task.FromResult(_memoryCache.TryGetValue(key, out _));
  58. }
  59. public string Get(string key)
  60. {
  61. return _memoryCache.Get(key)?.ToString();
  62. }
  63. public T Get<T>(string key)
  64. {
  65. return _memoryCache.Get<T>(key);
  66. }
  67. public Task<string> GetAsync(string key)
  68. {
  69. return Task.FromResult(Get(key));
  70. }
  71. public Task<T> GetAsync<T>(string key)
  72. {
  73. return Task.FromResult(Get<T>(key));
  74. }
  75. public bool Set(string key, object value)
  76. {
  77. _memoryCache.Set(key, value);
  78. return true;
  79. }
  80. public bool Set(string key, object value, TimeSpan expire)
  81. {
  82. _memoryCache.Set(key, value, expire);
  83. return true;
  84. }
  85. public Task<bool> SetAsync(string key, object value)
  86. {
  87. Set(key, value);
  88. return Task.FromResult(true);
  89. }
  90. public Task<bool> SetAsync(string key, object value, TimeSpan expire)
  91. {
  92. Set(key, value, expire);
  93. return Task.FromResult(true);
  94. }
  95. private List<string> GetAllKeys()
  96. {
  97. const BindingFlags flags = BindingFlags.Instance | BindingFlags.NonPublic;
  98. var entries = _memoryCache.GetType().GetField("_entries", flags).GetValue(_memoryCache);
  99. var cacheItems = entries as IDictionary;
  100. var keys = new List<string>();
  101. if (cacheItems == null) return keys;
  102. foreach (DictionaryEntry cacheItem in cacheItems)
  103. {
  104. keys.Add(cacheItem.Key.ToString());
  105. }
  106. return keys;
  107. }
  108. }
  109. }