CacheEntry.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436
  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. internal 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. internal static readonly byte NoBucketHash = byte.MaxValue;
  42. /// <summary>
  43. /// The item is not placed in a bucket. [DHC]
  44. /// </summary>
  45. internal 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. internal 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. // Add the entry to the cache dependency handler (we support multiple entries per handler)
  89. _objDependency.Changed += new CacheDependencyChangedHandler (OnChanged);
  90. }
  91. _longMinHits = longMinHits;
  92. }
  93. internal event CacheItemRemovedCallback _onRemoved;
  94. internal void OnChanged (object sender, CacheDependencyChangedArgs objDependency)
  95. {
  96. _objCache.Remove (_strKey, CacheItemRemovedReason.DependencyChanged);
  97. }
  98. /// <summary>
  99. /// Cleans up the cache entry, removes the cache dependency and calls the remove delegate.
  100. /// </summary>
  101. /// <param name="enumReason">The reason why the cache entry are going to be removed</param>
  102. internal void Close(CacheItemRemovedReason enumReason)
  103. {
  104. //HACK: optimized locks. [DHC]
  105. _lock.AcquireWriterLock(0);
  106. try
  107. {
  108. // Check if the item already is removed
  109. if (TestFlag(Flags.Removed))
  110. {
  111. return;
  112. }
  113. SetFlag(Flags.Removed);
  114. if (_onRemoved != null)
  115. {
  116. // Call the delegate to tell that we are now removing the entry
  117. try
  118. {
  119. _onRemoved(_strKey, _objItem, enumReason);
  120. }
  121. catch (System.Exception objException)
  122. {
  123. System.Diagnostics.Debug.Fail("System.Web.CacheEntry.Close() Exception when calling remove delegate", "Message: " + objException.Message + " Stack: " + objException.StackTrace + " Source:" + objException.Source);
  124. }
  125. }
  126. // If we have a dependency, remove the entry
  127. if (_objDependency != null)
  128. {
  129. _objDependency.Changed -= new CacheDependencyChangedHandler (OnChanged);
  130. }
  131. }
  132. finally
  133. {
  134. _lock.ReleaseWriterLock();
  135. }
  136. }
  137. /// <summary>
  138. /// Tests a specific flag is set or not.
  139. /// </summary>
  140. /// <param name="oFlag">Flag to test agains</param>
  141. /// <returns>Returns true if the flag is set.</returns>
  142. internal bool TestFlag(Flags oFlag)
  143. {
  144. _lock.AcquireReaderLock(0);
  145. try
  146. {
  147. if ((_enumFlags & oFlag) != 0)
  148. {
  149. return true;
  150. }
  151. }
  152. finally
  153. {
  154. _lock.ReleaseReaderLock();
  155. }
  156. return false;
  157. }
  158. /// <summary>
  159. /// Sets a specific flag.
  160. /// </summary>
  161. /// <param name="oFlag">Flag to set.</param>
  162. internal void SetFlag(Flags oFlag)
  163. {
  164. _lock.AcquireWriterLock(0);
  165. try
  166. {
  167. _enumFlags |= oFlag;
  168. }
  169. finally
  170. {
  171. _lock.ReleaseWriterLock ();
  172. }
  173. }
  174. /// <summary>
  175. /// Returns true if the object has minimum hit usage flushing enabled.
  176. /// </summary>
  177. internal bool HasUsage
  178. {
  179. get {
  180. if (_longMinHits == System.Int64.MaxValue)
  181. {
  182. return false;
  183. }
  184. return true;
  185. }
  186. }
  187. /// <summary>
  188. /// Returns true if the entry has absolute expiration.
  189. /// </summary>
  190. internal bool HasAbsoluteExpiration
  191. {
  192. get
  193. {
  194. //HACK: [DHC] Use constant defined in Cache.
  195. //if (_ticksExpires == System.DateTime.MaxValue.Ticks)
  196. if (_ticksExpires == Cache.NoAbsoluteExpiration.Ticks)
  197. {
  198. return false;
  199. }
  200. return true;
  201. }
  202. }
  203. /// <summary>
  204. /// Returns true if the entry has sliding expiration enabled.
  205. /// </summary>
  206. internal bool HasSlidingExpiration
  207. {
  208. get
  209. {
  210. //HACK: [DHC] Use constants defined in Cache.
  211. //if (_ticksSlidingExpiration == System.TimeSpan.Zero.Ticks)
  212. if (_ticksSlidingExpiration == Cache.NoSlidingExpiration.Ticks)
  213. {
  214. return false;
  215. }
  216. return true;
  217. }
  218. }
  219. /// <summary>
  220. /// Gets and sets the current expires bucket the entry is active in.
  221. /// </summary>
  222. internal byte ExpiresBucket
  223. {
  224. get
  225. {
  226. _lock.AcquireReaderLock(0);
  227. try
  228. {
  229. return _byteExpiresBucket;
  230. }
  231. finally
  232. {
  233. _lock.ReleaseReaderLock();
  234. }
  235. }
  236. set
  237. {
  238. _lock.AcquireWriterLock(0);
  239. try
  240. {
  241. _byteExpiresBucket = value;
  242. }
  243. finally
  244. {
  245. _lock.ReleaseWriterLock ();
  246. }
  247. }
  248. }
  249. /// <summary>
  250. /// Gets and sets the current index in the expires bucket of the current cache entry.
  251. /// </summary>
  252. internal int ExpiresIndex
  253. {
  254. get
  255. {
  256. _lock.AcquireReaderLock(0);
  257. try
  258. {
  259. return _intExpiresIndex;
  260. }
  261. finally
  262. {
  263. _lock.ReleaseReaderLock();
  264. }
  265. }
  266. set
  267. {
  268. _lock.AcquireWriterLock(0);
  269. try
  270. {
  271. _intExpiresIndex = value;
  272. }
  273. finally
  274. {
  275. _lock.ReleaseWriterLock();
  276. }
  277. }
  278. }
  279. /// <summary>
  280. /// Gets and sets the expiration of the cache entry.
  281. /// </summary>
  282. internal long Expires
  283. {
  284. get
  285. {
  286. _lock.AcquireReaderLock(0);
  287. try
  288. {
  289. return _ticksExpires;
  290. }
  291. finally
  292. {
  293. _lock.ReleaseReaderLock();
  294. }
  295. }
  296. set
  297. {
  298. _lock.AcquireWriterLock(0);
  299. try
  300. {
  301. _ticksExpires = value;
  302. }
  303. finally
  304. {
  305. _lock.ReleaseWriterLock();
  306. }
  307. }
  308. }
  309. /// <summary>
  310. /// Gets the sliding expiration value. The return value is in ticks (since 0/0-01 in 100nanosec)
  311. /// </summary>
  312. internal long SlidingExpiration
  313. {
  314. get
  315. {
  316. return _ticksSlidingExpiration;
  317. }
  318. }
  319. /// <summary>
  320. /// Returns the current cached item.
  321. /// </summary>
  322. internal object Item
  323. {
  324. get
  325. {
  326. return _objItem;
  327. }
  328. }
  329. /// <summary>
  330. /// Returns the current cache identifier.
  331. /// </summary>
  332. internal string Key
  333. {
  334. get
  335. {
  336. return _strKey;
  337. }
  338. }
  339. /// <summary>
  340. /// Gets and sets the current number of hits on the cache entry.
  341. /// </summary>
  342. internal long Hits
  343. {
  344. // todo: Could be optimized by using interlocked methods..
  345. get
  346. {
  347. _lock.AcquireReaderLock(0);
  348. try
  349. {
  350. return _longHits;
  351. }
  352. finally
  353. {
  354. _lock.ReleaseReaderLock();
  355. }
  356. }
  357. set
  358. {
  359. _lock.AcquireWriterLock(0);
  360. try
  361. {
  362. _longHits = value;
  363. }
  364. finally
  365. {
  366. _lock.ReleaseWriterLock();
  367. }
  368. }
  369. }
  370. /// <summary>
  371. /// Returns minimum hits for the usage flushing rutine.
  372. /// </summary>
  373. internal long MinimumHits
  374. {
  375. get
  376. {
  377. return _longMinHits;
  378. }
  379. }
  380. /// <summary>
  381. /// Returns the priority of the cache entry.
  382. /// </summary>
  383. internal CacheItemPriority Priority
  384. {
  385. get
  386. {
  387. return _enumPriority;
  388. }
  389. }
  390. }
  391. }