Cache.cs 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. //
  2. // System.Web.Caching
  3. //
  4. // Author:
  5. // Patrik Torstensson
  6. // Daniel Cazzulino [DHC] ([email protected])
  7. //
  8. //
  9. // Permission is hereby granted, free of charge, to any person obtaining
  10. // a copy of this software and associated documentation files (the
  11. // "Software"), to deal in the Software without restriction, including
  12. // without limitation the rights to use, copy, modify, merge, publish,
  13. // distribute, sublicense, and/or sell copies of the Software, and to
  14. // permit persons to whom the Software is furnished to do so, subject to
  15. // the following conditions:
  16. //
  17. // The above copyright notice and this permission notice shall be
  18. // included in all copies or substantial portions of the Software.
  19. //
  20. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  21. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  22. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  23. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  24. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  25. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  26. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  27. //
  28. using System;
  29. using System.Collections;
  30. using System.Threading;
  31. namespace System.Web.Caching {
  32. public sealed class Cache : IEnumerable {
  33. public static readonly DateTime NoAbsoluteExpiration = DateTime.MaxValue;
  34. public static readonly TimeSpan NoSlidingExpiration = TimeSpan.Zero;
  35. // Helper objects
  36. private CacheExpires _objExpires;
  37. // The data storage
  38. private Hashtable _arrEntries;
  39. private ReaderWriterLock _lockEntries;
  40. private int _nItems;
  41. static private TimeSpan _datetimeOneYear = TimeSpan.FromDays (365);
  42. public Cache () {
  43. _nItems = 0;
  44. _lockEntries = new ReaderWriterLock ();
  45. _arrEntries = new Hashtable ();
  46. _objExpires = new CacheExpires (this);
  47. }
  48. #if TARGET_J2EE
  49. internal void Destroy()
  50. {
  51. _arrEntries = null;
  52. _objExpires.Close();
  53. }
  54. #endif
  55. private IDictionaryEnumerator CreateEnumerator () {
  56. Hashtable objTable;
  57. //Locking with -1 provides a non-expiring lock.
  58. _lockEntries.AcquireReaderLock (-1);
  59. try {
  60. // Create a new hashtable to return as collection of public items
  61. objTable = new Hashtable (_arrEntries.Count);
  62. foreach (DictionaryEntry objEntry in _arrEntries) {
  63. if (objEntry.Key == null)
  64. continue;
  65. CacheEntry entry = (CacheEntry) objEntry.Value;
  66. if (entry.IsPublic)
  67. objTable.Add (objEntry.Key, entry.Item);
  68. }
  69. } finally {
  70. _lockEntries.ReleaseReaderLock ();
  71. }
  72. return objTable.GetEnumerator ();
  73. }
  74. IEnumerator IEnumerable.GetEnumerator () {
  75. return GetEnumerator ();
  76. }
  77. public IDictionaryEnumerator GetEnumerator () {
  78. return CreateEnumerator ();
  79. }
  80. internal void Touch(string strKey) {
  81. GetEntry (strKey);
  82. }
  83. public object Add (string strKey, object objItem, CacheDependency objDependency,
  84. DateTime absolutExpiration, TimeSpan slidingExpiration,
  85. CacheItemPriority enumPriority, CacheItemRemovedCallback eventRemoveCallback) {
  86. return Add (strKey, objItem, objDependency, absolutExpiration,
  87. slidingExpiration, enumPriority, eventRemoveCallback, true, false);
  88. }
  89. private object Add (string strKey,
  90. object objItem,
  91. CacheDependency objDependency,
  92. DateTime absolutExpiration,
  93. TimeSpan slidingExpiration,
  94. CacheItemPriority enumPriority,
  95. CacheItemRemovedCallback eventRemoveCallback,
  96. bool pub,
  97. bool overwrite) {
  98. if (strKey == null)
  99. throw new ArgumentNullException ("strKey");
  100. if (objItem == null)
  101. throw new ArgumentNullException ("objItem");
  102. if (slidingExpiration > _datetimeOneYear)
  103. throw new ArgumentOutOfRangeException ("slidingExpiration");
  104. CacheEntry objEntry;
  105. CacheEntry objOldEntry = null;
  106. long longHitRange = 10000;
  107. // todo: check decay and make up the minHit range
  108. objEntry = new CacheEntry ( this,
  109. strKey,
  110. objItem,
  111. objDependency,
  112. eventRemoveCallback,
  113. absolutExpiration,
  114. slidingExpiration,
  115. longHitRange,
  116. pub,
  117. enumPriority);
  118. _lockEntries.AcquireWriterLock (-1);
  119. try {
  120. _nItems++;
  121. if (_arrEntries.Contains (strKey)) {
  122. if (overwrite)
  123. objOldEntry = _arrEntries [strKey] as CacheEntry;
  124. else
  125. return null;
  126. }
  127. objEntry.Hit ();
  128. _arrEntries [strKey] = objEntry;
  129. // If we have any kind of expiration add into the CacheExpires
  130. // Do this under the lock so no-one can retrieve the objEntry
  131. // before it is fully initialized.
  132. if (objEntry.HasSlidingExpiration || objEntry.HasAbsoluteExpiration) {
  133. if (objEntry.HasSlidingExpiration)
  134. objEntry.Expires = DateTime.UtcNow.Ticks + objEntry.SlidingExpiration;
  135. _objExpires.Add (objEntry);
  136. }
  137. } finally {
  138. _lockEntries.ReleaseLock ();
  139. }
  140. if (objOldEntry != null) {
  141. if (objOldEntry.HasAbsoluteExpiration || objOldEntry.HasSlidingExpiration)
  142. _objExpires.Remove (objOldEntry);
  143. objOldEntry.Close (CacheItemRemovedReason.Removed);
  144. }
  145. return objEntry.Item;
  146. }
  147. public void Insert (string strKey, object objItem) {
  148. Add (strKey, objItem, null, NoAbsoluteExpiration, NoSlidingExpiration,
  149. CacheItemPriority.Default, null, true, true);
  150. }
  151. public void Insert (string strKey, object objItem, CacheDependency objDependency) {
  152. Add (strKey, objItem, objDependency, NoAbsoluteExpiration, NoSlidingExpiration,
  153. CacheItemPriority.Default, null, true, true);
  154. }
  155. public void Insert (string strKey, object objItem, CacheDependency objDependency,
  156. DateTime absolutExpiration, TimeSpan slidingExpiration) {
  157. Add (strKey, objItem, objDependency, absolutExpiration, slidingExpiration,
  158. CacheItemPriority.Default, null, true, true);
  159. }
  160. public void Insert (string strKey, object objItem, CacheDependency objDependency,
  161. DateTime absolutExpiration, TimeSpan slidingExpiration,
  162. CacheItemPriority enumPriority, CacheItemRemovedCallback eventRemoveCallback) {
  163. Add (strKey, objItem, objDependency, absolutExpiration, slidingExpiration,
  164. enumPriority, eventRemoveCallback, true, true);
  165. }
  166. // Called from other internal System.Web methods to add non-public objects into
  167. // cache, like output cache etc
  168. internal void InsertPrivate (string strKey, object objItem, CacheDependency objDependency,
  169. DateTime absolutExpiration, TimeSpan slidingExpiration,
  170. CacheItemPriority enumPriority, CacheItemRemovedCallback eventRemoveCallback)
  171. {
  172. Add (strKey, objItem, objDependency, absolutExpiration, slidingExpiration,
  173. enumPriority, eventRemoveCallback, false, true);
  174. }
  175. internal void InsertPrivate (string strKey, object objItem, CacheDependency objDependency)
  176. {
  177. Add (strKey, objItem, objDependency, NoAbsoluteExpiration, NoSlidingExpiration,
  178. CacheItemPriority.Default, null, false, true);
  179. }
  180. public object Remove (string strKey) {
  181. return Remove (strKey, CacheItemRemovedReason.Removed);
  182. }
  183. internal object Remove (string strKey, CacheItemRemovedReason enumReason) {
  184. CacheEntry objEntry = null;
  185. if (strKey == null)
  186. throw new ArgumentNullException ("strKey");
  187. _lockEntries.AcquireWriterLock (-1);
  188. try {
  189. objEntry = _arrEntries [strKey] as CacheEntry;
  190. if (null == objEntry)
  191. return null;
  192. _arrEntries.Remove (strKey);
  193. _nItems--;
  194. }
  195. finally {
  196. _lockEntries.ReleaseWriterLock ();
  197. }
  198. if (objEntry.HasAbsoluteExpiration || objEntry.HasSlidingExpiration)
  199. _objExpires.Remove (objEntry);
  200. objEntry.Close (enumReason);
  201. return objEntry.Item;
  202. }
  203. public object Get (string strKey) {
  204. CacheEntry objEntry = GetEntry (strKey);
  205. if (objEntry == null)
  206. return null;
  207. return objEntry.Item;
  208. }
  209. internal CacheEntry GetEntry (string strKey) {
  210. CacheEntry objEntry = null;
  211. long ticksNow = DateTime.UtcNow.Ticks;
  212. if (strKey == null)
  213. throw new ArgumentNullException ("strKey");
  214. _lockEntries.AcquireReaderLock (-1);
  215. try {
  216. objEntry = _arrEntries [strKey] as CacheEntry;
  217. if (null == objEntry)
  218. return null;
  219. }
  220. finally {
  221. _lockEntries.ReleaseReaderLock ();
  222. }
  223. if (objEntry.HasSlidingExpiration || objEntry.HasAbsoluteExpiration) {
  224. if (objEntry.Expires < ticksNow) {
  225. Remove (strKey, CacheItemRemovedReason.Expired);
  226. return null;
  227. }
  228. }
  229. objEntry.Hit ();
  230. if (objEntry.HasSlidingExpiration) {
  231. objEntry.Expires = ticksNow + objEntry.SlidingExpiration;
  232. }
  233. return objEntry;
  234. }
  235. public int Count {
  236. get { return _nItems; }
  237. }
  238. public object this [string strKey] {
  239. get {
  240. return Get (strKey);
  241. }
  242. set {
  243. Insert (strKey, value);
  244. }
  245. }
  246. }
  247. }