6 // Daniel Cazzulino (dcazzulino@users.sf.net)
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:
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
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.
31 using System
.Threading
;
33 namespace System
.Web
.Caching
{
35 /// Class responsible for representing a cache entry.
37 internal class CacheEntry
{
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();
71 /// Constructs a new cache entry
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
) {
84 _enumFlags
|= Flags
.Public
;
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
);
119 /// Cleans up the cache entry, removes the cache dependency and calls the remove delegate.
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);
127 // Check if the item already is removed
128 if ((_enumFlags
& Flags
.Removed
) != 0)
131 _enumFlags
|= Flags
.Removed
;
133 if (_onRemoved
!= null)
134 removedEvents
= _onRemoved
.GetInvocationList ();
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
;
146 removed (_strKey
, _objItem
, enumReason
);
148 catch (System
.Exception obj
) {
149 HttpApplicationFactory
.SignalError (obj
);
154 foreach (Delegate del
in removedEvents
) {
155 CacheItemRemovedCallback removed
= (CacheItemRemovedCallback
) del
;
157 removed (_strKey
, _objItem
, enumReason
);
165 _lock
.AcquireWriterLock(-1);
167 // If we have a dependency, remove the entry
168 if (_objDependency
!= null)
169 _objDependency
.Changed
-= new CacheDependencyChangedHandler (OnChanged
);
172 _lock
.ReleaseWriterLock();
176 internal bool HasUsage
{
178 if (_longMinHits
== System
.Int64
.MaxValue
)
185 internal bool HasAbsoluteExpiration
{
187 if (_ticksExpires
== Cache
.NoAbsoluteExpiration
.Ticks
)
194 internal bool HasSlidingExpiration
{
196 if (_ticksSlidingExpiration
== Cache
.NoSlidingExpiration
.Ticks
)
203 internal byte ExpiresBucket
{
205 return _byteExpiresBucket
;
208 _byteExpiresBucket
= value;
212 internal int ExpiresIndex
{
214 return _intExpiresIndex
;
218 _intExpiresIndex
= value;
222 internal long Expires
{
224 return _ticksExpires
;
227 _ticksExpires
= value;
231 internal long SlidingExpiration
{
233 return _ticksSlidingExpiration
;
237 internal object Item
{
243 internal string Key
{
255 internal long MinimumHits
{
261 internal CacheItemPriority Priority
{
263 return _enumPriority
;
267 internal bool IsPublic
{
269 return (_enumFlags
& Flags
.Public
) != 0;
273 internal void Hit () {
274 Interlocked
.Increment (ref _longHits
);