CacheManager.cs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Data;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. using System.Web;
  9. namespace Ant.Service.Utility
  10. {
  11. /// <summary>
  12. /// 缓存管理
  13. /// 将用户凭证、令牌的关系数据存放于Cache中
  14. /// </summary>
  15. public class CacheManager
  16. {
  17. private volatile static CacheManager instance = null;
  18. private static object locker = new Object();
  19. public static CacheManager Instance
  20. {
  21. get
  22. {
  23. if (instance == null)
  24. {
  25. lock (locker)
  26. {
  27. if (instance == null)
  28. {
  29. instance = new CacheManager();
  30. }
  31. }
  32. }
  33. return instance;
  34. }
  35. }
  36. /// <summary>
  37. /// 初始化数据结构
  38. /// </summary>
  39. /// <remarks>
  40. /// ----------------------------------------------------
  41. /// | token(令牌) | info(用户凭证) | timeout(过期时间) |
  42. /// |--------------------------------------------------|
  43. /// </remarks>
  44. private static void cacheInit(string token, object obj, double timeout)
  45. {
  46. if (HttpContext.Current.Cache[token] == null)
  47. {
  48. if (timeout == 0 || timeout == null)
  49. {
  50. timeout = double.Parse(System.Configuration.ConfigurationManager.AppSettings["timeout"]);
  51. }
  52. //HttpContext.Current.Cache.Insert(UserName, dt, null, DateTime.Now.AddMinutes(timeout), TimeSpan.Zero);//TimeSpan.Zero表示不使用平滑过期策略
  53. //HttpContext.Current.Cache.Insert(UserName, dt, null, DateTime.MaxValue, TimeSpan.FromMinutes(timeout));//表示缓存连续30分钟没有访问就过期。
  54. HttpContext.Current.Cache.Insert(token, obj, null, System.Web.Caching.Cache.NoAbsoluteExpiration, TimeSpan.FromMinutes(timeout));//表示缓存连续30分钟没有访问就过期。
  55. }
  56. }
  57. /// <summary>
  58. /// 判断令牌是否存在
  59. /// </summary>
  60. /// <param name="token">令牌</param>
  61. /// <returns></returns>
  62. public static bool TokenIsExist(string token)
  63. {
  64. if (token.IsEmpty())
  65. {
  66. return false;
  67. }
  68. else
  69. {
  70. object dt = HttpContext.Current.Cache[token];
  71. if (dt == null)
  72. return false;
  73. else
  74. return true;
  75. }
  76. }
  77. /// <summary>
  78. /// 通过令牌获取缓存的值
  79. /// </summary>
  80. /// <param name="token">令牌</param>
  81. /// <returns></returns>
  82. public static object GetTokenValue(string token)
  83. {
  84. object dt = HttpContext.Current.Cache[token];
  85. return dt;
  86. }
  87. /// <summary>
  88. /// 获取所有列表数据
  89. /// </summary>
  90. /// <returns></returns>
  91. public static List<string> GetAllList()
  92. {
  93. List<string> cacheKeys = new List<string>();
  94. Dictionary<string, object> dictionary = new Dictionary<string, object>();
  95. IDictionaryEnumerator CacheEnum = HttpContext.Current.Cache.GetEnumerator();
  96. while (CacheEnum.MoveNext())
  97. {
  98. string token = CacheEnum.Key.ToString();
  99. cacheKeys.Add(token);
  100. }
  101. return cacheKeys;
  102. }
  103. /// <summary>
  104. /// 判断令牌是否存在
  105. /// </summary>
  106. /// <param name="token">令牌</param>
  107. /// <returns></returns>
  108. public static bool TokenClear(string token)
  109. {
  110. object obj = HttpContext.Current.Cache.Remove(token);
  111. if (obj == null)
  112. return false;
  113. else
  114. return true;
  115. }
  116. /// <returns></returns>
  117. /// <summary>
  118. /// 移除全部缓存
  119. /// </summary>
  120. public static void RemoveAllCache()
  121. {
  122. System.Web.Caching.Cache _cache = HttpContext.Current.Cache;
  123. IDictionaryEnumerator CacheEnum = _cache.GetEnumerator();
  124. while (CacheEnum.MoveNext())
  125. {
  126. _cache.Remove(CacheEnum.Key.ToString());
  127. }
  128. }
  129. /// <summary>
  130. /// 更新令牌过期时间
  131. /// </summary>
  132. /// <param name="token">令牌</param>
  133. /// <param name="time">过期时间</param>
  134. public static void TokenTimeUpdate(string token, DateTime time)
  135. {
  136. DataTable dt = (DataTable)HttpContext.Current.Cache["CERT"];
  137. DataRow[] dr = dt.Select("token = '" + token + "'");
  138. if (dr.Length > 0)
  139. {
  140. dr[0]["timeout"] = time;
  141. }
  142. }
  143. /// <summary>
  144. /// 添加令牌
  145. /// </summary>
  146. /// <param name="token">令牌</param>
  147. /// <param name="info">凭证</param>
  148. /// <param name="timeout">过期时间</param>
  149. public static void TokenInsert(string token, object info, double timeout = 0)
  150. {
  151. if (TokenIsExist(token))
  152. {
  153. HttpContext.Current.Cache[token] = info;//凭证存则替换缓存内容
  154. //FieldValueCollection.Instance[token] = username;
  155. }
  156. else
  157. {
  158. //FieldValueCollection.Instance.Add(token, username);
  159. cacheInit(token, info, timeout);
  160. }
  161. }
  162. }//end class
  163. }