2
0

Cache.cs 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372
  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. #if NET_2_0
  33. using System.Collections.Generic;
  34. #endif
  35. namespace System.Web.Caching
  36. {
  37. // CAS - no InheritanceDemand here as the class is sealed
  38. [AspNetHostingPermission (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
  39. public sealed class Cache: IEnumerable
  40. {
  41. #if NET_2_0
  42. Dictionary <string, CacheItem> cache;
  43. #else
  44. Hashtable cache;
  45. #endif
  46. Timer timer;
  47. bool needsTimer;
  48. public static readonly DateTime NoAbsoluteExpiration = DateTime.MaxValue;
  49. public static readonly TimeSpan NoSlidingExpiration = TimeSpan.Zero;
  50. public Cache ()
  51. {
  52. #if NET_2_0
  53. cache = new Dictionary <string, CacheItem> ();
  54. #else
  55. cache = new Hashtable ();
  56. #endif
  57. }
  58. public int Count {
  59. get { return cache.Count; }
  60. }
  61. public object this [string key] {
  62. get { return Get (key); }
  63. set { Insert (key, value); }
  64. }
  65. public object Add (string key, object value, CacheDependency dependencies, DateTime absoluteExpiration, TimeSpan slidingExpiration, CacheItemPriority priority, CacheItemRemovedCallback onRemoveCallback)
  66. {
  67. if (key == null)
  68. throw new ArgumentNullException ("key");
  69. lock (cache) {
  70. CacheItem it;
  71. #if NET_2_0
  72. cache.TryGetValue (key, out it);
  73. #else
  74. it = (CacheItem) cache [key];
  75. #endif
  76. if (it != null)
  77. return it.Value;
  78. Insert (key, value, dependencies, absoluteExpiration, slidingExpiration, priority, onRemoveCallback);
  79. }
  80. return null;
  81. }
  82. public object Get (string key)
  83. {
  84. lock (cache) {
  85. CacheItem it;
  86. #if NET_2_0
  87. if (!cache.TryGetValue (key, out it))
  88. return null;
  89. #else
  90. it = (CacheItem) cache [key];
  91. if (it == null)
  92. return null;
  93. #endif
  94. if (it.Dependency != null && it.Dependency.HasChanged) {
  95. Remove (it.Key, CacheItemRemovedReason.DependencyChanged);
  96. return null;
  97. }
  98. if (it.SlidingExpiration != NoSlidingExpiration) {
  99. it.AbsoluteExpiration = DateTime.Now + it.SlidingExpiration;
  100. } else if (DateTime.Now >= it.AbsoluteExpiration) {
  101. Remove (key, CacheItemRemovedReason.Expired);
  102. return null;
  103. }
  104. return it.Value;
  105. }
  106. }
  107. public void Insert (string key, object value)
  108. {
  109. Insert (key, value, null, NoAbsoluteExpiration, NoSlidingExpiration, CacheItemPriority.Normal, null);
  110. }
  111. public void Insert (string key, object value, CacheDependency dependencies)
  112. {
  113. Insert (key, value, dependencies, NoAbsoluteExpiration, NoSlidingExpiration, CacheItemPriority.Normal, null);
  114. }
  115. public void Insert (string key, object value, CacheDependency dependencies, DateTime absoluteExpiration, TimeSpan slidingExpiration)
  116. {
  117. Insert (key, value, dependencies, absoluteExpiration, slidingExpiration, CacheItemPriority.Normal, null);
  118. }
  119. public void Insert (string key, object value, CacheDependency dependencies, DateTime absoluteExpiration, TimeSpan slidingExpiration, CacheItemPriority priority, CacheItemRemovedCallback onRemoveCallback)
  120. {
  121. if (key == null)
  122. throw new ArgumentNullException ("key");
  123. if (value == null)
  124. throw new ArgumentNullException ("value");
  125. if (slidingExpiration < TimeSpan.Zero || slidingExpiration > TimeSpan.FromDays (365))
  126. throw new ArgumentNullException ("slidingExpiration");
  127. if (absoluteExpiration != NoAbsoluteExpiration && slidingExpiration != NoSlidingExpiration)
  128. throw new ArgumentException ("Both absoluteExpiration and slidingExpiration are specified");
  129. CacheItem ci = new CacheItem ();
  130. ci.Value = value;
  131. ci.Key = key;
  132. if (dependencies != null) {
  133. ci.Dependency = dependencies;
  134. dependencies.DependencyChanged += new EventHandler (OnDependencyChanged);
  135. dependencies.SetCache (this);
  136. }
  137. ci.SlidingExpiration = slidingExpiration;
  138. if (slidingExpiration != NoSlidingExpiration)
  139. ci.AbsoluteExpiration = DateTime.Now + slidingExpiration;
  140. else
  141. ci.AbsoluteExpiration = absoluteExpiration;
  142. ci.Priority = priority;
  143. ci.OnRemoveCallback = onRemoveCallback;
  144. lock (cache) {
  145. cache [key] = ci;
  146. ci.LastChange = DateTime.Now;
  147. if (ci.AbsoluteExpiration != NoAbsoluteExpiration && timer == null) {
  148. timer = new Timer (new TimerCallback (TimerRun), null, 60000, 60000);
  149. needsTimer = true;
  150. }
  151. }
  152. }
  153. public object Remove (string key)
  154. {
  155. return Remove (key, CacheItemRemovedReason.Removed);
  156. }
  157. object Remove (string key, CacheItemRemovedReason reason)
  158. {
  159. lock (cache) {
  160. CacheItem it;
  161. #if NET_2_0
  162. cache.TryGetValue (key, out it);
  163. #else
  164. it = (CacheItem) cache [key];
  165. #endif
  166. if (it != null) {
  167. if (it.Dependency != null) {
  168. #if NET_2_0
  169. it.Dependency.SetCache (null);
  170. #endif
  171. it.Dependency.DependencyChanged -= new EventHandler (OnDependencyChanged);
  172. it.Dependency.Dispose ();
  173. }
  174. cache.Remove (key);
  175. if (it.OnRemoveCallback != null) {
  176. try {
  177. it.OnRemoveCallback (key, it.Value, reason);
  178. } catch {
  179. //TODO: anything to be done here?
  180. }
  181. }
  182. return it.Value;
  183. } else
  184. return null;
  185. }
  186. }
  187. // Used when shutting down the application so that
  188. // session_end events are sent for all sessions.
  189. internal void InvokePrivateCallbacks ()
  190. {
  191. CacheItemRemovedReason reason = CacheItemRemovedReason.Removed;
  192. lock (cache) {
  193. foreach (string key in cache.Keys) {
  194. CacheItem item;
  195. #if NET_2_0
  196. cache.TryGetValue (key, out item);
  197. #else
  198. item = (CacheItem) cache [key];
  199. #endif
  200. if (item != null && item.OnRemoveCallback != null) {
  201. try {
  202. item.OnRemoveCallback (key, item.Value, reason);
  203. } catch {
  204. //TODO: anything to be done here?
  205. }
  206. }
  207. }
  208. }
  209. }
  210. public IDictionaryEnumerator GetEnumerator ()
  211. {
  212. ArrayList list = new ArrayList ();
  213. lock (cache) {
  214. #if NET_2_0
  215. foreach (CacheItem it in cache.Values)
  216. list.Add (it);
  217. #else
  218. list.AddRange (cache.Values);
  219. #endif
  220. }
  221. return new CacheItemEnumerator (list);
  222. }
  223. IEnumerator IEnumerable.GetEnumerator ()
  224. {
  225. return GetEnumerator ();
  226. }
  227. void TimerRun (object ob)
  228. {
  229. CheckExpiration ();
  230. }
  231. void OnDependencyChanged (object o, EventArgs a)
  232. {
  233. CheckExpiration ();
  234. }
  235. internal void CheckExpiration ()
  236. {
  237. ArrayList list;
  238. lock (cache) {
  239. list = new ArrayList ();
  240. #if NET_2_0
  241. foreach (CacheItem it in cache.Values)
  242. list.Add (it);
  243. #else
  244. list.AddRange (cache.Values);
  245. #endif
  246. needsTimer = false;
  247. }
  248. DateTime now = DateTime.Now;
  249. foreach (CacheItem it in list) {
  250. if (it.Dependency != null && it.Dependency.HasChanged)
  251. Remove (it.Key, CacheItemRemovedReason.DependencyChanged);
  252. else if (it.AbsoluteExpiration != NoAbsoluteExpiration) {
  253. if (now >= it.AbsoluteExpiration)
  254. Remove (it.Key, CacheItemRemovedReason.Expired);
  255. else
  256. needsTimer = true;
  257. }
  258. }
  259. lock (cache) {
  260. if (!needsTimer && timer != null) {
  261. timer.Dispose ();
  262. timer = null;
  263. }
  264. }
  265. }
  266. internal DateTime GetKeyLastChange (string key)
  267. {
  268. lock (cache) {
  269. CacheItem it;
  270. #if NET_2_0
  271. cache.TryGetValue (key, out it);
  272. #else
  273. it = (CacheItem) cache [key];
  274. #endif
  275. if (it == null) return DateTime.MaxValue;
  276. return it.LastChange;
  277. }
  278. }
  279. }
  280. class CacheItem
  281. {
  282. public object Value;
  283. public string Key;
  284. public CacheDependency Dependency;
  285. public DateTime AbsoluteExpiration;
  286. public TimeSpan SlidingExpiration;
  287. public CacheItemPriority Priority;
  288. public CacheItemRemovedCallback OnRemoveCallback;
  289. public DateTime LastChange;
  290. }
  291. class CacheItemEnumerator: IDictionaryEnumerator
  292. {
  293. ArrayList list;
  294. int pos = -1;
  295. public CacheItemEnumerator (ArrayList list)
  296. {
  297. this.list = list;
  298. }
  299. CacheItem Item {
  300. get {
  301. if (pos < 0 || pos >= list.Count)
  302. throw new InvalidOperationException ();
  303. return (CacheItem) list [pos];
  304. }
  305. }
  306. public DictionaryEntry Entry {
  307. get { return new DictionaryEntry (Item.Key, Item.Value); }
  308. }
  309. public object Key {
  310. get { return Item.Key; }
  311. }
  312. public object Value {
  313. get { return Item.Value; }
  314. }
  315. public object Current {
  316. get { return Entry; }
  317. }
  318. public bool MoveNext ()
  319. {
  320. return (++pos < list.Count);
  321. }
  322. public void Reset ()
  323. {
  324. pos = -1;
  325. }
  326. }
  327. }