Cache.cs 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  1. //
  2. // System.Web.Caching.Cache
  3. //
  4. // Author(s):
  5. // Lluis Sanchez ([email protected])
  6. //
  7. // (C) 2005 Novell, Inc (http://www.novell.com)
  8. //
  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.
  28. //
  29. using System;
  30. using System.Threading;
  31. using System.Collections;
  32. namespace System.Web.Caching
  33. {
  34. public sealed class Cache: IEnumerable
  35. {
  36. Hashtable cache;
  37. Timer timer;
  38. bool needsTimer;
  39. public static readonly DateTime NoAbsoluteExpiration = DateTime.MaxValue;
  40. public static readonly TimeSpan NoSlidingExpiration = TimeSpan.Zero;
  41. public Cache ()
  42. {
  43. cache = new Hashtable ();
  44. }
  45. public int Count {
  46. get { return cache.Count; }
  47. }
  48. public object this [string key] {
  49. get { return Get (key); }
  50. set { Insert (key, value); }
  51. }
  52. public object Add (string key, object value, CacheDependency dependencies, DateTime absoluteExpiration, TimeSpan slidingExpiration, CacheItemPriority priority, CacheItemRemovedCallback onRemoveCallback)
  53. {
  54. if (key == null)
  55. throw new ArgumentNullException ("key");
  56. lock (cache) {
  57. CacheItem it = (CacheItem) cache [key];
  58. if (it != null)
  59. return it.Value;
  60. Insert (key, value, dependencies, absoluteExpiration, slidingExpiration, priority, onRemoveCallback);
  61. }
  62. return null;
  63. }
  64. public object Get (string key)
  65. {
  66. lock (cache) {
  67. CacheItem it = (CacheItem) cache [key];
  68. if (it == null)
  69. return null;
  70. if (it.SlidingExpiration != NoSlidingExpiration)
  71. it.AbsoluteExpiration = DateTime.Now + it.SlidingExpiration;
  72. else if (DateTime.Now >= it.AbsoluteExpiration) {
  73. Remove (key, CacheItemRemovedReason.Expired);
  74. return null;
  75. }
  76. return it.Value;
  77. }
  78. }
  79. public void Insert (string key, object value)
  80. {
  81. Insert (key, value, null, NoAbsoluteExpiration, NoSlidingExpiration, CacheItemPriority.Normal, null);
  82. }
  83. public void Insert (string key, object value, CacheDependency dependencies)
  84. {
  85. Insert (key, value, dependencies, NoAbsoluteExpiration, NoSlidingExpiration, CacheItemPriority.Normal, null);
  86. }
  87. public void Insert (string key, object value, CacheDependency dependencies, DateTime absoluteExpiration, TimeSpan slidingExpiration)
  88. {
  89. Insert (key, value, dependencies, absoluteExpiration, slidingExpiration, CacheItemPriority.Normal, null);
  90. }
  91. public void Insert (string key, object value, CacheDependency dependencies, DateTime absoluteExpiration, TimeSpan slidingExpiration, CacheItemPriority priority, CacheItemRemovedCallback onRemoveCallback)
  92. {
  93. InsertInternal (key, value, dependencies, absoluteExpiration, slidingExpiration, priority, onRemoveCallback, false);
  94. }
  95. internal void InsertPrivate (string key, object value, CacheDependency dependencies)
  96. {
  97. InsertInternal (key, value, dependencies, NoAbsoluteExpiration, NoSlidingExpiration, CacheItemPriority.Normal, null, true);
  98. }
  99. internal void InsertPrivate (string key, object value, CacheDependency dependencies, DateTime absoluteExpiration, TimeSpan slidingExpiration, CacheItemPriority priority, CacheItemRemovedCallback onRemoveCallback)
  100. {
  101. InsertInternal (key, value, dependencies, absoluteExpiration, slidingExpiration, priority, onRemoveCallback, true);
  102. }
  103. void InsertInternal (string key, object value, CacheDependency dependency, DateTime absoluteExpiration, TimeSpan slidingExpiration, CacheItemPriority priority, CacheItemRemovedCallback onRemoveCallback, bool isprivate)
  104. {
  105. if (key == null)
  106. throw new ArgumentNullException ("key");
  107. if (value == null)
  108. throw new ArgumentNullException ("value");
  109. if (slidingExpiration < TimeSpan.Zero || slidingExpiration > TimeSpan.FromDays (365))
  110. throw new ArgumentNullException ("slidingExpiration");
  111. if (absoluteExpiration != NoAbsoluteExpiration && slidingExpiration != NoSlidingExpiration)
  112. throw new ArgumentException ("Both absoluteExpiration and slidingExpiration are specified");
  113. CacheItem ci = new CacheItem ();
  114. ci.Value = value;
  115. ci.Key = key;
  116. ci.Private = isprivate;
  117. if (dependency != null) {
  118. ci.Dependency = dependency;
  119. dependency.DependencyChanged += new EventHandler (OnDependencyChanged);
  120. dependency.SetCache (this);
  121. }
  122. ci.SlidingExpiration = slidingExpiration;
  123. if (slidingExpiration != NoSlidingExpiration)
  124. ci.AbsoluteExpiration = DateTime.Now + slidingExpiration;
  125. else
  126. ci.AbsoluteExpiration = absoluteExpiration;
  127. ci.Priority = priority;
  128. ci.OnRemoveCallback = onRemoveCallback;
  129. lock (cache) {
  130. cache [key] = ci;
  131. ci.LastChange = DateTime.Now;
  132. if (ci.AbsoluteExpiration != NoAbsoluteExpiration && timer == null) {
  133. timer = new Timer (new TimerCallback (TimerRun), null, 60000, 60000);
  134. needsTimer = true;
  135. }
  136. }
  137. }
  138. public object Remove (string key)
  139. {
  140. return Remove (key, CacheItemRemovedReason.Removed);
  141. }
  142. object Remove (string key, CacheItemRemovedReason reason)
  143. {
  144. lock (cache) {
  145. CacheItem it = (CacheItem) cache [key];
  146. if (it != null) {
  147. if (it.Dependency != null) {
  148. it.Dependency.DependencyChanged -= new EventHandler (OnDependencyChanged);
  149. it.Dependency.Dispose ();
  150. }
  151. cache.Remove (key);
  152. if (it.OnRemoveCallback != null)
  153. it.OnRemoveCallback (key, it.Value, reason);
  154. return it.Value;
  155. }
  156. else
  157. return null;
  158. }
  159. }
  160. public IDictionaryEnumerator GetEnumerator ()
  161. {
  162. ArrayList list = new ArrayList ();
  163. lock (cache) {
  164. foreach (CacheItem it in cache.Values)
  165. if (!it.Private)
  166. list.Add (it);
  167. }
  168. return new CacheItemEnumerator (list);
  169. }
  170. IEnumerator IEnumerable.GetEnumerator ()
  171. {
  172. return GetEnumerator ();
  173. }
  174. void TimerRun (object ob)
  175. {
  176. CheckExpiration ();
  177. }
  178. void OnDependencyChanged (object o, EventArgs a)
  179. {
  180. CheckExpiration ();
  181. }
  182. internal void CheckExpiration ()
  183. {
  184. ArrayList list;
  185. lock (cache) {
  186. list = new ArrayList ();
  187. list.AddRange (cache.Values);
  188. needsTimer = false;
  189. }
  190. DateTime now = DateTime.Now;
  191. foreach (CacheItem it in list) {
  192. if (it.Dependency != null && it.Dependency.HasChanged)
  193. Remove (it.Key, CacheItemRemovedReason.DependencyChanged);
  194. else if (it.AbsoluteExpiration != NoAbsoluteExpiration) {
  195. if (now >= it.AbsoluteExpiration)
  196. Remove (it.Key, CacheItemRemovedReason.Expired);
  197. else
  198. needsTimer = true;
  199. }
  200. }
  201. lock (cache) {
  202. if (!needsTimer && timer != null) {
  203. timer.Dispose ();
  204. timer = null;
  205. }
  206. }
  207. }
  208. internal DateTime GetKeyLastChange (string key)
  209. {
  210. lock (cache) {
  211. CacheItem it = (CacheItem) cache [key];
  212. if (it == null) return DateTime.MaxValue;
  213. return it.LastChange;
  214. }
  215. }
  216. }
  217. class CacheItem
  218. {
  219. public object Value;
  220. public string Key;
  221. public CacheDependency Dependency;
  222. public DateTime AbsoluteExpiration;
  223. public TimeSpan SlidingExpiration;
  224. public CacheItemPriority Priority;
  225. public CacheItemRemovedCallback OnRemoveCallback;
  226. public DateTime LastChange;
  227. public bool Private;
  228. }
  229. class CacheItemEnumerator: IDictionaryEnumerator
  230. {
  231. ArrayList list;
  232. int pos = -1;
  233. public CacheItemEnumerator (ArrayList list)
  234. {
  235. this.list = list;
  236. }
  237. CacheItem Item {
  238. get {
  239. if (pos < 0 || pos >= list.Count)
  240. throw new InvalidOperationException ();
  241. return (CacheItem) list [pos];
  242. }
  243. }
  244. public DictionaryEntry Entry {
  245. get { return new DictionaryEntry (Item.Key, Item.Value); }
  246. }
  247. public object Key {
  248. get { return Item.Key; }
  249. }
  250. public object Value {
  251. get { return Item.Value; }
  252. }
  253. public object Current {
  254. get { return Entry; }
  255. }
  256. public bool MoveNext ()
  257. {
  258. return (++pos < list.Count);
  259. }
  260. public void Reset ()
  261. {
  262. pos = -1;
  263. }
  264. }
  265. }