You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

150 lines
6.0 KiB

2 years ago
  1. using System;
  2. using System.Web;
  3. using System.Web.Caching;
  4. namespace EasyBL
  5. {
  6. /// <summary>
  7. /// HttpRuntime Cache读取设置缓存信息封装 <auther><name>John</name><date>20185-8-11</date></auther>
  8. /// 使用描述:给缓存赋值使用HttpRuntimeCache.Set(key,value....)等参数(第三个参数可以传递文件的路径(HttpContext.Current.Server.MapPath()))
  9. /// 读取缓存中的值使用JObject jObject=HttpRuntimeCache.Get(key) as JObject,读取到值之后就可以进行一系列判断
  10. /// </summary>
  11. public static class HttpRuntimeCache
  12. {
  13. /// <summary>
  14. /// 设置缓存时间,配置(从配置文件中读取)
  15. /// </summary>
  16. private const double Seconds = 30 * 24 * 60 * 60;
  17. /// <summary>
  18. /// 缓存指定对象,设置缓存
  19. /// </summary>
  20. /// <param name="key">todo: describe key parameter on Set</param>
  21. /// <param name="value">todo: describe value parameter on Set</param>
  22. public static bool Set(string key, object value)
  23. {
  24. return Set(key, value, null, DateTime.Now.AddSeconds(Seconds), Cache.NoSlidingExpiration, CacheItemPriority.Default, null);
  25. }
  26. /// <summary>
  27. /// 缓存指定对象,设置缓存
  28. /// </summary>
  29. /// <param name="key">todo: describe key parameter on Set</param>
  30. /// <param name="value">todo: describe value parameter on Set</param>
  31. /// <param name="path">todo: describe path parameter on Set</param>
  32. public static bool Set(string key, object value, string path)
  33. {
  34. try
  35. {
  36. var cacheDependency = new CacheDependency(path);
  37. return Set(key, value, cacheDependency);
  38. }
  39. catch (Exception)
  40. {
  41. return false;
  42. }
  43. }
  44. /// <summary>
  45. /// 缓存指定对象,设置缓存
  46. /// </summary>
  47. /// <param name="key">todo: describe key parameter on Set</param>
  48. /// <param name="value">todo: describe value parameter on Set</param>
  49. /// <param name="cacheDependency">todo: describe cacheDependency parameter on Set</param>
  50. public static bool Set(string key, object value, CacheDependency cacheDependency)
  51. {
  52. return Set(key, value, cacheDependency, Cache.NoAbsoluteExpiration, Cache.NoSlidingExpiration, CacheItemPriority.Default, null);
  53. }
  54. /// <summary>
  55. /// 缓存指定对象,设置缓存
  56. /// </summary>
  57. /// <param name="key">todo: describe key parameter on Set</param>
  58. /// <param name="value">todo: describe value parameter on Set</param>
  59. /// <param name="seconds">todo: describe seconds parameter on Set</param>
  60. /// <param name="isAbsulute">todo: describe isAbsulute parameter on Set</param>
  61. public static bool Set(string key, object value, double seconds, bool isAbsulute)
  62. {
  63. return Set(key, value, null, (isAbsulute ? DateTime.Now.AddSeconds(seconds) : Cache.NoAbsoluteExpiration),
  64. (isAbsulute ? Cache.NoSlidingExpiration : TimeSpan.FromSeconds(seconds)), CacheItemPriority.Default, null);
  65. }
  66. /// <summary>
  67. /// 获取缓存对象
  68. /// </summary>
  69. /// <param name="key">todo: describe key parameter on Get</param>
  70. public static object Get(string key)
  71. {
  72. return GetPrivate(key);
  73. }
  74. /// <summary>
  75. /// 判断缓存中是否含有缓存该键
  76. /// </summary>
  77. /// <param name="key">todo: describe key parameter on Exists</param>
  78. public static bool Exists(string key)
  79. {
  80. return (GetPrivate(key) != null);
  81. }
  82. /// <summary>
  83. /// 移除缓存对象
  84. /// </summary>
  85. /// <param name="key"></param>
  86. /// <returns></returns>
  87. public static bool Remove(string key)
  88. {
  89. if (string.IsNullOrEmpty(key))
  90. {
  91. return false;
  92. }
  93. HttpRuntime.Cache.Remove(key);
  94. return true;
  95. }
  96. /// <summary>
  97. /// 移除所有缓存
  98. /// </summary>
  99. /// <returns></returns>
  100. public static bool RemoveAll()
  101. {
  102. var iDictionaryEnumerator = HttpRuntime.Cache.GetEnumerator();
  103. while (iDictionaryEnumerator.MoveNext())
  104. {
  105. HttpRuntime.Cache.Remove(Convert.ToString(iDictionaryEnumerator.Key));
  106. }
  107. return true;
  108. }
  109. /// <summary>
  110. /// 设置缓存
  111. /// </summary>
  112. /// <param name="key">todo: describe key parameter on Set</param>
  113. /// <param name="value">todo: describe value parameter on Set</param>
  114. /// <param name="cacheDependency">todo: describe cacheDependency parameter on Set</param>
  115. /// <param name="dateTime">todo: describe dateTime parameter on Set</param>
  116. /// <param name="timeSpan">todo: describe timeSpan parameter on Set</param>
  117. /// <param name="cacheItemPriority">todo: describe cacheItemPriority parameter on Set</param>
  118. /// <param name="cacheItemRemovedCallback">
  119. /// todo: describe cacheItemRemovedCallback parameter on Set
  120. /// </param>
  121. public static bool Set(string key, object value, CacheDependency cacheDependency, DateTime dateTime,
  122. TimeSpan timeSpan, CacheItemPriority cacheItemPriority, CacheItemRemovedCallback cacheItemRemovedCallback)
  123. {
  124. if (string.IsNullOrEmpty(key) || value == null)
  125. {
  126. return false;
  127. }
  128. HttpRuntime.Cache.Insert(key, value, cacheDependency, dateTime, timeSpan, cacheItemPriority, cacheItemRemovedCallback);
  129. return true;
  130. }
  131. /// <summary>
  132. /// 获取缓存
  133. /// </summary>
  134. /// <param name="key">todo: describe key parameter on GetPrivate</param>
  135. private static object GetPrivate(string key)
  136. {
  137. return string.IsNullOrEmpty(key) ? null : HttpRuntime.Cache.Get(key);
  138. }
  139. }
  140. }