CacheHelper.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Web.Caching;
  5. namespace Masuit.Tools.Net
  6. {
  7. /// <summary>
  8. /// 全局统一的缓存类
  9. /// </summary>
  10. public static class CacheHelper
  11. {
  12. #region 获取数据缓存
  13. /// <summary>
  14. /// 获取数据缓存
  15. /// </summary>
  16. /// <typeparam name="T">返回的类型</typeparam>
  17. /// <param name="cache"></param>
  18. /// <param name="cacheKey">键</param>
  19. public static T GetCache<T>(this Cache cache, string cacheKey)
  20. {
  21. return (T)cache[cacheKey];
  22. }
  23. #endregion
  24. #region 设置数据缓存
  25. /// <summary>
  26. /// 设置数据缓存
  27. /// </summary>
  28. /// <param name="cache"></param>
  29. /// <param name="cacheKey">键</param>
  30. /// <param name="objObject">值</param>
  31. public static void SetCache(this Cache cache, string cacheKey, object objObject)
  32. {
  33. cache.Insert(cacheKey, objObject);
  34. }
  35. /// <summary>
  36. /// 设置数据缓存
  37. /// </summary>
  38. /// <param name="cache"></param>
  39. /// <param name="cacheKey">键</param>
  40. /// <param name="objObject">值</param>
  41. /// <param name="timeout">过期时间</param>
  42. /// <exception cref="ArgumentNullException"><paramref name="cacheKey"/>"/> is <c>null</c>.</exception>
  43. public static void SetCache(this Cache cache, string cacheKey, object objObject, TimeSpan timeout)
  44. {
  45. if (cacheKey == null) throw new ArgumentNullException(nameof(cacheKey));
  46. cache.Insert(cacheKey, objObject, null, DateTime.MaxValue, timeout, CacheItemPriority.NotRemovable, null);
  47. }
  48. /// <summary>
  49. /// 设置当前应用程序指定CacheKey的Cache值
  50. /// </summary>
  51. /// <param name="cache"></param>
  52. /// <param name="cacheKey">键</param>
  53. /// <param name="objObject">值</param>
  54. /// <param name="absoluteExpiration">绝对过期时间</param>
  55. /// <param name="slidingExpiration">滑动过期时间</param>
  56. public static void SetCache(this Cache cache, string cacheKey, object objObject, DateTime absoluteExpiration, TimeSpan slidingExpiration)
  57. {
  58. cache.Insert(cacheKey, objObject, null, absoluteExpiration, slidingExpiration);
  59. }
  60. #endregion
  61. #region 移除缓存
  62. /// <summary>
  63. /// 移除指定数据缓存
  64. /// </summary>
  65. /// <param name="cache"></param>
  66. /// <param name="cacheKey">键</param>
  67. public static void RemoveAllCache(this Cache cache, string cacheKey) => cache.Remove(cacheKey);
  68. /// <summary>
  69. /// 移除全部缓存
  70. /// </summary>
  71. public static void RemoveAllCache(this Cache cache)
  72. {
  73. IDictionaryEnumerator cacheEnum = cache.GetEnumerator();
  74. while (cacheEnum.MoveNext())
  75. {
  76. cache.Remove(cacheEnum.Key.ToString());
  77. }
  78. }
  79. #endregion
  80. private static SortedDictionary<string, object> dic = new SortedDictionary<string, object>();
  81. private static volatile Cache instance;
  82. private static readonly object LockHelper = new object();
  83. /// <summary>
  84. /// 添加缓存数据
  85. /// </summary>
  86. /// <param name="key">键</param>
  87. /// <param name="value">值</param>
  88. public static void Add(string key, object value)
  89. {
  90. dic.Add(key, value);
  91. }
  92. /// <summary>
  93. /// 缓存实例
  94. /// </summary>
  95. public static Cache Instance
  96. {
  97. get
  98. {
  99. if (instance == null)
  100. {
  101. lock (LockHelper)
  102. {
  103. if (instance == null)
  104. {
  105. instance = new Cache();
  106. }
  107. }
  108. }
  109. return instance;
  110. }
  111. }
  112. }
  113. }