Cache.cs 8.8 KB

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