县级监管平台

CacheHelper.cs 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. using System.Threading.Tasks;
  7. using System.Web;
  8. using System.Web.Caching;
  9. namespace CallCenterApi.Common
  10. {
  11. public class CacheHelper
  12. {
  13. /// <summary>
  14. /// 创建缓存项的文件依赖
  15. /// </summary>
  16. /// <param name="key">缓存Key</param>
  17. /// <param name="obj">object对象</param>
  18. /// <param name="fileName">文件绝对路径</param>
  19. public static void Insert(string key, object obj, string fileName)
  20. {
  21. //创建缓存依赖项
  22. CacheDependency dep = new CacheDependency(fileName);
  23. //创建缓存
  24. HttpContext.Current.Cache.Insert(key, obj, dep);
  25. }
  26. /// <summary>
  27. /// 创建缓存,无过期时间
  28. /// </summary>
  29. /// <param name="key"></param>
  30. /// <param name="obj"></param>
  31. public static void Insert(string key, object obj)
  32. {
  33. HttpContext.Current.Cache.Insert(key, obj);
  34. }
  35. /// <summary>
  36. /// 创建缓存项过期
  37. /// </summary>
  38. /// <param name="key">缓存Key</param>
  39. /// <param name="obj">object对象</param>
  40. /// <param name="expires">过期时间(分钟)</param>
  41. public static void Insert(string key, object obj, int expires)
  42. {
  43. //最后一次访问开始计算
  44. //HttpContext.Current.Cache.Insert(key, obj, null, Cache.NoAbsoluteExpiration, new TimeSpan(0, expires, 0));
  45. //从设置时开始计算
  46. HttpContext.Current.Cache.Insert(key, obj, null, DateTime.UtcNow.AddMinutes(expires), Cache.NoSlidingExpiration);
  47. }
  48. /// <summary>
  49. /// 创建缓存项过期
  50. /// </summary>
  51. /// <param name="key">缓存Key</param>
  52. /// <param name="obj">object对象</param>
  53. /// <param name="expires">过期时间(分钟)</param>
  54. /// <param name="priority">缓存清理优先级</param>
  55. public static void Insert(string key, object obj, int expires, CacheItemPriority priority)
  56. {
  57. //从设置时开始计算
  58. HttpContext.Current.Cache.Insert(key, obj, null, DateTime.UtcNow.AddMinutes(expires), Cache.NoSlidingExpiration, priority, null);
  59. }
  60. /// <summary>
  61. /// 创建缓存项过期
  62. /// </summary>
  63. /// <param name="key">缓存Key</param>
  64. /// <param name="obj">object对象</param>
  65. /// <param name="expires">过期时间(分钟)</param>
  66. /// <param name="priority">缓存清理优先级</param>
  67. public static void Insert(string key, object obj, int expires, CacheItemPriority priority, CacheItemRemovedCallback onRemoveCallback)
  68. {
  69. //从设置时开始计算
  70. HttpContext.Current.Cache.Insert(key, obj, null, DateTime.UtcNow.AddMinutes(expires), Cache.NoSlidingExpiration, priority, onRemoveCallback);
  71. }
  72. /// <summary>
  73. /// 获取缓存对象
  74. /// </summary>
  75. /// <param name="key">缓存Key</param>
  76. /// <returns>object对象</returns>
  77. public static object Get(string key)
  78. {
  79. if (string.IsNullOrEmpty(key))
  80. {
  81. return null;
  82. }
  83. return HttpContext.Current.Cache.Get(key);
  84. }
  85. /// <summary>
  86. /// 获取缓存对象
  87. /// </summary>
  88. /// <typeparam name="T">T对象</typeparam>
  89. /// <param name="key">缓存Key</param>
  90. /// <returns></returns>
  91. public static T Get<T>(string key)
  92. {
  93. object obj = Get(key);
  94. return obj == null ? default(T) : (T)obj;
  95. }
  96. /// <summary>
  97. /// 移出Cache对象
  98. /// </summary>
  99. /// <param name="CacheKey"></param>
  100. public static void Remove(string Key)
  101. {
  102. System.Web.Caching.Cache objCache = HttpRuntime.Cache;
  103. objCache.Remove(Key);
  104. }
  105. /// <summary>
  106. /// 移除所有Cache对象
  107. /// </summary>
  108. public static void RemoveAll()
  109. {
  110. System.Web.Caching.Cache cache = HttpRuntime.Cache;
  111. IDictionaryEnumerator cacheEnum = cache.GetEnumerator();
  112. ArrayList al = new ArrayList();
  113. while (cacheEnum.MoveNext())
  114. {
  115. al.Add(cacheEnum.Key);
  116. }
  117. foreach (string key in al)
  118. {
  119. cache.Remove(key);
  120. }
  121. }
  122. }
  123. }