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