1
0

MemoryCache.cs 3.3 KB

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