Cache.cs 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  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. private IDictionaryEnumerator CreateEnumerator () {
  49. Hashtable objTable;
  50. //Locking with -1 provides a non-expiring lock.
  51. _lockEntries.AcquireReaderLock (-1);
  52. try {
  53. // Create a new hashtable to return as collection of public items
  54. objTable = new Hashtable (_arrEntries.Count);
  55. foreach (DictionaryEntry objEntry in _arrEntries) {
  56. if (objEntry.Key == null)
  57. continue;
  58. CacheEntry entry = (CacheEntry) objEntry.Value;
  59. if (entry.IsPublic)
  60. objTable.Add (objEntry.Key, entry.Item);
  61. }
  62. } finally {
  63. _lockEntries.ReleaseReaderLock ();
  64. }
  65. return objTable.GetEnumerator ();
  66. }
  67. IEnumerator IEnumerable.GetEnumerator () {
  68. return GetEnumerator ();
  69. }
  70. public IDictionaryEnumerator GetEnumerator () {
  71. return CreateEnumerator ();
  72. }
  73. internal void Touch(string strKey) {
  74. GetEntry (strKey);
  75. }
  76. public object Add (string strKey, object objItem, CacheDependency objDependency,
  77. DateTime absolutExpiration, TimeSpan slidingExpiration,
  78. CacheItemPriority enumPriority, CacheItemRemovedCallback eventRemoveCallback) {
  79. return Add (strKey, objItem, objDependency, absolutExpiration,
  80. slidingExpiration, enumPriority, eventRemoveCallback, true, false);
  81. }
  82. private object Add (string strKey,
  83. object objItem,
  84. CacheDependency objDependency,
  85. DateTime absolutExpiration,
  86. TimeSpan slidingExpiration,
  87. CacheItemPriority enumPriority,
  88. CacheItemRemovedCallback eventRemoveCallback,
  89. bool pub,
  90. bool overwrite) {
  91. if (strKey == null)
  92. throw new ArgumentNullException ("strKey");
  93. if (objItem == null)
  94. throw new ArgumentNullException ("objItem");
  95. if (slidingExpiration > _datetimeOneYear)
  96. throw new ArgumentOutOfRangeException ("slidingExpiration");
  97. CacheEntry objEntry;
  98. CacheEntry objOldEntry = null;
  99. long longHitRange = 10000;
  100. // todo: check decay and make up the minHit range
  101. objEntry = new CacheEntry ( this,
  102. strKey,
  103. objItem,
  104. objDependency,
  105. eventRemoveCallback,
  106. absolutExpiration,
  107. slidingExpiration,
  108. longHitRange,
  109. pub,
  110. enumPriority);
  111. _lockEntries.AcquireWriterLock (-1);
  112. try {
  113. _nItems++;
  114. if (_arrEntries.Contains (strKey)) {
  115. if (overwrite)
  116. objOldEntry = _arrEntries [strKey] as CacheEntry;
  117. else
  118. return null;
  119. }
  120. objEntry.Hit ();
  121. _arrEntries [strKey] = objEntry;
  122. // If we have any kind of expiration add into the CacheExpires
  123. // Do this under the lock so no-one can retrieve the objEntry
  124. // before it is fully initialized.
  125. if (objEntry.HasSlidingExpiration || objEntry.HasAbsoluteExpiration) {
  126. if (objEntry.HasSlidingExpiration)
  127. objEntry.Expires = DateTime.UtcNow.Ticks + objEntry.SlidingExpiration;
  128. _objExpires.Add (objEntry);
  129. }
  130. } finally {
  131. _lockEntries.ReleaseLock ();
  132. }
  133. if (objOldEntry != null) {
  134. if (objOldEntry.HasAbsoluteExpiration || objOldEntry.HasSlidingExpiration)
  135. _objExpires.Remove (objOldEntry);
  136. objOldEntry.Close (CacheItemRemovedReason.Removed);
  137. }
  138. return objEntry.Item;
  139. }
  140. public void Insert (string strKey, object objItem) {
  141. Add (strKey, objItem, null, NoAbsoluteExpiration, NoSlidingExpiration,
  142. CacheItemPriority.Default, null, true, true);
  143. }
  144. public void Insert (string strKey, object objItem, CacheDependency objDependency) {
  145. Add (strKey, objItem, objDependency, NoAbsoluteExpiration, NoSlidingExpiration,
  146. CacheItemPriority.Default, null, true, true);
  147. }
  148. public void Insert (string strKey, object objItem, CacheDependency objDependency,
  149. DateTime absolutExpiration, TimeSpan slidingExpiration) {
  150. Add (strKey, objItem, objDependency, absolutExpiration, slidingExpiration,
  151. CacheItemPriority.Default, null, true, true);
  152. }
  153. public void Insert (string strKey, object objItem, CacheDependency objDependency,
  154. DateTime absolutExpiration, TimeSpan slidingExpiration,
  155. CacheItemPriority enumPriority, CacheItemRemovedCallback eventRemoveCallback) {
  156. Add (strKey, objItem, objDependency, absolutExpiration, slidingExpiration,
  157. enumPriority, eventRemoveCallback, true, true);
  158. }
  159. // Called from other internal System.Web methods to add non-public objects into
  160. // cache, like output cache etc
  161. internal void InsertPrivate (string strKey, object objItem, CacheDependency objDependency,
  162. DateTime absolutExpiration, TimeSpan slidingExpiration,
  163. CacheItemPriority enumPriority, CacheItemRemovedCallback eventRemoveCallback)
  164. {
  165. Add (strKey, objItem, objDependency, absolutExpiration, slidingExpiration,
  166. enumPriority, eventRemoveCallback, false, true);
  167. }
  168. internal void InsertPrivate (string strKey, object objItem, CacheDependency objDependency)
  169. {
  170. Add (strKey, objItem, objDependency, NoAbsoluteExpiration, NoSlidingExpiration,
  171. CacheItemPriority.Default, null, false, true);
  172. }
  173. public object Remove (string strKey) {
  174. return Remove (strKey, CacheItemRemovedReason.Removed);
  175. }
  176. internal object Remove (string strKey, CacheItemRemovedReason enumReason) {
  177. CacheEntry objEntry = null;
  178. if (strKey == null)
  179. throw new ArgumentNullException ("strKey");
  180. _lockEntries.AcquireWriterLock (-1);
  181. try {
  182. objEntry = _arrEntries [strKey] as CacheEntry;
  183. if (null == objEntry)
  184. return null;
  185. _arrEntries.Remove (strKey);
  186. _nItems--;
  187. }
  188. finally {
  189. _lockEntries.ReleaseWriterLock ();
  190. }
  191. if (objEntry.HasAbsoluteExpiration || objEntry.HasSlidingExpiration)
  192. _objExpires.Remove (objEntry);
  193. objEntry.Close (enumReason);
  194. return objEntry.Item;
  195. }
  196. public object Get (string strKey) {
  197. CacheEntry objEntry = GetEntry (strKey);
  198. if (objEntry == null)
  199. return null;
  200. return objEntry.Item;
  201. }
  202. internal CacheEntry GetEntry (string strKey) {
  203. CacheEntry objEntry = null;
  204. long ticksNow = DateTime.UtcNow.Ticks;
  205. if (strKey == null)
  206. throw new ArgumentNullException ("strKey");
  207. _lockEntries.AcquireReaderLock (-1);
  208. try {
  209. objEntry = _arrEntries [strKey] as CacheEntry;
  210. if (null == objEntry)
  211. return null;
  212. }
  213. finally {
  214. _lockEntries.ReleaseReaderLock ();
  215. }
  216. if (objEntry.HasSlidingExpiration || objEntry.HasAbsoluteExpiration) {
  217. if (objEntry.Expires < ticksNow) {
  218. Remove (strKey, CacheItemRemovedReason.Expired);
  219. return null;
  220. }
  221. }
  222. objEntry.Hit ();
  223. if (objEntry.HasSlidingExpiration) {
  224. objEntry.Expires = ticksNow + objEntry.SlidingExpiration;
  225. }
  226. return objEntry;
  227. }
  228. public int Count {
  229. get { return _nItems; }
  230. }
  231. public object this [string strKey] {
  232. get {
  233. return Get (strKey);
  234. }
  235. set {
  236. Insert (strKey, value);
  237. }
  238. }
  239. }
  240. }