WebCachE.cs 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Configuration;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. using System.Web;
  9. using System.Web.Caching;
  10. namespace Cache.WebCache
  11. {
  12. /// <summary>
  13. /// 默认的.net WebCache接口实现
  14. /// </summary>
  15. public class WebCache
  16. {
  17. protected static volatile System.Web.Caching.Cache webCache = HttpRuntime.Cache;
  18. /// <summary>
  19. /// 判断值是否存在..
  20. /// </summary>
  21. /// <param name="key"></param>
  22. /// <returns></returns>
  23. public bool IsExist(string key)
  24. {
  25. if (string.IsNullOrEmpty(key))
  26. return false;
  27. return webCache[key] != null;
  28. }
  29. /// <summary>
  30. /// 写入某个Cache
  31. /// </summary>
  32. /// <param name="key">键</param>
  33. /// <param name="o">对像</param>
  34. public void AddObject<T>(string key, T o, int t = 0)
  35. {
  36. if (String.IsNullOrEmpty(key) || o == null)
  37. return;
  38. if (t == 0)
  39. t = int.Parse(ConfigurationManager.AppSettings["OutTime"]);
  40. CacheItemRemovedCallback callBack = new CacheItemRemovedCallback(onRemove);
  41. webCache.Insert(key, o, null, System.DateTime.Now.AddMinutes(t), System.Web.Caching.Cache.NoSlidingExpiration, System.Web.Caching.CacheItemPriority.High, callBack);
  42. }
  43. /// <summary>
  44. /// 取某个key对应的object值
  45. /// </summary>
  46. /// <param name="key">键</param>
  47. /// <returns></returns>
  48. public T GetObject<T>(string key)
  49. {
  50. if (!String.IsNullOrEmpty(key))
  51. return (T)webCache.Get(key);
  52. return default(T);
  53. }
  54. /// <summary>
  55. /// 移出某个Cache
  56. /// </summary>
  57. /// <param name="key">键</param>
  58. public void RemoveObject(string key)
  59. {
  60. if (!String.IsNullOrEmpty(key))
  61. webCache.Remove(key);
  62. }
  63. /// <summary>
  64. /// 批量移出类型key的对像
  65. /// </summary>
  66. /// <param name="key"></param>
  67. public void RemoveObjectByRegex(string pattern)
  68. {
  69. if (!string.IsNullOrEmpty(pattern))
  70. {
  71. IDictionaryEnumerator enu = webCache.GetEnumerator();
  72. while (enu.MoveNext())
  73. {
  74. string key = enu.Key.ToString();
  75. if (key.IndexOf(pattern, StringComparison.CurrentCultureIgnoreCase) != -1)
  76. {
  77. RemoveObject(key);
  78. }
  79. }
  80. }
  81. }
  82. /// <summary>
  83. /// 移出所有Cache
  84. /// </summary>
  85. public void RemoveAll()
  86. {
  87. IDictionaryEnumerator enu = webCache.GetEnumerator();
  88. while (enu.MoveNext())
  89. {
  90. RemoveObject(enu.Key.ToString());
  91. }
  92. }
  93. public void onRemove(string key, object val, CacheItemRemovedReason reason)
  94. {
  95. switch (reason)
  96. {
  97. case CacheItemRemovedReason.DependencyChanged:
  98. //更新缓存映射表
  99. break;
  100. case CacheItemRemovedReason.Expired:
  101. //更新缓存映射表
  102. break;
  103. case CacheItemRemovedReason.Removed:
  104. break;
  105. case CacheItemRemovedReason.Underused:
  106. break;
  107. default:
  108. break;
  109. }
  110. }
  111. /// <summary>
  112. /// 返回所有键值
  113. /// </summary>
  114. /// <returns></returns>
  115. public List<string> GetKeys()
  116. {
  117. List<string> list = new List<string>();
  118. IDictionaryEnumerator enu = webCache.GetEnumerator();
  119. while (enu.MoveNext())
  120. {
  121. list.Add(enu.Key.ToString());
  122. }
  123. return list;
  124. }
  125. }
  126. }