Cache.cs 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  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.Dependency != null && it.Dependency.HasChanged) {
  73. Remove (it.Key, CacheItemRemovedReason.DependencyChanged);
  74. return null;
  75. }
  76. if (it.SlidingExpiration != NoSlidingExpiration) {
  77. it.AbsoluteExpiration = DateTime.Now + it.SlidingExpiration;
  78. } else if (DateTime.Now >= it.AbsoluteExpiration) {
  79. Remove (key, CacheItemRemovedReason.Expired);
  80. return null;
  81. }
  82. return it.Value;
  83. }
  84. }
  85. public void Insert (string key, object value)
  86. {
  87. Insert (key, value, null, NoAbsoluteExpiration, NoSlidingExpiration, CacheItemPriority.Normal, null);
  88. }
  89. public void Insert (string key, object value, CacheDependency dependencies)
  90. {
  91. Insert (key, value, dependencies, NoAbsoluteExpiration, NoSlidingExpiration, CacheItemPriority.Normal, null);
  92. }
  93. public void Insert (string key, object value, CacheDependency dependencies, DateTime absoluteExpiration, TimeSpan slidingExpiration)
  94. {
  95. Insert (key, value, dependencies, absoluteExpiration, slidingExpiration, CacheItemPriority.Normal, null);
  96. }
  97. public void Insert (string key, object value, CacheDependency dependencies, DateTime absoluteExpiration, TimeSpan slidingExpiration, CacheItemPriority priority, CacheItemRemovedCallback onRemoveCallback)
  98. {
  99. InsertInternal (key, value, dependencies, absoluteExpiration, slidingExpiration, priority, onRemoveCallback, false);
  100. }
  101. internal void InsertPrivate (string key, object value, CacheDependency dependencies)
  102. {
  103. InsertInternal (key, value, dependencies, NoAbsoluteExpiration, NoSlidingExpiration, CacheItemPriority.Normal, null, true);
  104. }
  105. internal void InsertPrivate (string key, object value, CacheDependency dependencies, DateTime absoluteExpiration, TimeSpan slidingExpiration, CacheItemPriority priority, CacheItemRemovedCallback onRemoveCallback)
  106. {
  107. InsertInternal (key, value, dependencies, absoluteExpiration, slidingExpiration, priority, onRemoveCallback, true);
  108. }
  109. void InsertInternal (string key, object value, CacheDependency dependency, DateTime absoluteExpiration, TimeSpan slidingExpiration, CacheItemPriority priority, CacheItemRemovedCallback onRemoveCallback, bool isprivate)
  110. {
  111. if (key == null)
  112. throw new ArgumentNullException ("key");
  113. if (value == null)
  114. throw new ArgumentNullException ("value");
  115. if (slidingExpiration < TimeSpan.Zero || slidingExpiration > TimeSpan.FromDays (365))
  116. throw new ArgumentNullException ("slidingExpiration");
  117. if (absoluteExpiration != NoAbsoluteExpiration && slidingExpiration != NoSlidingExpiration)
  118. throw new ArgumentException ("Both absoluteExpiration and slidingExpiration are specified");
  119. CacheItem ci = new CacheItem ();
  120. ci.Value = value;
  121. ci.Key = key;
  122. ci.Private = isprivate;
  123. if (dependency != null) {
  124. ci.Dependency = dependency;
  125. dependency.DependencyChanged += new EventHandler (OnDependencyChanged);
  126. dependency.SetCache (this);
  127. }
  128. ci.SlidingExpiration = slidingExpiration;
  129. if (slidingExpiration != NoSlidingExpiration)
  130. ci.AbsoluteExpiration = DateTime.Now + slidingExpiration;
  131. else
  132. ci.AbsoluteExpiration = absoluteExpiration;
  133. ci.Priority = priority;
  134. ci.OnRemoveCallback = onRemoveCallback;
  135. lock (cache) {
  136. cache [key] = ci;
  137. ci.LastChange = DateTime.Now;
  138. if (ci.AbsoluteExpiration != NoAbsoluteExpiration && timer == null) {
  139. timer = new Timer (new TimerCallback (TimerRun), null, 60000, 60000);
  140. needsTimer = true;
  141. }
  142. }
  143. }
  144. public object Remove (string key)
  145. {
  146. return Remove (key, CacheItemRemovedReason.Removed);
  147. }
  148. object Remove (string key, CacheItemRemovedReason reason)
  149. {
  150. lock (cache) {
  151. CacheItem it = (CacheItem) cache [key];
  152. if (it != null) {
  153. if (it.Dependency != null) {
  154. #if NET_2_0
  155. it.Dependency.SetCache (null);
  156. #endif
  157. it.Dependency.DependencyChanged -= new EventHandler (OnDependencyChanged);
  158. it.Dependency.Dispose ();
  159. }
  160. cache.Remove (key);
  161. if (it.OnRemoveCallback != null) {
  162. try {
  163. it.OnRemoveCallback (key, it.Value, reason);
  164. } catch {
  165. //TODO: anything to be done here?
  166. }
  167. }
  168. return it.Value;
  169. }
  170. else
  171. return null;
  172. }
  173. }
  174. // Used when shutting down the application so that
  175. // session_end events are sent for all sessions.
  176. internal void InvokePrivateCallbacks ()
  177. {
  178. CacheItemRemovedReason reason = CacheItemRemovedReason.Removed;
  179. lock (cache) {
  180. foreach (string key in cache.Keys) {
  181. CacheItem item = (CacheItem) cache [key];
  182. if (item == null || false == item.Private)
  183. continue;
  184. if (item.OnRemoveCallback != null) {
  185. try {
  186. item.OnRemoveCallback (key, item.Value, reason);
  187. } catch {
  188. //TODO: anything to be done here?
  189. }
  190. }
  191. }
  192. }
  193. }
  194. public IDictionaryEnumerator GetEnumerator ()
  195. {
  196. ArrayList list = new ArrayList ();
  197. lock (cache) {
  198. foreach (CacheItem it in cache.Values)
  199. if (!it.Private)
  200. list.Add (it);
  201. }
  202. return new CacheItemEnumerator (list);
  203. }
  204. IEnumerator IEnumerable.GetEnumerator ()
  205. {
  206. return GetEnumerator ();
  207. }
  208. void TimerRun (object ob)
  209. {
  210. CheckExpiration ();
  211. }
  212. void OnDependencyChanged (object o, EventArgs a)
  213. {
  214. CheckExpiration ();
  215. }
  216. internal void CheckExpiration ()
  217. {
  218. ArrayList list;
  219. lock (cache) {
  220. list = new ArrayList ();
  221. list.AddRange (cache.Values);
  222. needsTimer = false;
  223. }
  224. DateTime now = DateTime.Now;
  225. foreach (CacheItem it in list) {
  226. if (it.Dependency != null && it.Dependency.HasChanged)
  227. Remove (it.Key, CacheItemRemovedReason.DependencyChanged);
  228. else if (it.AbsoluteExpiration != NoAbsoluteExpiration) {
  229. if (now >= it.AbsoluteExpiration)
  230. Remove (it.Key, CacheItemRemovedReason.Expired);
  231. else
  232. needsTimer = true;
  233. }
  234. }
  235. lock (cache) {
  236. if (!needsTimer && timer != null) {
  237. timer.Dispose ();
  238. timer = null;
  239. }
  240. }
  241. }
  242. internal DateTime GetKeyLastChange (string key)
  243. {
  244. lock (cache) {
  245. CacheItem it = (CacheItem) cache [key];
  246. if (it == null) return DateTime.MaxValue;
  247. return it.LastChange;
  248. }
  249. }
  250. }
  251. class CacheItem
  252. {
  253. public object Value;
  254. public string Key;
  255. public CacheDependency Dependency;
  256. public DateTime AbsoluteExpiration;
  257. public TimeSpan SlidingExpiration;
  258. public CacheItemPriority Priority;
  259. public CacheItemRemovedCallback OnRemoveCallback;
  260. public DateTime LastChange;
  261. public bool Private;
  262. }
  263. class CacheItemEnumerator: IDictionaryEnumerator
  264. {
  265. ArrayList list;
  266. int pos = -1;
  267. public CacheItemEnumerator (ArrayList list)
  268. {
  269. this.list = list;
  270. }
  271. CacheItem Item {
  272. get {
  273. if (pos < 0 || pos >= list.Count)
  274. throw new InvalidOperationException ();
  275. return (CacheItem) list [pos];
  276. }
  277. }
  278. public DictionaryEntry Entry {
  279. get { return new DictionaryEntry (Item.Key, Item.Value); }
  280. }
  281. public object Key {
  282. get { return Item.Key; }
  283. }
  284. public object Value {
  285. get { return Item.Value; }
  286. }
  287. public object Current {
  288. get { return Entry; }
  289. }
  290. public bool MoveNext ()
  291. {
  292. return (++pos < list.Count);
  293. }
  294. public void Reset ()
  295. {
  296. pos = -1;
  297. }
  298. }
  299. }