CacheEntry.cs 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  1. //
  2. // System.Web.Caching
  3. //
  4. // Author:
  5. // Patrik Torstensson ([email protected])
  6. //
  7. // (C) Copyright Patrik Torstensson, 2001
  8. //
  9. namespace System.Web.Caching
  10. {
  11. /// <summary>
  12. /// Class responsible for representing a cache entry.
  13. /// </summary>
  14. public class CacheEntry
  15. {
  16. /// <summary>
  17. /// Defines the status of the current cache entry
  18. /// </summary>
  19. public enum Flags
  20. {
  21. Removed = 0,
  22. Public = 1
  23. }
  24. private CacheItemPriority _enumPriority;
  25. private long _longHits;
  26. private byte _byteExpiresBucket;
  27. private int _intExpiresIndex;
  28. private long _ticksExpires;
  29. private long _ticksSlidingExpiration;
  30. private string _strKey;
  31. private object _objItem;
  32. private long _longMinHits;
  33. private Flags _enumFlags;
  34. private CacheDependency _objDependency;
  35. private Cache _objCache;
  36. /// <summary>
  37. /// Constructs a new cache entry
  38. /// </summary>
  39. /// <param name="strKey">The cache key used to reference the item.</param>
  40. /// <param name="objItem">The item to be added to the cache.</param>
  41. /// <param name="objDependency">The file or cache key dependencies for the item. When any dependency changes, the object becomes invalid and is removed from the cache. If there are no dependencies, this paramter contains a null reference.</param>
  42. /// <param name="dtExpires">The time at which the added object expires and is removed from the cache. </param>
  43. /// <param name="tsSpan">The interval between the time the added object was last accessed and when that object expires. If this value is the equivalent of 20 minutes, the object expires and is removed from the cache 20 minutes after it is last accessed.</param>
  44. /// <param name="longMinHits">Used to detect and control if the item should be flushed due to under usage</param>
  45. /// <param name="boolPublic">Defines if the item is public or not</param>
  46. /// <param name="enumPriority">The relative cost of the object, as expressed by the CacheItemPriority enumeration. The cache uses this value when it evicts objects; objects with a lower cost are removed from the cache before objects with a higher cost.</param>
  47. public CacheEntry( Cache objManager, string strKey, object objItem, CacheDependency objDependency, CacheItemRemovedCallback eventRemove,
  48. System.DateTime dtExpires, System.TimeSpan tsSpan, long longMinHits, bool boolPublic, CacheItemPriority enumPriority )
  49. {
  50. if (boolPublic)
  51. {
  52. SetFlag(Flags.Public);
  53. }
  54. _strKey = strKey;
  55. _objItem = objItem;
  56. _objCache = objManager;
  57. _onRemoved += eventRemove;
  58. _enumPriority = enumPriority;
  59. _ticksExpires = dtExpires.Ticks;
  60. _ticksSlidingExpiration = tsSpan.Ticks;
  61. // If we have a sliding expiration it overrides the absolute expiration (MS behavior)
  62. if (tsSpan.Ticks != System.TimeSpan.Zero.Ticks)
  63. {
  64. _ticksExpires = System.DateTime.Now.AddTicks(_ticksSlidingExpiration).Ticks;
  65. }
  66. _objDependency = objDependency;
  67. if (_objDependency != null)
  68. {
  69. if (_objDependency.IsDisposed)
  70. {
  71. throw new System.ObjectDisposedException("System.Web.CacheDependency");
  72. }
  73. // Add the entry to the cache dependency handler (we support multiple entries per handler)
  74. _objDependency.Changed += new CacheDependency.CacheDependencyCallback(OnChanged);
  75. }
  76. _longMinHits = longMinHits;
  77. }
  78. private event CacheItemRemovedCallback _onRemoved;
  79. public void OnChanged(CacheDependency objDependency)
  80. {
  81. _objCache.Remove(_strKey, CacheItemRemovedReason.DependencyChanged);
  82. }
  83. /// <summary>
  84. /// Cleans up the cache entry, removes the cache dependency and calls the remove delegate.
  85. /// </summary>
  86. /// <param name="enumReason">The reason why the cache entry are going to be removed</param>
  87. public void Close(CacheItemRemovedReason enumReason)
  88. {
  89. lock(this)
  90. {
  91. // Check if the item already is removed
  92. if (TestFlag(Flags.Removed))
  93. {
  94. return;
  95. }
  96. SetFlag(Flags.Removed);
  97. if (_onRemoved != null)
  98. {
  99. // Call the delegate to tell that we are now removing the entry
  100. try
  101. {
  102. _onRemoved(_strKey, _objItem, enumReason);
  103. }
  104. catch (System.Exception objException)
  105. {
  106. System.Diagnostics.Debug.Fail("System.Web.CacheEntry.Close() Exception when calling remove delegate", "Message: " + objException.Message + " Stack: " + objException.StackTrace + " Source:" + objException.Source);
  107. }
  108. }
  109. // If we have a dependency, remove the entry
  110. if (_objDependency != null)
  111. {
  112. _objDependency.Changed -= new CacheDependency.CacheDependencyCallback(OnChanged);
  113. if (!_objDependency.HasEvents)
  114. {
  115. _objDependency.Dispose();
  116. }
  117. }
  118. }
  119. }
  120. /// <summary>
  121. /// Tests a specific flag is set or not.
  122. /// </summary>
  123. /// <param name="oFlag">Flag to test agains</param>
  124. /// <returns>Returns true if the flag is set.</returns>
  125. public bool TestFlag(Flags oFlag)
  126. {
  127. lock(this)
  128. {
  129. if ((_enumFlags & oFlag) != 0)
  130. {
  131. return true;
  132. }
  133. return false;
  134. }
  135. }
  136. /// <summary>
  137. /// Sets a specific flag.
  138. /// </summary>
  139. /// <param name="oFlag">Flag to set.</param>
  140. public void SetFlag(Flags oFlag)
  141. {
  142. lock (this)
  143. {
  144. _enumFlags |= oFlag;
  145. }
  146. }
  147. /// <summary>
  148. /// Returns true if the object has minimum hit usage flushing enabled.
  149. /// </summary>
  150. public bool HasUsage
  151. {
  152. get {
  153. if (_longMinHits == System.Int64.MaxValue)
  154. {
  155. return false;
  156. }
  157. return true;
  158. }
  159. }
  160. /// <summary>
  161. /// Returns true if the entry has absolute expiration.
  162. /// </summary>
  163. public bool HasAbsoluteExpiration
  164. {
  165. get
  166. {
  167. if (_ticksExpires == System.DateTime.MaxValue.Ticks)
  168. {
  169. return false;
  170. }
  171. return true;
  172. }
  173. }
  174. /// <summary>
  175. /// Returns true if the entry has sliding expiration enabled.
  176. /// </summary>
  177. public bool HasSlidingExpiration
  178. {
  179. get
  180. {
  181. if (_ticksSlidingExpiration == System.TimeSpan.Zero.Ticks)
  182. {
  183. return false;
  184. }
  185. return true;
  186. }
  187. }
  188. /// <summary>
  189. /// Gets and sets the current expires bucket the entry is active in.
  190. /// </summary>
  191. public byte ExpiresBucket
  192. {
  193. get
  194. {
  195. lock (this)
  196. {
  197. return _byteExpiresBucket;
  198. }
  199. }
  200. set
  201. {
  202. lock (this)
  203. {
  204. _byteExpiresBucket = ExpiresBucket;
  205. }
  206. }
  207. }
  208. /// <summary>
  209. /// Gets and sets the current index in the expires bucket of the current cache entry.
  210. /// </summary>
  211. public int ExpiresIndex
  212. {
  213. get
  214. {
  215. lock (this)
  216. {
  217. return _intExpiresIndex;
  218. }
  219. }
  220. set
  221. {
  222. lock (this)
  223. {
  224. _intExpiresIndex = ExpiresIndex;
  225. }
  226. }
  227. }
  228. /// <summary>
  229. /// Gets and sets the expiration of the cache entry.
  230. /// </summary>
  231. public long Expires
  232. {
  233. get
  234. {
  235. lock (this)
  236. {
  237. return _ticksExpires;
  238. }
  239. }
  240. set
  241. {
  242. lock (this)
  243. {
  244. _ticksExpires = Expires;
  245. }
  246. }
  247. }
  248. /// <summary>
  249. /// Gets the sliding expiration value. The return value is in ticks (since 0/0-01 in 100nanosec)
  250. /// </summary>
  251. public long SlidingExpiration
  252. {
  253. get
  254. {
  255. return _ticksSlidingExpiration;
  256. }
  257. }
  258. /// <summary>
  259. /// Returns the current cached item.
  260. /// </summary>
  261. public object Item
  262. {
  263. get
  264. {
  265. return _objItem;
  266. }
  267. }
  268. /// <summary>
  269. /// Returns the current cache identifier.
  270. /// </summary>
  271. public string Key
  272. {
  273. get
  274. {
  275. return _strKey;
  276. }
  277. }
  278. /// <summary>
  279. /// Gets and sets the current number of hits on the cache entry.
  280. /// </summary>
  281. public long Hits
  282. {
  283. // todo: Could be optimized by using interlocked methods..
  284. get
  285. {
  286. lock (this)
  287. {
  288. return _longHits;
  289. }
  290. }
  291. set
  292. {
  293. lock (this)
  294. {
  295. _longHits = Hits;
  296. }
  297. }
  298. }
  299. /// <summary>
  300. /// Returns minimum hits for the usage flushing rutine.
  301. /// </summary>
  302. public long MinimumHits
  303. {
  304. get
  305. {
  306. return _longMinHits;
  307. }
  308. }
  309. /// <summary>
  310. /// Returns the priority of the cache entry.
  311. /// </summary>
  312. public CacheItemPriority Priority
  313. {
  314. get
  315. {
  316. return _enumPriority;
  317. }
  318. }
  319. }
  320. }