鄂尔多斯-招源科技

CacheHelper.cs 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. /// <returns>object对象</returns>
  65. public static object Get(string key)
  66. {
  67. if (string.IsNullOrEmpty(key))
  68. {
  69. return null;
  70. }
  71. return HttpContext.Current.Cache.Get(key);
  72. }
  73. /// <summary>
  74. /// 获取缓存对象
  75. /// </summary>
  76. /// <typeparam name="T">T对象</typeparam>
  77. /// <param name="key">缓存Key</param>
  78. /// <returns></returns>
  79. public static T Get<T>(string key)
  80. {
  81. object obj = Get(key);
  82. return obj == null ? default(T) : (T)obj;
  83. }
  84. /// <summary>
  85. /// 移出Cache对象
  86. /// </summary>
  87. /// <param name="CacheKey"></param>
  88. public static void Remove(string Key)
  89. {
  90. System.Web.Caching.Cache objCache = HttpRuntime.Cache;
  91. objCache.Remove(Key);
  92. }
  93. /// <summary>
  94. /// 移除所有Cache对象
  95. /// </summary>
  96. public static void RemoveAll()
  97. {
  98. System.Web.Caching.Cache cache = HttpRuntime.Cache;
  99. IDictionaryEnumerator cacheEnum = cache.GetEnumerator();
  100. ArrayList al = new ArrayList();
  101. while (cacheEnum.MoveNext())
  102. {
  103. al.Add(cacheEnum.Key);
  104. }
  105. foreach (string key in al)
  106. {
  107. cache.Remove(key);
  108. }
  109. }
  110. }
  111. }