WebCache.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. using Common;
  2. using System;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. using System.Web;
  9. using System.Web.Caching;
  10. namespace KC.Cache.WebCache
  11. {
  12. public class WebCache:ICache
  13. {
  14. protected static volatile System.Web.Caching.Cache webCache = HttpRuntime.Cache;
  15. private static int timeOut = ConfigHelper.GetValue("OutTime").TryToInt32();
  16. /// <summary>
  17. /// 写入缓存,单体
  18. /// </summary>
  19. /// <param name="value">对象数据</param>
  20. /// <param name="cacheKey">键</param>
  21. /// <param name="seconds">几秒过期</param>
  22. public void WriteCache<T>(string cacheKey, T value, int seconds = 0) where T : class
  23. {
  24. if (String.IsNullOrEmpty(cacheKey) || value == null)
  25. return;
  26. if (seconds == 0)
  27. seconds = timeOut;
  28. var callBack = new CacheItemRemovedCallback(onRemove);
  29. webCache.Insert(cacheKey, value, null, System.DateTime.Now.AddSeconds(seconds), System.Web.Caching.Cache.NoSlidingExpiration, CacheItemPriority.High, callBack);
  30. }
  31. /// <summary>
  32. /// 读取缓存,单体
  33. /// </summary>
  34. /// <param name="cacheKey">键</param>
  35. /// <returns></returns>
  36. public T GetCache<T>(string cacheKey) where T : class
  37. {
  38. if (!String.IsNullOrEmpty(cacheKey))
  39. return (T)webCache.Get(cacheKey);
  40. return default(T);
  41. }
  42. /// <summary>
  43. /// 获取所有key
  44. /// </summary>
  45. /// <returns></returns>
  46. public List<string> GetAllKeys()
  47. {
  48. List<string> list = new List<string>();
  49. var enu = webCache.GetEnumerator();
  50. while (enu.MoveNext())
  51. {
  52. list.Add(enu.Key.ToString());
  53. }
  54. return list;
  55. }
  56. /// <summary>
  57. /// 确定当前Key是否过期
  58. /// </summary>
  59. /// <param name="key"></param>
  60. /// <returns></returns>
  61. public bool HasExpire(string cacheKey)
  62. {
  63. if (string.IsNullOrEmpty(cacheKey))
  64. return false;
  65. return webCache[cacheKey] != null;
  66. }
  67. /// <summary>
  68. /// 移除指定数据缓存
  69. /// </summary>
  70. /// <param name="cacheKey">键</param>
  71. public void RemoveCache(string cacheKey)
  72. {
  73. if (!String.IsNullOrEmpty(cacheKey))
  74. webCache.Remove(cacheKey);
  75. }
  76. /// <summary>
  77. /// 批量删除数据缓存
  78. /// </summary>
  79. /// <param name="keys"></param>
  80. public void RemoveCache(List<string> keys)
  81. {
  82. if (keys.Count > 0)
  83. {
  84. var enu = webCache.GetEnumerator();
  85. while (enu.MoveNext())
  86. {
  87. string key = enu.Key.ToString();
  88. if (keys.Where(p => p == key).Count() > 0)
  89. {
  90. RemoveCache(key);
  91. }
  92. }
  93. }
  94. }
  95. #region MyRegion
  96. public void onRemove(string key, object val, CacheItemRemovedReason reason)
  97. {
  98. switch (reason)
  99. {
  100. case CacheItemRemovedReason.DependencyChanged:
  101. //更新缓存映射表
  102. break;
  103. case CacheItemRemovedReason.Expired:
  104. //更新缓存映射表
  105. break;
  106. case CacheItemRemovedReason.Removed:
  107. break;
  108. case CacheItemRemovedReason.Underused:
  109. break;
  110. default:
  111. break;
  112. }
  113. }
  114. #endregion
  115. }
  116. }