Cache.cs 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  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. it.Dependency.DependencyChanged -= new EventHandler (OnDependencyChanged);
  155. it.Dependency.Dispose ();
  156. }
  157. cache.Remove (key);
  158. if (it.OnRemoveCallback != null) {
  159. try {
  160. it.OnRemoveCallback (key, it.Value, reason);
  161. } catch {
  162. //TODO: anything to be done here?
  163. }
  164. }
  165. return it.Value;
  166. }
  167. else
  168. return null;
  169. }
  170. }
  171. // Used when shutting down the application so that
  172. // session_end events are sent for all sessions.
  173. internal void InvokePrivateCallbacks ()
  174. {
  175. CacheItemRemovedReason reason = CacheItemRemovedReason.Removed;
  176. lock (cache) {
  177. foreach (string key in cache.Keys) {
  178. CacheItem item = (CacheItem) cache [key];
  179. if (item == null || false == item.Private)
  180. continue;
  181. if (item.OnRemoveCallback != null) {
  182. try {
  183. item.OnRemoveCallback (key, item.Value, reason);
  184. } catch {
  185. //TODO: anything to be done here?
  186. }
  187. }
  188. }
  189. }
  190. }
  191. public IDictionaryEnumerator GetEnumerator ()
  192. {
  193. ArrayList list = new ArrayList ();
  194. lock (cache) {
  195. foreach (CacheItem it in cache.Values)
  196. if (!it.Private)
  197. list.Add (it);
  198. }
  199. return new CacheItemEnumerator (list);
  200. }
  201. IEnumerator IEnumerable.GetEnumerator ()
  202. {
  203. return GetEnumerator ();
  204. }
  205. void TimerRun (object ob)
  206. {
  207. CheckExpiration ();
  208. }
  209. void OnDependencyChanged (object o, EventArgs a)
  210. {
  211. CheckExpiration ();
  212. }
  213. internal void CheckExpiration ()
  214. {
  215. ArrayList list;
  216. lock (cache) {
  217. list = new ArrayList ();
  218. list.AddRange (cache.Values);
  219. needsTimer = false;
  220. }
  221. DateTime now = DateTime.Now;
  222. foreach (CacheItem it in list) {
  223. if (it.Dependency != null && it.Dependency.HasChanged)
  224. Remove (it.Key, CacheItemRemovedReason.DependencyChanged);
  225. else if (it.AbsoluteExpiration != NoAbsoluteExpiration) {
  226. if (now >= it.AbsoluteExpiration)
  227. Remove (it.Key, CacheItemRemovedReason.Expired);
  228. else
  229. needsTimer = true;
  230. }
  231. }
  232. lock (cache) {
  233. if (!needsTimer && timer != null) {
  234. timer.Dispose ();
  235. timer = null;
  236. }
  237. }
  238. }
  239. internal DateTime GetKeyLastChange (string key)
  240. {
  241. lock (cache) {
  242. CacheItem it = (CacheItem) cache [key];
  243. if (it == null) return DateTime.MaxValue;
  244. return it.LastChange;
  245. }
  246. }
  247. }
  248. class CacheItem
  249. {
  250. public object Value;
  251. public string Key;
  252. public CacheDependency Dependency;
  253. public DateTime AbsoluteExpiration;
  254. public TimeSpan SlidingExpiration;
  255. public CacheItemPriority Priority;
  256. public CacheItemRemovedCallback OnRemoveCallback;
  257. public DateTime LastChange;
  258. public bool Private;
  259. }
  260. class CacheItemEnumerator: IDictionaryEnumerator
  261. {
  262. ArrayList list;
  263. int pos = -1;
  264. public CacheItemEnumerator (ArrayList list)
  265. {
  266. this.list = list;
  267. }
  268. CacheItem Item {
  269. get {
  270. if (pos < 0 || pos >= list.Count)
  271. throw new InvalidOperationException ();
  272. return (CacheItem) list [pos];
  273. }
  274. }
  275. public DictionaryEntry Entry {
  276. get { return new DictionaryEntry (Item.Key, Item.Value); }
  277. }
  278. public object Key {
  279. get { return Item.Key; }
  280. }
  281. public object Value {
  282. get { return Item.Value; }
  283. }
  284. public object Current {
  285. get { return Entry; }
  286. }
  287. public bool MoveNext ()
  288. {
  289. return (++pos < list.Count);
  290. }
  291. public void Reset ()
  292. {
  293. pos = -1;
  294. }
  295. }
  296. }