| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313 |
- //
- // System.Web.Caching
- //
- // Author:
- // Patrik Torstensson
- // Daniel Cazzulino [DHC] ([email protected])
- //
- //
- // Permission is hereby granted, free of charge, to any person obtaining
- // a copy of this software and associated documentation files (the
- // "Software"), to deal in the Software without restriction, including
- // without limitation the rights to use, copy, modify, merge, publish,
- // distribute, sublicense, and/or sell copies of the Software, and to
- // permit persons to whom the Software is furnished to do so, subject to
- // the following conditions:
- //
- // The above copyright notice and this permission notice shall be
- // included in all copies or substantial portions of the Software.
- //
- // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
- // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
- // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
- // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
- // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
- // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
- // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
- //
- using System;
- using System.Collections;
- using System.Threading;
- namespace System.Web.Caching {
- public sealed class Cache : IEnumerable {
- public static readonly DateTime NoAbsoluteExpiration = DateTime.MaxValue;
- public static readonly TimeSpan NoSlidingExpiration = TimeSpan.Zero;
- // Helper objects
- private CacheExpires _objExpires;
- // The data storage
- private Hashtable _arrEntries;
- private ReaderWriterLock _lockEntries;
- private int _nItems;
-
- static private TimeSpan _datetimeOneYear = TimeSpan.FromDays (365);
- public Cache () {
- _nItems = 0;
- _lockEntries = new ReaderWriterLock ();
- _arrEntries = new Hashtable ();
- _objExpires = new CacheExpires (this);
- }
- #if TARGET_J2EE
- internal void Destroy()
- {
- _arrEntries = null;
- _objExpires.Close();
- }
- #endif
- private IDictionaryEnumerator CreateEnumerator () {
- Hashtable objTable;
- //Locking with -1 provides a non-expiring lock.
- _lockEntries.AcquireReaderLock (-1);
- try {
- // Create a new hashtable to return as collection of public items
- objTable = new Hashtable (_arrEntries.Count);
- foreach (DictionaryEntry objEntry in _arrEntries) {
- if (objEntry.Key == null)
- continue;
- CacheEntry entry = (CacheEntry) objEntry.Value;
- if (entry.IsPublic)
- objTable.Add (objEntry.Key, entry.Item);
- }
- } finally {
- _lockEntries.ReleaseReaderLock ();
- }
- return objTable.GetEnumerator ();
- }
- IEnumerator IEnumerable.GetEnumerator () {
- return GetEnumerator ();
- }
- public IDictionaryEnumerator GetEnumerator () {
- return CreateEnumerator ();
- }
- internal void Touch(string strKey) {
- GetEntry (strKey);
- }
- public object Add (string strKey, object objItem, CacheDependency objDependency,
- DateTime absolutExpiration, TimeSpan slidingExpiration,
- CacheItemPriority enumPriority, CacheItemRemovedCallback eventRemoveCallback) {
-
- return Add (strKey, objItem, objDependency, absolutExpiration,
- slidingExpiration, enumPriority, eventRemoveCallback, true, false);
- }
- private object Add (string strKey,
- object objItem,
- CacheDependency objDependency,
- DateTime absolutExpiration,
- TimeSpan slidingExpiration,
- CacheItemPriority enumPriority,
- CacheItemRemovedCallback eventRemoveCallback,
- bool pub,
- bool overwrite) {
- if (strKey == null)
- throw new ArgumentNullException ("strKey");
- if (objItem == null)
- throw new ArgumentNullException ("objItem");
- if (slidingExpiration > _datetimeOneYear)
- throw new ArgumentOutOfRangeException ("slidingExpiration");
- CacheEntry objEntry;
- CacheEntry objOldEntry = null;
- long longHitRange = 10000;
- // todo: check decay and make up the minHit range
- objEntry = new CacheEntry ( this,
- strKey,
- objItem,
- objDependency,
- eventRemoveCallback,
- absolutExpiration,
- slidingExpiration,
- longHitRange,
- pub,
- enumPriority);
- _lockEntries.AcquireWriterLock (-1);
- try {
- _nItems++;
- if (_arrEntries.Contains (strKey)) {
- if (overwrite)
- objOldEntry = _arrEntries [strKey] as CacheEntry;
- else
- return null;
- }
-
- objEntry.Hit ();
- _arrEntries [strKey] = objEntry;
- // If we have any kind of expiration add into the CacheExpires
- // Do this under the lock so no-one can retrieve the objEntry
- // before it is fully initialized.
- if (objEntry.HasSlidingExpiration || objEntry.HasAbsoluteExpiration) {
- if (objEntry.HasSlidingExpiration)
- objEntry.Expires = DateTime.UtcNow.Ticks + objEntry.SlidingExpiration;
- _objExpires.Add (objEntry);
- }
- } finally {
- _lockEntries.ReleaseLock ();
- }
- if (objOldEntry != null) {
- if (objOldEntry.HasAbsoluteExpiration || objOldEntry.HasSlidingExpiration)
- _objExpires.Remove (objOldEntry);
- objOldEntry.Close (CacheItemRemovedReason.Removed);
- }
- return objEntry.Item;
- }
-
- public void Insert (string strKey, object objItem) {
- Add (strKey, objItem, null, NoAbsoluteExpiration, NoSlidingExpiration,
- CacheItemPriority.Default, null, true, true);
- }
- public void Insert (string strKey, object objItem, CacheDependency objDependency) {
- Add (strKey, objItem, objDependency, NoAbsoluteExpiration, NoSlidingExpiration,
- CacheItemPriority.Default, null, true, true);
- }
- public void Insert (string strKey, object objItem, CacheDependency objDependency,
- DateTime absolutExpiration, TimeSpan slidingExpiration) {
- Add (strKey, objItem, objDependency, absolutExpiration, slidingExpiration,
- CacheItemPriority.Default, null, true, true);
- }
- public void Insert (string strKey, object objItem, CacheDependency objDependency,
- DateTime absolutExpiration, TimeSpan slidingExpiration,
- CacheItemPriority enumPriority, CacheItemRemovedCallback eventRemoveCallback) {
- Add (strKey, objItem, objDependency, absolutExpiration, slidingExpiration,
- enumPriority, eventRemoveCallback, true, true);
- }
- // Called from other internal System.Web methods to add non-public objects into
- // cache, like output cache etc
- internal void InsertPrivate (string strKey, object objItem, CacheDependency objDependency,
- DateTime absolutExpiration, TimeSpan slidingExpiration,
- CacheItemPriority enumPriority, CacheItemRemovedCallback eventRemoveCallback)
- {
- Add (strKey, objItem, objDependency, absolutExpiration, slidingExpiration,
- enumPriority, eventRemoveCallback, false, true);
- }
- internal void InsertPrivate (string strKey, object objItem, CacheDependency objDependency)
- {
- Add (strKey, objItem, objDependency, NoAbsoluteExpiration, NoSlidingExpiration,
- CacheItemPriority.Default, null, false, true);
- }
- public object Remove (string strKey) {
- return Remove (strKey, CacheItemRemovedReason.Removed);
- }
- internal object Remove (string strKey, CacheItemRemovedReason enumReason) {
- CacheEntry objEntry = null;
- if (strKey == null)
- throw new ArgumentNullException ("strKey");
- _lockEntries.AcquireWriterLock (-1);
- try {
- objEntry = _arrEntries [strKey] as CacheEntry;
- if (null == objEntry)
- return null;
- _arrEntries.Remove (strKey);
- _nItems--;
- }
- finally {
- _lockEntries.ReleaseWriterLock ();
- }
- if (objEntry.HasAbsoluteExpiration || objEntry.HasSlidingExpiration)
- _objExpires.Remove (objEntry);
- objEntry.Close (enumReason);
- return objEntry.Item;
- }
- public object Get (string strKey) {
- CacheEntry objEntry = GetEntry (strKey);
- if (objEntry == null)
- return null;
- return objEntry.Item;
- }
- internal CacheEntry GetEntry (string strKey) {
- CacheEntry objEntry = null;
- long ticksNow = DateTime.UtcNow.Ticks;
-
- if (strKey == null)
- throw new ArgumentNullException ("strKey");
- _lockEntries.AcquireReaderLock (-1);
- try {
- objEntry = _arrEntries [strKey] as CacheEntry;
- if (null == objEntry)
- return null;
- }
- finally {
- _lockEntries.ReleaseReaderLock ();
- }
- if (objEntry.HasSlidingExpiration || objEntry.HasAbsoluteExpiration) {
- if (objEntry.Expires < ticksNow) {
- Remove (strKey, CacheItemRemovedReason.Expired);
- return null;
- }
- }
- objEntry.Hit ();
- if (objEntry.HasSlidingExpiration) {
- objEntry.Expires = ticksNow + objEntry.SlidingExpiration;
- }
- return objEntry;
- }
- public int Count {
- get { return _nItems; }
- }
- public object this [string strKey] {
- get {
- return Get (strKey);
- }
- set {
- Insert (strKey, value);
- }
- }
- }
- }
|