// // System.Web.Caching // // Author: // Patrik Torstensson (Patrik.Torstensson@labs2.com) // // (C) Copyright Patrik Torstensson, 2001 // namespace System.Web.Caching { /// /// Class responsible for representing a cache entry. /// public class CacheEntry { /// /// Defines the status of the current cache entry /// public enum Flags { Removed = 0, Public = 1 } private CacheItemPriority _enumPriority; private long _longHits; private byte _byteExpiresBucket; private int _intExpiresIndex; private long _ticksExpires; private long _ticksSlidingExpiration; private string _strKey; private object _objItem; private long _longMinHits; private Flags _enumFlags; private CacheDependency _objDependency; private Cache _objCache; /// /// Constructs a new cache entry /// /// The cache key used to reference the item. /// The item to be added to the cache. /// 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. /// The time at which the added object expires and is removed from the cache. /// 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. /// Used to detect and control if the item should be flushed due to under usage /// Defines if the item is public or not /// 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. public CacheEntry( Cache objManager, string strKey, object objItem, CacheDependency objDependency, CacheItemRemovedCallback eventRemove, System.DateTime dtExpires, System.TimeSpan tsSpan, long longMinHits, bool boolPublic, CacheItemPriority enumPriority ) { if (boolPublic) { SetFlag(Flags.Public); } _strKey = strKey; _objItem = objItem; _objCache = objManager; _onRemoved += eventRemove; _enumPriority = enumPriority; _ticksExpires = dtExpires.Ticks; _ticksSlidingExpiration = tsSpan.Ticks; // If we have a sliding expiration it overrides the absolute expiration (MS behavior) if (tsSpan.Ticks != System.TimeSpan.Zero.Ticks) { _ticksExpires = System.DateTime.Now.AddTicks(_ticksSlidingExpiration).Ticks; } _objDependency = objDependency; if (_objDependency != null) { if (_objDependency.IsDisposed) { throw new System.ObjectDisposedException("System.Web.CacheDependency"); } // Add the entry to the cache dependency handler (we support multiple entries per handler) _objDependency.Changed += new CacheDependency.CacheDependencyCallback(OnChanged); } _longMinHits = longMinHits; } private event CacheItemRemovedCallback _onRemoved; public void OnChanged(CacheDependency objDependency) { _objCache.Remove(_strKey, CacheItemRemovedReason.DependencyChanged); } /// /// Cleans up the cache entry, removes the cache dependency and calls the remove delegate. /// /// The reason why the cache entry are going to be removed public void Close(CacheItemRemovedReason enumReason) { lock(this) { // Check if the item already is removed if (TestFlag(Flags.Removed)) { return; } SetFlag(Flags.Removed); if (_onRemoved != null) { // Call the delegate to tell that we are now removing the entry try { _onRemoved(_strKey, _objItem, enumReason); } catch (System.Exception objException) { System.Diagnostics.Debug.Fail("System.Web.CacheEntry.Close() Exception when calling remove delegate", "Message: " + objException.Message + " Stack: " + objException.StackTrace + " Source:" + objException.Source); } } // If we have a dependency, remove the entry if (_objDependency != null) { _objDependency.Changed -= new CacheDependency.CacheDependencyCallback(OnChanged); if (!_objDependency.HasEvents) { _objDependency.Dispose(); } } } } /// /// Tests a specific flag is set or not. /// /// Flag to test agains /// Returns true if the flag is set. public bool TestFlag(Flags oFlag) { lock(this) { if ((_enumFlags & oFlag) != 0) { return true; } return false; } } /// /// Sets a specific flag. /// /// Flag to set. public void SetFlag(Flags oFlag) { lock (this) { _enumFlags |= oFlag; } } /// /// Returns true if the object has minimum hit usage flushing enabled. /// public bool HasUsage { get { if (_longMinHits == System.Int64.MaxValue) { return false; } return true; } } /// /// Returns true if the entry has absolute expiration. /// public bool HasAbsoluteExpiration { get { if (_ticksExpires == System.DateTime.MaxValue.Ticks) { return false; } return true; } } /// /// Returns true if the entry has sliding expiration enabled. /// public bool HasSlidingExpiration { get { if (_ticksSlidingExpiration == System.TimeSpan.Zero.Ticks) { return false; } return true; } } /// /// Gets and sets the current expires bucket the entry is active in. /// public byte ExpiresBucket { get { lock (this) { return _byteExpiresBucket; } } set { lock (this) { _byteExpiresBucket = ExpiresBucket; } } } /// /// Gets and sets the current index in the expires bucket of the current cache entry. /// public int ExpiresIndex { get { lock (this) { return _intExpiresIndex; } } set { lock (this) { _intExpiresIndex = ExpiresIndex; } } } /// /// Gets and sets the expiration of the cache entry. /// public long Expires { get { lock (this) { return _ticksExpires; } } set { lock (this) { _ticksExpires = Expires; } } } /// /// Gets the sliding expiration value. The return value is in ticks (since 0/0-01 in 100nanosec) /// public long SlidingExpiration { get { return _ticksSlidingExpiration; } } /// /// Returns the current cached item. /// public object Item { get { return _objItem; } } /// /// Returns the current cache identifier. /// public string Key { get { return _strKey; } } /// /// Gets and sets the current number of hits on the cache entry. /// public long Hits { // todo: Could be optimized by using interlocked methods.. get { lock (this) { return _longHits; } } set { lock (this) { _longHits = Hits; } } } /// /// Returns minimum hits for the usage flushing rutine. /// public long MinimumHits { get { return _longMinHits; } } /// /// Returns the priority of the cache entry. /// public CacheItemPriority Priority { get { return _enumPriority; } } } }