Cache.cs 9.4 KB

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