WebCache.cs 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. using System;
  2. using System.Collections;
  3. using System.Web;
  4. using System.Web.Caching;
  5. namespace CB.Cache.WebCache
  6. {
  7. /// <summary>
  8. /// 默认的缓存策略类
  9. /// </summary>
  10. public class WebCache : ICacheStrategy
  11. {
  12. protected static volatile System.Web.Caching.Cache webCache = System.Web.HttpRuntime.Cache;
  13. /// <summary>
  14. /// 默认缓存存活期为3600秒(1小时)
  15. /// </summary>
  16. protected int _timeOut = 3600;
  17. //private static object syncObj = new object();
  18. /// <summary>
  19. /// 构造函数
  20. /// </summary>
  21. static WebCache() { }
  22. /// <summary>
  23. /// 设置到期相对时间[单位: 秒]
  24. /// </summary>
  25. public virtual int TimeOut
  26. {
  27. set { _timeOut = value > 0 ? value : 3600; }
  28. get { return _timeOut > 0 ? _timeOut : 3600; }
  29. }
  30. public static System.Web.Caching.Cache GetWebCacheObj
  31. {
  32. get { return webCache; }
  33. }
  34. /// <summary>
  35. /// 加入当前对象到缓存中
  36. /// </summary>
  37. /// <param name="objId">对象的键值</param>
  38. /// <param name="o">缓存的对象</param>
  39. public virtual void AddObject(string objId, object o)
  40. {
  41. if (objId == null || objId.Length == 0 || o == null)
  42. {
  43. return;
  44. }
  45. webCache.Insert(objId, o, null, DateTime.Now.AddSeconds(TimeOut), System.Web.Caching.Cache.NoSlidingExpiration, System.Web.Caching.CacheItemPriority.High, null);
  46. }
  47. /// <summary>
  48. /// 加入当前对象到缓存中
  49. /// </summary>
  50. /// <param name="objId">对象的键值</param>
  51. /// <param name="o">缓存的对象</param>
  52. /// <param name="o">到期时间,单位:秒</param>
  53. public virtual void AddObject(string objId, object o, int expire)
  54. {
  55. if (objId == null || objId.Length == 0 || o == null)
  56. {
  57. return;
  58. }
  59. //表示永不过期
  60. if (expire == 0)
  61. {
  62. webCache.Insert(objId, o, null, DateTime.MaxValue, TimeSpan.Zero, System.Web.Caching.CacheItemPriority.High, null);
  63. }
  64. else
  65. {
  66. webCache.Insert(objId, o, null, DateTime.Now.AddSeconds(expire), System.Web.Caching.Cache.NoSlidingExpiration, System.Web.Caching.CacheItemPriority.High, null);
  67. }
  68. }
  69. /// <summary>
  70. /// 加入当前对象到缓存中,并对相关文件建立依赖
  71. /// </summary>
  72. /// <param name="objId">对象的键值</param>
  73. /// <param name="o">缓存的对象</param>
  74. /// <param name="files">监视的路径文件</param>
  75. public virtual void AddObjectWithFileChange(string objId, object o, string[] files)
  76. {
  77. if (objId == null || objId.Length == 0 || o == null)
  78. {
  79. return;
  80. }
  81. CacheItemRemovedCallback callBack = new CacheItemRemovedCallback(onRemove);
  82. CacheDependency dep = new CacheDependency(files, DateTime.Now);
  83. webCache.Insert(objId, o, dep, System.DateTime.Now.AddSeconds(TimeOut), System.Web.Caching.Cache.NoSlidingExpiration, System.Web.Caching.CacheItemPriority.High, callBack);
  84. }
  85. /// <summary>
  86. /// 加入当前对象到缓存中,并使用依赖键
  87. /// </summary>
  88. /// <param name="objId">对象的键值</param>
  89. /// <param name="o">缓存的对象</param>
  90. /// <param name="dependKey">依赖关联的键值</param>
  91. public virtual void AddObjectWithDepend(string objId, object o, string[] dependKey)
  92. {
  93. if (objId == null || objId.Length == 0 || o == null)
  94. {
  95. return;
  96. }
  97. CacheItemRemovedCallback callBack = new CacheItemRemovedCallback(onRemove);
  98. CacheDependency dep = new CacheDependency(null, dependKey, DateTime.Now);
  99. webCache.Insert(objId, o, dep, System.DateTime.Now.AddSeconds(TimeOut), System.Web.Caching.Cache.NoSlidingExpiration, System.Web.Caching.CacheItemPriority.High, callBack);
  100. }
  101. /// <summary>
  102. /// 建立回调委托的一个实例
  103. /// </summary>
  104. /// <param name="key"></param>
  105. /// <param name="val"></param>
  106. /// <param name="reason"></param>
  107. public void onRemove(string key, object val, CacheItemRemovedReason reason)
  108. {
  109. switch (reason)
  110. {
  111. case CacheItemRemovedReason.DependencyChanged:
  112. break;
  113. case CacheItemRemovedReason.Expired:
  114. {
  115. //CacheItemRemovedCallback callBack = new CacheItemRemovedCallback(this.onRemove);
  116. //webCache.Insert(key, val, null, System.DateTime.Now.AddMinutes(TimeOut),
  117. // System.Web.Caching.Cache.NoSlidingExpiration,
  118. // System.Web.Caching.CacheItemPriority.High,
  119. // callBack);
  120. break;
  121. }
  122. case CacheItemRemovedReason.Removed:
  123. {
  124. break;
  125. }
  126. case CacheItemRemovedReason.Underused:
  127. {
  128. break;
  129. }
  130. default: break;
  131. }
  132. }
  133. /// <summary>
  134. /// 删除缓存对象
  135. /// </summary>
  136. /// <param name="objId">对象的关键字</param>
  137. public virtual void RemoveObject(string objId)
  138. {
  139. if (objId == null || objId.Length == 0)
  140. {
  141. return;
  142. }
  143. webCache.Remove(objId);
  144. }
  145. /// <summary>
  146. /// 返回一个指定的对象
  147. /// </summary>
  148. /// <param name="objId">对象的关键字</param>
  149. /// <returns>对象</returns>
  150. public virtual object GetObject(string objId)
  151. {
  152. if (objId == null || objId.Length == 0)
  153. {
  154. return null;
  155. }
  156. return webCache.Get(objId);
  157. }
  158. /// <summary>
  159. /// 清空的有缓存数据
  160. /// </summary>
  161. public virtual void FlushAll()
  162. {
  163. IDictionaryEnumerator CacheEnum = HttpRuntime.Cache.GetEnumerator();
  164. while (CacheEnum.MoveNext())
  165. {
  166. webCache.Remove(CacheEnum.Key.ToString());
  167. }
  168. }
  169. }
  170. }