BaseCache.cs 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Web;
  4. using System.Web.Caching;
  5. namespace CP.Cache
  6. {
  7. public class BaseCache
  8. {
  9. protected static volatile System.Web.Caching.Cache cache = HttpRuntime.Cache;
  10. /// <summary>
  11. /// cache是否存在
  12. /// </summary>
  13. /// <param name="k"></param>
  14. /// <returns></returns>
  15. public static bool IsExist(string k)
  16. {
  17. if (string.IsNullOrEmpty(k))
  18. return false;
  19. return cache[k] != null;
  20. }
  21. /// <summary>
  22. /// 获取cache
  23. /// </summary>
  24. /// <typeparam name="T"></typeparam>
  25. /// <param name="k"></param>
  26. /// <returns></returns>
  27. public static T Get<T>(string k)
  28. {
  29. if (!string.IsNullOrEmpty(k) && cache[k] != null)
  30. {
  31. return (T)cache.Get(k);
  32. }
  33. return default(T);
  34. }
  35. /// <summary>
  36. /// 添加cache
  37. /// </summary>
  38. /// <typeparam name="T"></typeparam>
  39. /// <param name="k">key</param>
  40. /// <param name="v">value</param>
  41. /// <param name="m">绝对过期时间</param>
  42. public static void Add<T>(string k,T v,int m = 0)
  43. {
  44. if(!string.IsNullOrEmpty(k) && v != null)
  45. {
  46. if (m > 0)
  47. {
  48. cache.Insert(k, v, null, DateTime.Now.AddMinutes(m), System.Web.Caching.Cache.NoSlidingExpiration);
  49. }
  50. else
  51. {
  52. cache.Insert(k, v);
  53. }
  54. }
  55. }
  56. /// <summary>
  57. /// 移出Cache
  58. /// </summary>
  59. /// <param name="k"></param>
  60. public static void Remove(string k)
  61. {
  62. if(IsExist(k))
  63. {
  64. cache.Remove(k);
  65. }
  66. }
  67. /// <summary>
  68. /// 批量移出cache
  69. /// </summary>
  70. /// <param name="k"></param>
  71. public static void RemoveList(string k)
  72. {
  73. if (!string.IsNullOrEmpty(k))
  74. {
  75. var e = cache.GetEnumerator();
  76. while (e.MoveNext())
  77. {
  78. if (e.Key.ToString().IndexOf(k, StringComparison.OrdinalIgnoreCase) != -1)
  79. {
  80. Remove(e.Key.ToString());
  81. }
  82. }
  83. }
  84. }
  85. /// <summary>
  86. /// 移出所有Cache
  87. /// </summary>
  88. public static void RemoveAll()
  89. {
  90. var e = cache.GetEnumerator();
  91. while (e.MoveNext())
  92. {
  93. Remove(e.Key.ToString());
  94. }
  95. }
  96. /// <summary>
  97. /// 获取所有key
  98. /// </summary>
  99. /// <returns></returns>
  100. public static List<string> GetKeys()
  101. {
  102. List<string> list = new List<string>();
  103. var e = cache.GetEnumerator();
  104. while (e.MoveNext())
  105. {
  106. list.Add(e.Key.ToString());
  107. }
  108. return list;
  109. }
  110. /// <summary>
  111. /// cache总数
  112. /// </summary>
  113. /// <returns></returns>
  114. public static int Count()
  115. {
  116. return cache.Count;
  117. }
  118. }
  119. }