CacheEntry.cs 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. //
  2. // System.Web.Caching
  3. //
  4. // Author:
  5. // Patrik Torstensson
  6. // Daniel Cazzulino ([email protected])
  7. //
  8. //
  9. // Permission is hereby granted, free of charge, to any person obtaining
  10. // a copy of this software and associated documentation files (the
  11. // "Software"), to deal in the Software without restriction, including
  12. // without limitation the rights to use, copy, modify, merge, publish,
  13. // distribute, sublicense, and/or sell copies of the Software, and to
  14. // permit persons to whom the Software is furnished to do so, subject to
  15. // the following conditions:
  16. //
  17. // The above copyright notice and this permission notice shall be
  18. // included in all copies or substantial portions of the Software.
  19. //
  20. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  21. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  22. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  23. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  24. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  25. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  26. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  27. //
  28. using System;
  29. using System.Threading;
  30. namespace System.Web.Caching {
  31. /// <summary>
  32. /// Class responsible for representing a cache entry.
  33. /// </summary>
  34. internal class CacheEntry {
  35. internal enum Flags {
  36. Removed = 1,
  37. Public = 2
  38. }
  39. private CacheItemPriority _enumPriority;
  40. private long _longHits;
  41. private byte _byteExpiresBucket;
  42. private int _intExpiresIndex;
  43. private long _ticksExpires;
  44. private long _ticksSlidingExpiration;
  45. private string _strKey;
  46. private object _objItem;
  47. private long _longMinHits;
  48. private Flags _enumFlags;
  49. private CacheDependency _objDependency;
  50. private Cache _objCache;
  51. internal static readonly byte NoBucketHash = byte.MaxValue;
  52. internal static readonly int NoIndexInBucket = int.MaxValue;
  53. internal event CacheItemRemovedCallback _onRemoved;
  54. private ReaderWriterLock _lock = new ReaderWriterLock();
  55. /// <summary>
  56. /// Constructs a new cache entry
  57. /// </summary>
  58. /// <param name="strKey">The cache key used to reference the item.</param>
  59. /// <param name="objItem">The item to be added to the cache.</param>
  60. /// <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>
  61. /// <param name="dtExpires">The time at which the added object expires and is removed from the cache. </param>
  62. /// <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>
  63. /// <param name="longMinHits">Used to detect and control if the item should be flushed due to under usage</param>
  64. /// <param name="boolPublic">Defines if the item is public or not</param>
  65. /// <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>
  66. internal CacheEntry (Cache objManager, string strKey, object objItem, CacheDependency objDependency, CacheItemRemovedCallback eventRemove,
  67. System.DateTime dtExpires, System.TimeSpan tsSpan, long longMinHits, bool boolPublic, CacheItemPriority enumPriority ) {
  68. if (boolPublic)
  69. _enumFlags |= Flags.Public;
  70. _strKey = strKey;
  71. _objItem = objItem;
  72. _objCache = objManager;
  73. _onRemoved += eventRemove;
  74. _enumPriority = enumPriority;
  75. _ticksExpires = dtExpires.ToUniversalTime ().Ticks;
  76. _ticksSlidingExpiration = tsSpan.Ticks;
  77. // If we have a sliding expiration it overrides the absolute expiration (MS behavior)
  78. // This is because sliding expiration causes the absolute expiration to be
  79. // moved after each period, and the absolute expiration is the value used
  80. // for all expiration calculations.
  81. if (tsSpan.Ticks != Cache.NoSlidingExpiration.Ticks)
  82. _ticksExpires = System.DateTime.UtcNow.AddTicks(_ticksSlidingExpiration).Ticks;
  83. _objDependency = objDependency;
  84. if (_objDependency != null)
  85. // Add the entry to the cache dependency handler (we support multiple entries per handler)
  86. _objDependency.Changed += new CacheDependencyChangedHandler (OnChanged);
  87. _longMinHits = longMinHits;
  88. }
  89. internal void OnChanged (object sender, CacheDependencyChangedArgs objDependency) {
  90. _objCache.Remove (_strKey, CacheItemRemovedReason.DependencyChanged);
  91. }
  92. /// <summary>
  93. /// Cleans up the cache entry, removes the cache dependency and calls the remove delegate.
  94. /// </summary>
  95. /// <param name="enumReason">The reason why the cache entry are going to be removed</param>
  96. internal void Close(CacheItemRemovedReason enumReason) {
  97. Delegate [] removedEvents = null;
  98. _lock.AcquireWriterLock(-1);
  99. try {
  100. // Check if the item already is removed
  101. if ((_enumFlags & Flags.Removed) != 0)
  102. return;
  103. _enumFlags |= Flags.Removed;
  104. if (_onRemoved != null)
  105. removedEvents = _onRemoved.GetInvocationList ();
  106. }
  107. finally {
  108. _lock.ReleaseWriterLock();
  109. }
  110. if (removedEvents != null) {
  111. // Call the delegate to tell that we are now removing the entry
  112. if ((_enumFlags & Flags.Public) != 0) {
  113. foreach (Delegate del in removedEvents) {
  114. CacheItemRemovedCallback removed = (CacheItemRemovedCallback) del;
  115. try {
  116. removed (_strKey, _objItem, enumReason);
  117. }
  118. catch (System.Exception obj) {
  119. HttpApplicationFactory.SignalError (obj);
  120. }
  121. }
  122. }
  123. else {
  124. foreach (Delegate del in removedEvents) {
  125. CacheItemRemovedCallback removed = (CacheItemRemovedCallback) del;
  126. try {
  127. removed (_strKey, _objItem, enumReason);
  128. }
  129. catch (Exception) {
  130. }
  131. }
  132. }
  133. }
  134. _lock.AcquireWriterLock(-1);
  135. try {
  136. // If we have a dependency, remove the entry
  137. if (_objDependency != null)
  138. _objDependency.Changed -= new CacheDependencyChangedHandler (OnChanged);
  139. }
  140. finally {
  141. _lock.ReleaseWriterLock();
  142. }
  143. }
  144. internal bool HasUsage {
  145. get {
  146. if (_longMinHits == System.Int64.MaxValue)
  147. return false;
  148. return true;
  149. }
  150. }
  151. internal bool HasAbsoluteExpiration {
  152. get {
  153. if (_ticksExpires == Cache.NoAbsoluteExpiration.Ticks)
  154. return false;
  155. return true;
  156. }
  157. }
  158. internal bool HasSlidingExpiration {
  159. get {
  160. if (_ticksSlidingExpiration == Cache.NoSlidingExpiration.Ticks)
  161. return false;
  162. return true;
  163. }
  164. }
  165. internal byte ExpiresBucket {
  166. get {
  167. return _byteExpiresBucket;
  168. }
  169. set {
  170. _byteExpiresBucket = value;
  171. }
  172. }
  173. internal int ExpiresIndex {
  174. get {
  175. return _intExpiresIndex;
  176. }
  177. set {
  178. _intExpiresIndex = value;
  179. }
  180. }
  181. internal long Expires {
  182. get {
  183. return _ticksExpires;
  184. }
  185. set {
  186. _ticksExpires = value;
  187. }
  188. }
  189. internal long SlidingExpiration {
  190. get {
  191. return _ticksSlidingExpiration;
  192. }
  193. }
  194. internal object Item {
  195. get {
  196. return _objItem;
  197. }
  198. }
  199. internal string Key {
  200. get {
  201. return _strKey;
  202. }
  203. }
  204. internal long Hits {
  205. get {
  206. return _longHits;
  207. }
  208. }
  209. internal long MinimumHits {
  210. get {
  211. return _longMinHits;
  212. }
  213. }
  214. internal CacheItemPriority Priority {
  215. get {
  216. return _enumPriority;
  217. }
  218. }
  219. internal bool IsPublic {
  220. get {
  221. return (_enumFlags & Flags.Public) != 0;
  222. }
  223. }
  224. internal void Hit () {
  225. Interlocked.Increment (ref _longHits);
  226. }
  227. }
  228. }