| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344 |
- //
- // System.Web.Caching.Cache
- //
- // Author(s):
- // Lluis Sanchez ([email protected])
- //
- // (C) 2005 Novell, Inc (http://www.novell.com)
- //
- //
- // 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.Threading;
- using System.Collections;
- using System.Security.Permissions;
- namespace System.Web.Caching
- {
- // CAS - no InheritanceDemand here as the class is sealed
- [AspNetHostingPermission (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
- public sealed class Cache: IEnumerable
- {
- Hashtable cache;
- Timer timer;
- bool needsTimer;
-
- public static readonly DateTime NoAbsoluteExpiration = DateTime.MaxValue;
- public static readonly TimeSpan NoSlidingExpiration = TimeSpan.Zero;
-
- public Cache ()
- {
- cache = new Hashtable ();
- }
-
- public int Count {
- get { return cache.Count; }
- }
-
- public object this [string key] {
- get { return Get (key); }
- set { Insert (key, value); }
- }
-
- public object Add (string key, object value, CacheDependency dependencies, DateTime absoluteExpiration, TimeSpan slidingExpiration, CacheItemPriority priority, CacheItemRemovedCallback onRemoveCallback)
- {
- if (key == null)
- throw new ArgumentNullException ("key");
- lock (cache) {
- CacheItem it = (CacheItem) cache [key];
- if (it != null)
- return it.Value;
- Insert (key, value, dependencies, absoluteExpiration, slidingExpiration, priority, onRemoveCallback);
- }
- return null;
- }
-
- public object Get (string key)
- {
- lock (cache) {
- CacheItem it = (CacheItem) cache [key];
- if (it == null)
- return null;
- if (it.Dependency != null && it.Dependency.HasChanged) {
- Remove (it.Key, CacheItemRemovedReason.DependencyChanged);
- return null;
- }
- if (it.SlidingExpiration != NoSlidingExpiration) {
- it.AbsoluteExpiration = DateTime.Now + it.SlidingExpiration;
- } else if (DateTime.Now >= it.AbsoluteExpiration) {
- Remove (key, CacheItemRemovedReason.Expired);
- return null;
- }
- return it.Value;
- }
- }
-
- public void Insert (string key, object value)
- {
- Insert (key, value, null, NoAbsoluteExpiration, NoSlidingExpiration, CacheItemPriority.Normal, null);
- }
-
- public void Insert (string key, object value, CacheDependency dependencies)
- {
- Insert (key, value, dependencies, NoAbsoluteExpiration, NoSlidingExpiration, CacheItemPriority.Normal, null);
- }
-
- public void Insert (string key, object value, CacheDependency dependencies, DateTime absoluteExpiration, TimeSpan slidingExpiration)
- {
- Insert (key, value, dependencies, absoluteExpiration, slidingExpiration, CacheItemPriority.Normal, null);
- }
-
- public void Insert (string key, object value, CacheDependency dependencies, DateTime absoluteExpiration, TimeSpan slidingExpiration, CacheItemPriority priority, CacheItemRemovedCallback onRemoveCallback)
- {
- InsertInternal (key, value, dependencies, absoluteExpiration, slidingExpiration, priority, onRemoveCallback, false);
- }
-
- internal void InsertPrivate (string key, object value, CacheDependency dependencies)
- {
- InsertInternal (key, value, dependencies, NoAbsoluteExpiration, NoSlidingExpiration, CacheItemPriority.Normal, null, true);
- }
-
- internal void InsertPrivate (string key, object value, CacheDependency dependencies, DateTime absoluteExpiration, TimeSpan slidingExpiration, CacheItemPriority priority, CacheItemRemovedCallback onRemoveCallback)
- {
- InsertInternal (key, value, dependencies, absoluteExpiration, slidingExpiration, priority, onRemoveCallback, true);
- }
-
- void InsertInternal (string key, object value, CacheDependency dependency, DateTime absoluteExpiration, TimeSpan slidingExpiration, CacheItemPriority priority, CacheItemRemovedCallback onRemoveCallback, bool isprivate)
- {
- if (key == null)
- throw new ArgumentNullException ("key");
- if (value == null)
- throw new ArgumentNullException ("value");
- if (slidingExpiration < TimeSpan.Zero || slidingExpiration > TimeSpan.FromDays (365))
- throw new ArgumentNullException ("slidingExpiration");
- if (absoluteExpiration != NoAbsoluteExpiration && slidingExpiration != NoSlidingExpiration)
- throw new ArgumentException ("Both absoluteExpiration and slidingExpiration are specified");
-
- CacheItem ci = new CacheItem ();
- ci.Value = value;
- ci.Key = key;
- ci.Private = isprivate;
-
- if (dependency != null) {
- ci.Dependency = dependency;
- dependency.DependencyChanged += new EventHandler (OnDependencyChanged);
- dependency.SetCache (this);
- }
- ci.SlidingExpiration = slidingExpiration;
- if (slidingExpiration != NoSlidingExpiration)
- ci.AbsoluteExpiration = DateTime.Now + slidingExpiration;
- else
- ci.AbsoluteExpiration = absoluteExpiration;
-
- ci.Priority = priority;
- ci.OnRemoveCallback = onRemoveCallback;
-
- lock (cache) {
- cache [key] = ci;
- ci.LastChange = DateTime.Now;
- if (ci.AbsoluteExpiration != NoAbsoluteExpiration && timer == null) {
- timer = new Timer (new TimerCallback (TimerRun), null, 60000, 60000);
- needsTimer = true;
- }
- }
- }
-
- public object Remove (string key)
- {
- return Remove (key, CacheItemRemovedReason.Removed);
- }
-
- object Remove (string key, CacheItemRemovedReason reason)
- {
- lock (cache) {
- CacheItem it = (CacheItem) cache [key];
- if (it != null) {
- if (it.Dependency != null) {
- #if NET_2_0
- it.Dependency.SetCache (null);
- #endif
- it.Dependency.DependencyChanged -= new EventHandler (OnDependencyChanged);
- it.Dependency.Dispose ();
- }
- cache.Remove (key);
- if (it.OnRemoveCallback != null) {
- try {
- it.OnRemoveCallback (key, it.Value, reason);
- } catch {
- //TODO: anything to be done here?
- }
- }
- return it.Value;
- }
- else
- return null;
- }
- }
- // Used when shutting down the application so that
- // session_end events are sent for all sessions.
- internal void InvokePrivateCallbacks ()
- {
- CacheItemRemovedReason reason = CacheItemRemovedReason.Removed;
- lock (cache) {
- foreach (string key in cache.Keys) {
- CacheItem item = (CacheItem) cache [key];
- if (item == null || false == item.Private)
- continue;
- if (item.OnRemoveCallback != null) {
- try {
- item.OnRemoveCallback (key, item.Value, reason);
- } catch {
- //TODO: anything to be done here?
- }
- }
- }
- }
- }
- public IDictionaryEnumerator GetEnumerator ()
- {
- ArrayList list = new ArrayList ();
- lock (cache) {
- foreach (CacheItem it in cache.Values)
- if (!it.Private)
- list.Add (it);
- }
- return new CacheItemEnumerator (list);
- }
-
- IEnumerator IEnumerable.GetEnumerator ()
- {
- return GetEnumerator ();
- }
-
- void TimerRun (object ob)
- {
- CheckExpiration ();
- }
-
- void OnDependencyChanged (object o, EventArgs a)
- {
- CheckExpiration ();
- }
-
- internal void CheckExpiration ()
- {
- ArrayList list;
- lock (cache) {
- list = new ArrayList ();
- list.AddRange (cache.Values);
- needsTimer = false;
- }
-
- DateTime now = DateTime.Now;
- foreach (CacheItem it in list) {
- if (it.Dependency != null && it.Dependency.HasChanged)
- Remove (it.Key, CacheItemRemovedReason.DependencyChanged);
- else if (it.AbsoluteExpiration != NoAbsoluteExpiration) {
- if (now >= it.AbsoluteExpiration)
- Remove (it.Key, CacheItemRemovedReason.Expired);
- else
- needsTimer = true;
- }
- }
-
- lock (cache) {
- if (!needsTimer && timer != null) {
- timer.Dispose ();
- timer = null;
- }
- }
- }
-
- internal DateTime GetKeyLastChange (string key)
- {
- lock (cache) {
- CacheItem it = (CacheItem) cache [key];
- if (it == null) return DateTime.MaxValue;
- return it.LastChange;
- }
- }
- }
- class CacheItem
- {
- public object Value;
- public string Key;
- public CacheDependency Dependency;
- public DateTime AbsoluteExpiration;
- public TimeSpan SlidingExpiration;
- public CacheItemPriority Priority;
- public CacheItemRemovedCallback OnRemoveCallback;
- public DateTime LastChange;
- public bool Private;
- }
-
- class CacheItemEnumerator: IDictionaryEnumerator
- {
- ArrayList list;
- int pos = -1;
-
- public CacheItemEnumerator (ArrayList list)
- {
- this.list = list;
- }
-
- CacheItem Item {
- get {
- if (pos < 0 || pos >= list.Count)
- throw new InvalidOperationException ();
- return (CacheItem) list [pos];
- }
- }
-
- public DictionaryEntry Entry {
- get { return new DictionaryEntry (Item.Key, Item.Value); }
- }
-
- public object Key {
- get { return Item.Key; }
- }
-
- public object Value {
- get { return Item.Value; }
- }
-
- public object Current {
- get { return Entry; }
- }
-
- public bool MoveNext ()
- {
- return (++pos < list.Count);
- }
-
- public void Reset ()
- {
- pos = -1;
- }
- }
- }
|