CacheEntry.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445
  1. //
  2. // System.Web.Caching
  3. //
  4. // Author:
  5. // Patrik Torstensson ([email protected])
  6. // Changes:
  7. // Daniel Cazzulino [DHC] ([email protected])
  8. //
  9. // (C) Copyright Patrik Torstensson, 2001
  10. //
  11. namespace System.Web.Caching
  12. {
  13. /// <summary>
  14. /// Class responsible for representing a cache entry.
  15. /// </summary>
  16. public class CacheEntry
  17. {
  18. /// <summary>
  19. /// Defines the status of the current cache entry
  20. /// </summary>
  21. public enum Flags
  22. {
  23. Removed = 0,
  24. Public = 1
  25. }
  26. private CacheItemPriority _enumPriority;
  27. private long _longHits;
  28. private byte _byteExpiresBucket;
  29. private int _intExpiresIndex;
  30. private long _ticksExpires;
  31. private long _ticksSlidingExpiration;
  32. private string _strKey;
  33. private object _objItem;
  34. private long _longMinHits;
  35. private Flags _enumFlags;
  36. private CacheDependency _objDependency;
  37. private Cache _objCache;
  38. /// <summary>
  39. /// The item is not placed in a bucket. [DHC]
  40. /// </summary>
  41. public static readonly byte NoBucketHash = byte.MaxValue;
  42. /// <summary>
  43. /// The item is not placed in a bucket. [DHC]
  44. /// </summary>
  45. public static readonly int NoIndexInBucket = int.MaxValue;
  46. /// <summary>
  47. /// Lock for syncronized operations. [DHC]
  48. /// </summary>
  49. System.Threading.ReaderWriterLock _lock = new System.Threading.ReaderWriterLock();
  50. /// <summary>
  51. /// Constructs a new cache entry
  52. /// </summary>
  53. /// <param name="strKey">The cache key used to reference the item.</param>
  54. /// <param name="objItem">The item to be added to the cache.</param>
  55. /// <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>
  56. /// <param name="dtExpires">The time at which the added object expires and is removed from the cache. </param>
  57. /// <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>
  58. /// <param name="longMinHits">Used to detect and control if the item should be flushed due to under usage</param>
  59. /// <param name="boolPublic">Defines if the item is public or not</param>
  60. /// <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>
  61. public CacheEntry( Cache objManager, string strKey, object objItem, CacheDependency objDependency, CacheItemRemovedCallback eventRemove,
  62. System.DateTime dtExpires, System.TimeSpan tsSpan, long longMinHits, bool boolPublic, CacheItemPriority enumPriority )
  63. {
  64. if (boolPublic)
  65. {
  66. SetFlag(Flags.Public);
  67. }
  68. _strKey = strKey;
  69. _objItem = objItem;
  70. _objCache = objManager;
  71. _onRemoved += eventRemove;
  72. _enumPriority = enumPriority;
  73. _ticksExpires = dtExpires.Ticks;
  74. _ticksSlidingExpiration = tsSpan.Ticks;
  75. // If we have a sliding expiration it overrides the absolute expiration (MS behavior)
  76. // This is because sliding expiration causes the absolute expiration to be
  77. // moved after each period, and the absolute expiration is the value used
  78. // for all expiration calculations.
  79. //HACK: [DHC] Use constants defined in Cache.
  80. //if (tsSpan.Ticks != System.TimeSpan.Zero.Ticks)
  81. if (tsSpan.Ticks != Cache.NoSlidingExpiration.Ticks)
  82. {
  83. _ticksExpires = System.DateTime.Now.AddTicks(_ticksSlidingExpiration).Ticks;
  84. }
  85. _objDependency = objDependency;
  86. if (_objDependency != null)
  87. {
  88. if (_objDependency.IsDisposed)
  89. {
  90. throw new System.ObjectDisposedException("System.Web.CacheDependency");
  91. }
  92. // Add the entry to the cache dependency handler (we support multiple entries per handler)
  93. _objDependency.Changed += new CacheDependency.CacheDependencyCallback(OnChanged);
  94. }
  95. _longMinHits = longMinHits;
  96. }
  97. private event CacheItemRemovedCallback _onRemoved;
  98. public void OnChanged(CacheDependency objDependency)
  99. {
  100. _objCache.Remove(_strKey, CacheItemRemovedReason.DependencyChanged);
  101. }
  102. /// <summary>
  103. /// Cleans up the cache entry, removes the cache dependency and calls the remove delegate.
  104. /// </summary>
  105. /// <param name="enumReason">The reason why the cache entry are going to be removed</param>
  106. public void Close(CacheItemRemovedReason enumReason)
  107. {
  108. //HACK: optimized locks. [DHC]
  109. _lock.AcquireWriterLock(0);
  110. try
  111. {
  112. // Check if the item already is removed
  113. if (TestFlag(Flags.Removed))
  114. {
  115. return;
  116. }
  117. SetFlag(Flags.Removed);
  118. if (_onRemoved != null)
  119. {
  120. // Call the delegate to tell that we are now removing the entry
  121. try
  122. {
  123. _onRemoved(_strKey, _objItem, enumReason);
  124. }
  125. catch (System.Exception objException)
  126. {
  127. System.Diagnostics.Debug.Fail("System.Web.CacheEntry.Close() Exception when calling remove delegate", "Message: " + objException.Message + " Stack: " + objException.StackTrace + " Source:" + objException.Source);
  128. }
  129. }
  130. // If we have a dependency, remove the entry
  131. if (_objDependency != null)
  132. {
  133. _objDependency.Changed -= new CacheDependency.CacheDependencyCallback(OnChanged);
  134. if (!_objDependency.HasEvents)
  135. {
  136. _objDependency.Dispose();
  137. }
  138. }
  139. }
  140. finally
  141. {
  142. _lock.ReleaseWriterLock();
  143. }
  144. }
  145. /// <summary>
  146. /// Tests a specific flag is set or not.
  147. /// </summary>
  148. /// <param name="oFlag">Flag to test agains</param>
  149. /// <returns>Returns true if the flag is set.</returns>
  150. public bool TestFlag(Flags oFlag)
  151. {
  152. _lock.AcquireReaderLock(0);
  153. try
  154. {
  155. if ((_enumFlags & oFlag) != 0)
  156. {
  157. return true;
  158. }
  159. }
  160. finally
  161. {
  162. _lock.ReleaseReaderLock();
  163. }
  164. return false;
  165. }
  166. /// <summary>
  167. /// Sets a specific flag.
  168. /// </summary>
  169. /// <param name="oFlag">Flag to set.</param>
  170. public void SetFlag(Flags oFlag)
  171. {
  172. _lock.AcquireWriterLock(0);
  173. try
  174. {
  175. _enumFlags |= oFlag;
  176. }
  177. finally
  178. {
  179. _lock.ReleaseWriterLock ();
  180. }
  181. }
  182. /// <summary>
  183. /// Returns true if the object has minimum hit usage flushing enabled.
  184. /// </summary>
  185. public bool HasUsage
  186. {
  187. get {
  188. if (_longMinHits == System.Int64.MaxValue)
  189. {
  190. return false;
  191. }
  192. return true;
  193. }
  194. }
  195. /// <summary>
  196. /// Returns true if the entry has absolute expiration.
  197. /// </summary>
  198. public bool HasAbsoluteExpiration
  199. {
  200. get
  201. {
  202. //HACK: [DHC] Use constant defined in Cache.
  203. //if (_ticksExpires == System.DateTime.MaxValue.Ticks)
  204. if (_ticksExpires == Cache.NoAbsoluteExpiration.Ticks)
  205. {
  206. return false;
  207. }
  208. return true;
  209. }
  210. }
  211. /// <summary>
  212. /// Returns true if the entry has sliding expiration enabled.
  213. /// </summary>
  214. public bool HasSlidingExpiration
  215. {
  216. get
  217. {
  218. //HACK: [DHC] Use constants defined in Cache.
  219. //if (_ticksSlidingExpiration == System.TimeSpan.Zero.Ticks)
  220. if (_ticksSlidingExpiration == Cache.NoSlidingExpiration.Ticks)
  221. {
  222. return false;
  223. }
  224. return true;
  225. }
  226. }
  227. /// <summary>
  228. /// Gets and sets the current expires bucket the entry is active in.
  229. /// </summary>
  230. public byte ExpiresBucket
  231. {
  232. get
  233. {
  234. _lock.AcquireReaderLock(0);
  235. try
  236. {
  237. return _byteExpiresBucket;
  238. }
  239. finally
  240. {
  241. _lock.ReleaseReaderLock();
  242. }
  243. }
  244. set
  245. {
  246. _lock.AcquireWriterLock(0);
  247. try
  248. {
  249. _byteExpiresBucket = value;
  250. }
  251. finally
  252. {
  253. _lock.ReleaseWriterLock ();
  254. }
  255. }
  256. }
  257. /// <summary>
  258. /// Gets and sets the current index in the expires bucket of the current cache entry.
  259. /// </summary>
  260. public int ExpiresIndex
  261. {
  262. get
  263. {
  264. _lock.AcquireReaderLock(0);
  265. try
  266. {
  267. return _intExpiresIndex;
  268. }
  269. finally
  270. {
  271. _lock.ReleaseReaderLock();
  272. }
  273. }
  274. set
  275. {
  276. _lock.AcquireWriterLock(0);
  277. try
  278. {
  279. _intExpiresIndex = value;
  280. }
  281. finally
  282. {
  283. _lock.ReleaseWriterLock();
  284. }
  285. }
  286. }
  287. /// <summary>
  288. /// Gets and sets the expiration of the cache entry.
  289. /// </summary>
  290. public long Expires
  291. {
  292. get
  293. {
  294. _lock.AcquireReaderLock(0);
  295. try
  296. {
  297. return _ticksExpires;
  298. }
  299. finally
  300. {
  301. _lock.ReleaseReaderLock();
  302. }
  303. }
  304. set
  305. {
  306. _lock.AcquireWriterLock(0);
  307. try
  308. {
  309. _ticksExpires = value;
  310. }
  311. finally
  312. {
  313. _lock.ReleaseWriterLock();
  314. }
  315. }
  316. }
  317. /// <summary>
  318. /// Gets the sliding expiration value. The return value is in ticks (since 0/0-01 in 100nanosec)
  319. /// </summary>
  320. public long SlidingExpiration
  321. {
  322. get
  323. {
  324. return _ticksSlidingExpiration;
  325. }
  326. }
  327. /// <summary>
  328. /// Returns the current cached item.
  329. /// </summary>
  330. public object Item
  331. {
  332. get
  333. {
  334. return _objItem;
  335. }
  336. }
  337. /// <summary>
  338. /// Returns the current cache identifier.
  339. /// </summary>
  340. public string Key
  341. {
  342. get
  343. {
  344. return _strKey;
  345. }
  346. }
  347. /// <summary>
  348. /// Gets and sets the current number of hits on the cache entry.
  349. /// </summary>
  350. public long Hits
  351. {
  352. // todo: Could be optimized by using interlocked methods..
  353. get
  354. {
  355. _lock.AcquireReaderLock(0);
  356. try
  357. {
  358. return _longHits;
  359. }
  360. finally
  361. {
  362. _lock.ReleaseReaderLock();
  363. }
  364. }
  365. set
  366. {
  367. _lock.AcquireWriterLock(0);
  368. try
  369. {
  370. _longHits = value;
  371. }
  372. finally
  373. {
  374. _lock.ReleaseWriterLock();
  375. }
  376. }
  377. }
  378. /// <summary>
  379. /// Returns minimum hits for the usage flushing rutine.
  380. /// </summary>
  381. public long MinimumHits
  382. {
  383. get
  384. {
  385. return _longMinHits;
  386. }
  387. }
  388. /// <summary>
  389. /// Returns the priority of the cache entry.
  390. /// </summary>
  391. public CacheItemPriority Priority
  392. {
  393. get
  394. {
  395. return _enumPriority;
  396. }
  397. }
  398. }
  399. }