CommonMemoryCaching.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. using System;
  2. using System.Collections.Concurrent;
  3. using System.Collections.Generic;
  4. using System.Threading.Tasks;
  5. namespace WingCloudServer.GeneralDocTools.Helper
  6. {
  7. public class CommonMemoryCaching
  8. {
  9. private static readonly object SynObject = new object();
  10. private static CommonMemoryCaching instance = null;
  11. private CommonMemoryCaching()
  12. {
  13. Task.Run(async () =>
  14. {
  15. while (true)
  16. {
  17. List<string> list = new List<string>();
  18. foreach (var item in this._myCacheExpire.Keys)
  19. {
  20. this._myCacheExpire.TryGetValue(item, out var expireObj);
  21. if (expireObj != null)
  22. {
  23. if (expireObj.Item1 < expireObj.Item2)
  24. {
  25. list.Add(item);
  26. }
  27. }
  28. }
  29. foreach (var item in list)
  30. {
  31. this._myCacheExpire.TryRemove(item, out var expireObj);
  32. this._myCache.TryRemove(item, out var cacheObj);
  33. }
  34. await Task.Delay(500);
  35. }
  36. });
  37. }
  38. /// <summary>
  39. /// 单例模式
  40. /// </summary>
  41. public static CommonMemoryCaching GetInstance()
  42. {
  43. if (instance == null)
  44. {
  45. lock (SynObject)
  46. {
  47. if (instance == null)
  48. {
  49. instance = new CommonMemoryCaching();
  50. }
  51. }
  52. }
  53. return instance;
  54. }
  55. private readonly ConcurrentDictionary<string, object> _myCache = new ConcurrentDictionary<string, object>();
  56. private readonly ConcurrentDictionary<string, Tuple<DateTime, DateTime>> _myCacheExpire = new ConcurrentDictionary<string, Tuple<DateTime, DateTime>>();
  57. private const long DefaultExpireTime = 300000;
  58. /// <summary>
  59. /// 获取和设置缓存信息
  60. /// </summary>
  61. /// <typeparam name="T"></typeparam>
  62. /// <param name="cacheItemName"></param>
  63. /// <param name="objectSettingFunction"></param>
  64. /// <param name="expireTime">默认过期时间5分钟</param>
  65. /// <returns></returns>
  66. public T GetOrSetObjectFromCache<T>(string cacheItemName, Func<T> objectSettingFunction, bool isSetExpire = true, long expireTime = DefaultExpireTime)
  67. {
  68. T cacheObject = default(T);
  69. this._myCache.TryGetValue(cacheItemName, out var cacheObj);
  70. if (cacheObj != null)
  71. {
  72. cacheObject = (T)cacheObj;
  73. }
  74. else
  75. {
  76. if (objectSettingFunction != null)
  77. {
  78. cacheObject = objectSettingFunction();
  79. this._myCache.TryAdd(cacheItemName, cacheObject);
  80. if (isSetExpire)
  81. {
  82. Tuple<DateTime, DateTime> tuple = Tuple.Create<DateTime, DateTime>(DateTime.Now, DateTime.Now.AddMilliseconds(expireTime));
  83. this._myCacheExpire.TryAdd(cacheItemName, tuple);
  84. }
  85. }
  86. }
  87. return cacheObject;
  88. }
  89. /// <summary>
  90. /// 设置缓存
  91. /// </summary>
  92. /// <param name="key"></param>
  93. /// <param name="value"></param>
  94. /// <param name="cacheTimeInMinutes"></param>
  95. public void SetValueToCache(string key, object value, bool isSetExpire = true, long expireTime = DefaultExpireTime)
  96. {
  97. if (Contains(key))
  98. {
  99. Remove(key);
  100. }
  101. this._myCache.TryAdd(key, value);
  102. if (isSetExpire)
  103. {
  104. Tuple<DateTime, DateTime> tuple = Tuple.Create<DateTime, DateTime>(DateTime.Now, DateTime.Now.AddMilliseconds(expireTime));
  105. this._myCacheExpire.TryAdd(key, tuple);
  106. }
  107. }
  108. /// <summary>
  109. /// 获取缓存
  110. /// </summary>
  111. /// <param name="key"></param>
  112. /// <returns></returns>
  113. public object GetValueFromCache(string key)
  114. {
  115. if (Contains(key))
  116. {
  117. this._myCache.TryGetValue(key, out var cacheObj);
  118. return cacheObj;
  119. }
  120. else
  121. {
  122. return null;
  123. }
  124. }
  125. /// <summary>
  126. /// 验证缓存是否存在
  127. /// </summary>
  128. /// <param name="key"></param>
  129. /// <returns></returns>
  130. public bool Contains(string key)
  131. {
  132. return this._myCache.ContainsKey(key);
  133. }
  134. /// <summary>
  135. /// 删除制定缓存
  136. /// </summary>
  137. /// <param name="key"></param>
  138. public void Remove(string key)
  139. {
  140. this._myCache.TryRemove(key, out var cacheObj);
  141. this._myCacheExpire.TryRemove(key, out var expireObj);
  142. }
  143. /// <summary>
  144. /// 移除所有缓存
  145. /// </summary>
  146. public void RemoveAll()
  147. {
  148. this._myCache.Clear();
  149. this._myCacheExpire.Clear();
  150. }
  151. }
  152. }