Cache.cs 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677
  1. //
  2. // System.Web.Caching.Cache
  3. //
  4. // Author(s):
  5. // Lluis Sanchez ([email protected])
  6. // Marek Habersack <[email protected]>
  7. //
  8. // (C) 2005-2009 Novell, Inc (http://novell.com)
  9. //
  10. //
  11. // Permission is hereby granted, free of charge, to any person obtaining
  12. // a copy of this software and associated documentation files (the
  13. // "Software"), to deal in the Software without restriction, including
  14. // without limitation the rights to use, copy, modify, merge, publish,
  15. // distribute, sublicense, and/or sell copies of the Software, and to
  16. // permit persons to whom the Software is furnished to do so, subject to
  17. // the following conditions:
  18. //
  19. // The above copyright notice and this permission notice shall be
  20. // included in all copies or substantial portions of the Software.
  21. //
  22. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  23. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  24. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  25. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  26. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  27. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  28. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  29. //
  30. using System.Threading;
  31. using System.Collections;
  32. using System.Collections.Generic;
  33. using System.Linq;
  34. using System.Security.Permissions;
  35. using System.Web.Configuration;
  36. namespace System.Web.Caching
  37. {
  38. // CAS - no InheritanceDemand here as the class is sealed
  39. [AspNetHostingPermission (SecurityAction.LinkDemand, Level = AspNetHostingPermissionLevel.Minimal)]
  40. public sealed class Cache: IEnumerable
  41. {
  42. const int LOW_WATER_MARK = 10000; // Target number of items if high water mark is reached
  43. const int HIGH_WATER_MARK = 15000; // We start collection after exceeding this count
  44. public static readonly DateTime NoAbsoluteExpiration = DateTime.MaxValue;
  45. public static readonly TimeSpan NoSlidingExpiration = TimeSpan.Zero;
  46. // cacheLock will be released in the code below without checking whether it was
  47. // actually acquired. The API doesn't offer a reliable way to check whether the lock
  48. // is being held by the current thread and since Mono does't implement CER
  49. // (Constrained Execution Regions -
  50. // http://msdn.microsoft.com/en-us/library/ms228973.aspx) currently, we have no
  51. // reliable way of recording the information that the lock has been successfully
  52. // acquired.
  53. // It can happen that a Thread.Abort occurs while acquiring the lock and the lock
  54. // isn't actually held. In this case the attempt to release a lock will throw an
  55. // exception. It's better than a race of setting a boolean flag after acquiring the
  56. // lock and then relying upon it here to release it - that may cause a deadlock
  57. // should we fail to release the lock which was successfully acquired but
  58. // Thread.Abort happened right after that during the stloc instruction to set the
  59. // boolean flag. Once CERs are supported we can use the boolean flag reliably.
  60. ReaderWriterLockSlim cacheLock;
  61. Dictionary <string, CacheItem> cache;
  62. CacheItemPriorityQueue timedItems;
  63. Timer expirationTimer;
  64. long expirationTimerPeriod = 0;
  65. Cache dependencyCache;
  66. bool? disableExpiration;
  67. long privateBytesLimit = -1;
  68. long percentagePhysicalMemoryLimit = -1;
  69. bool DisableExpiration {
  70. get {
  71. if (disableExpiration == null) {
  72. var cs = WebConfigurationManager.GetWebApplicationSection ("system.web/caching/cache") as CacheSection;
  73. if (cs == null)
  74. disableExpiration = false;
  75. else
  76. disableExpiration = (bool)cs.DisableExpiration;
  77. }
  78. return (bool)disableExpiration;
  79. }
  80. }
  81. public long EffectivePrivateBytesLimit {
  82. get {
  83. if (privateBytesLimit == -1) {
  84. var cs = WebConfigurationManager.GetWebApplicationSection ("system.web/caching/cache") as CacheSection;
  85. if (cs == null)
  86. privateBytesLimit = 0;
  87. else
  88. privateBytesLimit = cs.PrivateBytesLimit;
  89. if (privateBytesLimit == 0) {
  90. // http://blogs.msdn.com/tmarq/archive/2007/06/25/some-history-on-the-asp-net-cache-memory-limits.aspx
  91. // TODO: calculate
  92. privateBytesLimit = 734003200;
  93. }
  94. }
  95. return privateBytesLimit;
  96. }
  97. }
  98. public long EffectivePercentagePhysicalMemoryLimit {
  99. get {
  100. if (percentagePhysicalMemoryLimit == -1) {
  101. var cs = WebConfigurationManager.GetWebApplicationSection ("system.web/caching/cache") as CacheSection;
  102. if (cs == null)
  103. percentagePhysicalMemoryLimit = 0;
  104. else
  105. percentagePhysicalMemoryLimit = cs.PercentagePhysicalMemoryUsedLimit;
  106. if (percentagePhysicalMemoryLimit == 0) {
  107. // http://blogs.msdn.com/tmarq/archive/2007/06/25/some-history-on-the-asp-net-cache-memory-limits.aspx
  108. // TODO: calculate
  109. percentagePhysicalMemoryLimit = 97;
  110. }
  111. }
  112. return percentagePhysicalMemoryLimit;
  113. }
  114. }
  115. public Cache ()
  116. {
  117. cacheLock = new ReaderWriterLockSlim ();
  118. cache = new Dictionary <string, CacheItem> (StringComparer.Ordinal);
  119. }
  120. public int Count {
  121. get { return cache.Count; }
  122. }
  123. public object this [string key] {
  124. get { return Get (key); }
  125. set { Insert (key, value); }
  126. }
  127. CacheItem GetCacheItem (string key)
  128. {
  129. if (key == null)
  130. return null;
  131. CacheItem ret;
  132. if (cache.TryGetValue (key, out ret))
  133. return ret;
  134. return null;
  135. }
  136. // Must ALWAYS be called with the cache lock held
  137. CacheItem RemoveCacheItem (string key)
  138. {
  139. if (key == null)
  140. return null;
  141. CacheItem ret = null;
  142. if (!cache.TryGetValue (key, out ret))
  143. return null;
  144. if (timedItems != null)
  145. timedItems.OnItemDisable (ret);
  146. ret.Disabled = true;
  147. cache.Remove (key);
  148. return ret;
  149. }
  150. void RemoveOldItemsIfNecessary ()
  151. {
  152. if (cache.Count < HIGH_WATER_MARK)
  153. return;
  154. ThreadPool.QueueUserWorkItem (delegate {
  155. DoRemoveOldItemsIfNecessary ();
  156. });
  157. }
  158. void DoRemoveOldItemsIfNecessary ()
  159. {
  160. ICollection <CacheItem> values = null;
  161. try {
  162. cacheLock.EnterWriteLock ();
  163. int count = cache.Count;
  164. values = cache.Values;
  165. if (RemoveOldItems (values.Where (item => item.Priority == CacheItemPriority.Low).OrderBy (item => item.LastChange).ToArray <CacheItem> (), ref count))
  166. return;
  167. if (RemoveOldItems (values.Where (item => item.Priority == CacheItemPriority.Normal).OrderBy (item => item.LastChange).ToArray <CacheItem> (), ref count))
  168. return;
  169. if (RemoveOldItems (values.Where (item => item.Priority == CacheItemPriority.AboveNormal).OrderBy (item => item.LastChange).ToArray <CacheItem> (), ref count))
  170. return;
  171. if (RemoveOldItems (values.Where (item => item.Priority == CacheItemPriority.High).OrderBy (item => item.LastChange).ToArray <CacheItem> (), ref count))
  172. return;
  173. } finally {
  174. // See comment at the top of the file, above cacheLock declaration
  175. cacheLock.ExitWriteLock ();
  176. values = null;
  177. GC.Collect ();
  178. }
  179. }
  180. bool RemoveOldItems (CacheItem[] byPriority, ref int count)
  181. {
  182. if (byPriority == null || byPriority.Length == 0)
  183. return false;
  184. foreach (CacheItem item in byPriority) {
  185. if (item.Disabled)
  186. continue;
  187. if (count < LOW_WATER_MARK)
  188. break;
  189. Remove (item.Key, CacheItemRemovedReason.Underused, false, true);
  190. count--;
  191. }
  192. Array.Clear (byPriority, 0, byPriority.Length);
  193. byPriority = null;
  194. return count < LOW_WATER_MARK;
  195. }
  196. public object Add (string key, object value, CacheDependency dependencies, DateTime absoluteExpiration, TimeSpan slidingExpiration, CacheItemPriority priority, CacheItemRemovedCallback onRemoveCallback)
  197. {
  198. if (key == null)
  199. throw new ArgumentNullException ("key");
  200. try {
  201. cacheLock.EnterWriteLock ();
  202. CacheItem it = GetCacheItem (key);
  203. if (it != null)
  204. return it.Value;
  205. Insert (key, value, dependencies, absoluteExpiration, slidingExpiration, priority, onRemoveCallback, null, false);
  206. } finally {
  207. // See comment at the top of the file, above cacheLock declaration
  208. cacheLock.ExitWriteLock ();
  209. }
  210. return null;
  211. }
  212. public object Get (string key)
  213. {
  214. try {
  215. cacheLock.EnterUpgradeableReadLock ();
  216. CacheItem it = GetCacheItem (key);
  217. if (it == null)
  218. return null;
  219. if (it.Dependency != null && it.Dependency.HasChanged) {
  220. try {
  221. cacheLock.EnterWriteLock ();
  222. if (!NeedsUpdate (it, CacheItemUpdateReason.DependencyChanged, false))
  223. Remove (it.Key, CacheItemRemovedReason.DependencyChanged, false, true);
  224. } finally {
  225. // See comment at the top of the file, above cacheLock declaration
  226. cacheLock.ExitWriteLock ();
  227. }
  228. return null;
  229. }
  230. if (!DisableExpiration) {
  231. if (it.SlidingExpiration != NoSlidingExpiration) {
  232. it.AbsoluteExpiration = DateTime.Now + it.SlidingExpiration;
  233. // Cast to long is ok since we know that sliding expiration
  234. // is less than 365 days (31536000000ms)
  235. long remaining = (long)it.SlidingExpiration.TotalMilliseconds;
  236. it.ExpiresAt = it.AbsoluteExpiration.Ticks;
  237. if (expirationTimer != null && (expirationTimerPeriod == 0 || expirationTimerPeriod > remaining)) {
  238. expirationTimerPeriod = remaining;
  239. expirationTimer.Change (expirationTimerPeriod, expirationTimerPeriod);
  240. }
  241. } else if (DateTime.Now >= it.AbsoluteExpiration) {
  242. try {
  243. cacheLock.EnterWriteLock ();
  244. if (!NeedsUpdate (it, CacheItemUpdateReason.Expired, false))
  245. Remove (key, CacheItemRemovedReason.Expired, false, true);
  246. } finally {
  247. // See comment at the top of the file, above cacheLock declaration
  248. cacheLock.ExitWriteLock ();
  249. }
  250. return null;
  251. }
  252. }
  253. return it.Value;
  254. } finally {
  255. // See comment at the top of the file, above cacheLock declaration
  256. cacheLock.ExitUpgradeableReadLock ();
  257. }
  258. }
  259. public void Insert (string key, object value)
  260. {
  261. Insert (key, value, null, NoAbsoluteExpiration, NoSlidingExpiration, CacheItemPriority.Normal, null, null, true);
  262. }
  263. public void Insert (string key, object value, CacheDependency dependencies)
  264. {
  265. Insert (key, value, dependencies, NoAbsoluteExpiration, NoSlidingExpiration, CacheItemPriority.Normal, null, null, true);
  266. }
  267. public void Insert (string key, object value, CacheDependency dependencies, DateTime absoluteExpiration, TimeSpan slidingExpiration)
  268. {
  269. Insert (key, value, dependencies, absoluteExpiration, slidingExpiration, CacheItemPriority.Normal, null, null, true);
  270. }
  271. public void Insert (string key, object value, CacheDependency dependencies, DateTime absoluteExpiration, TimeSpan slidingExpiration,
  272. CacheItemUpdateCallback onUpdateCallback)
  273. {
  274. Insert (key, value, dependencies, absoluteExpiration, slidingExpiration, CacheItemPriority.Normal, null, onUpdateCallback, true);
  275. }
  276. public void Insert (string key, object value, CacheDependency dependencies, DateTime absoluteExpiration, TimeSpan slidingExpiration,
  277. CacheItemPriority priority, CacheItemRemovedCallback onRemoveCallback)
  278. {
  279. Insert (key, value, dependencies, absoluteExpiration, slidingExpiration, priority, onRemoveCallback, null, true);
  280. }
  281. void Insert (string key, object value, CacheDependency dependencies, DateTime absoluteExpiration, TimeSpan slidingExpiration,
  282. CacheItemPriority priority, CacheItemRemovedCallback onRemoveCallback, CacheItemUpdateCallback onUpdateCallback, bool doLock)
  283. {
  284. if (key == null)
  285. throw new ArgumentNullException ("key");
  286. if (value == null)
  287. throw new ArgumentNullException ("value");
  288. if (slidingExpiration < TimeSpan.Zero || slidingExpiration > TimeSpan.FromDays (365))
  289. throw new ArgumentNullException ("slidingExpiration");
  290. if (absoluteExpiration != NoAbsoluteExpiration && slidingExpiration != NoSlidingExpiration)
  291. throw new ArgumentException ("Both absoluteExpiration and slidingExpiration are specified");
  292. CacheItem ci = new CacheItem ();
  293. ci.Value = value;
  294. ci.Key = key;
  295. if (dependencies != null) {
  296. ci.Dependency = dependencies;
  297. dependencies.DependencyChanged += new EventHandler (OnDependencyChanged);
  298. dependencies.SetCache (DependencyCache);
  299. }
  300. ci.Priority = priority;
  301. SetItemTimeout (ci, absoluteExpiration, slidingExpiration, onRemoveCallback, onUpdateCallback, key, doLock);
  302. }
  303. internal void SetItemTimeout (string key, DateTime absoluteExpiration, TimeSpan slidingExpiration, bool doLock)
  304. {
  305. CacheItem ci = null;
  306. try {
  307. if (doLock)
  308. cacheLock.EnterWriteLock ();
  309. ci = GetCacheItem (key);
  310. if (ci != null)
  311. SetItemTimeout (ci, absoluteExpiration, slidingExpiration, ci.OnRemoveCallback, null, key, false);
  312. else
  313. RemoveOldItemsIfNecessary ();
  314. } finally {
  315. if (doLock) {
  316. // See comment at the top of the file, above cacheLock declaration
  317. cacheLock.ExitWriteLock ();
  318. }
  319. }
  320. }
  321. void SetItemTimeout (CacheItem ci, DateTime absoluteExpiration, TimeSpan slidingExpiration, CacheItemRemovedCallback onRemoveCallback,
  322. CacheItemUpdateCallback onUpdateCallback, string key, bool doLock)
  323. {
  324. bool disableExpiration = DisableExpiration;
  325. if (!disableExpiration) {
  326. ci.SlidingExpiration = slidingExpiration;
  327. if (slidingExpiration != NoSlidingExpiration)
  328. ci.AbsoluteExpiration = DateTime.Now + slidingExpiration;
  329. else
  330. ci.AbsoluteExpiration = absoluteExpiration;
  331. }
  332. ci.OnRemoveCallback = onRemoveCallback;
  333. ci.OnUpdateCallback = onUpdateCallback;
  334. try {
  335. if (doLock)
  336. cacheLock.EnterWriteLock ();
  337. if (key != null)
  338. cache [key] = ci;
  339. ci.LastChange = DateTime.Now;
  340. if (!disableExpiration && ci.AbsoluteExpiration != NoAbsoluteExpiration) {
  341. ci.IsTimedItem = true;
  342. EnqueueTimedItem (ci);
  343. }
  344. } finally {
  345. if (doLock) {
  346. // See comment at the top of the file, above cacheLock declaration
  347. cacheLock.ExitWriteLock ();
  348. }
  349. }
  350. RemoveOldItemsIfNecessary ();
  351. }
  352. // MUST be called with cache lock held
  353. void EnqueueTimedItem (CacheItem item)
  354. {
  355. long remaining = Math.Max (0, (long)(item.AbsoluteExpiration - DateTime.Now).TotalMilliseconds);
  356. item.ExpiresAt = item.AbsoluteExpiration.Ticks;
  357. if (timedItems == null)
  358. timedItems = new CacheItemPriorityQueue ();
  359. if (remaining > 4294967294)
  360. // Maximum due time for timer
  361. // Item will expire properly anyway, as the timer will be
  362. // rescheduled for the item's expiration time once that item is
  363. // bubbled to the top of the priority queue.
  364. expirationTimerPeriod = 4294967294;
  365. else
  366. expirationTimerPeriod = remaining;
  367. if (expirationTimer == null)
  368. expirationTimer = new Timer (new TimerCallback (ExpireItems), null, expirationTimerPeriod, expirationTimerPeriod);
  369. else
  370. expirationTimer.Change (expirationTimerPeriod, expirationTimerPeriod);
  371. timedItems.Enqueue (item);
  372. }
  373. public object Remove (string key)
  374. {
  375. return Remove (key, CacheItemRemovedReason.Removed, true, true);
  376. }
  377. object Remove (string key, CacheItemRemovedReason reason, bool doLock, bool invokeCallback)
  378. {
  379. CacheItem it = null;
  380. try {
  381. if (doLock)
  382. cacheLock.EnterWriteLock ();
  383. it = RemoveCacheItem (key);
  384. } finally {
  385. if (doLock) {
  386. // See comment at the top of the file, above cacheLock declaration
  387. cacheLock.ExitWriteLock ();
  388. }
  389. }
  390. object ret = null;
  391. if (it != null) {
  392. if (it.Dependency != null) {
  393. it.Dependency.SetCache (null);
  394. it.Dependency.DependencyChanged -= new EventHandler (OnDependencyChanged);
  395. it.Dependency.Dispose ();
  396. }
  397. if (invokeCallback && it.OnRemoveCallback != null) {
  398. try {
  399. it.OnRemoveCallback (key, it.Value, reason);
  400. } catch {
  401. //TODO: anything to be done here?
  402. }
  403. }
  404. ret = it.Value;
  405. it.Value = null;
  406. it.Key = null;
  407. it.Dependency = null;
  408. it.OnRemoveCallback = null;
  409. it.OnUpdateCallback = null;
  410. it = null;
  411. }
  412. RemoveOldItemsIfNecessary ();
  413. return ret;
  414. }
  415. // Used when shutting down the application so that
  416. // session_end events are sent for all sessions.
  417. internal void InvokePrivateCallbacks ()
  418. {
  419. CacheItemRemovedReason reason = CacheItemRemovedReason.Removed;
  420. try {
  421. cacheLock.EnterReadLock ();
  422. foreach (string key in cache.Keys) {
  423. CacheItem item = GetCacheItem (key);
  424. if (item.Disabled)
  425. continue;
  426. if (item != null && item.OnRemoveCallback != null) {
  427. try {
  428. item.OnRemoveCallback (key, item.Value, reason);
  429. } catch {
  430. //TODO: anything to be done here?
  431. }
  432. }
  433. }
  434. } finally {
  435. // See comment at the top of the file, above cacheLock declaration
  436. cacheLock.ExitReadLock ();
  437. }
  438. }
  439. public IDictionaryEnumerator GetEnumerator ()
  440. {
  441. ArrayList list = new ArrayList ();
  442. try {
  443. cacheLock.EnterReadLock ();
  444. foreach (CacheItem it in cache.Values)
  445. list.Add (it);
  446. } finally {
  447. // See comment at the top of the file, above cacheLock declaration
  448. cacheLock.ExitReadLock ();
  449. }
  450. return new CacheItemEnumerator (list);
  451. }
  452. IEnumerator IEnumerable.GetEnumerator ()
  453. {
  454. return GetEnumerator ();
  455. }
  456. void OnDependencyChanged (object o, EventArgs a)
  457. {
  458. CheckDependencies ();
  459. }
  460. bool NeedsUpdate (CacheItem item, CacheItemUpdateReason reason, bool needLock)
  461. {
  462. try {
  463. if (needLock)
  464. cacheLock.EnterWriteLock ();
  465. if (item == null || item.OnUpdateCallback == null)
  466. return false;
  467. object expensiveObject;
  468. CacheDependency dependency;
  469. DateTime absoluteExpiration;
  470. TimeSpan slidingExpiration;
  471. string key = item.Key;
  472. CacheItemUpdateCallback updateCB = item.OnUpdateCallback;
  473. updateCB (key, reason, out expensiveObject, out dependency, out absoluteExpiration, out slidingExpiration);
  474. if (expensiveObject == null)
  475. return false;
  476. CacheItemPriority priority = item.Priority;
  477. CacheItemRemovedCallback removeCB = item.OnRemoveCallback;
  478. CacheItemRemovedReason whyRemoved;
  479. switch (reason) {
  480. case CacheItemUpdateReason.Expired:
  481. whyRemoved = CacheItemRemovedReason.Expired;
  482. break;
  483. case CacheItemUpdateReason.DependencyChanged:
  484. whyRemoved = CacheItemRemovedReason.DependencyChanged;
  485. break;
  486. default:
  487. whyRemoved = CacheItemRemovedReason.Removed;
  488. break;
  489. }
  490. Remove (key, whyRemoved, false, false);
  491. Insert (key, expensiveObject, dependency, absoluteExpiration, slidingExpiration, priority, removeCB, updateCB, false);
  492. return true;
  493. } catch (Exception) {
  494. return false;
  495. } finally {
  496. if (needLock) {
  497. // See comment at the top of the file, above cacheLock declaration
  498. cacheLock.ExitWriteLock ();
  499. }
  500. }
  501. }
  502. void ExpireItems (object data)
  503. {
  504. DateTime now = DateTime.Now;
  505. CacheItem item = timedItems.Peek ();
  506. try {
  507. cacheLock.EnterWriteLock ();
  508. while (item != null) {
  509. if (!item.Disabled && item.ExpiresAt > now.Ticks)
  510. break;
  511. if (item.Disabled) {
  512. item = timedItems.Dequeue ();
  513. continue;
  514. }
  515. item = timedItems.Dequeue ();
  516. if (item != null) {
  517. if (!NeedsUpdate (item, CacheItemUpdateReason.Expired, false))
  518. Remove (item.Key, CacheItemRemovedReason.Expired, false, true);
  519. }
  520. item = timedItems.Peek ();
  521. }
  522. } finally {
  523. // See comment at the top of the file, above cacheLock declaration
  524. cacheLock.ExitWriteLock ();
  525. }
  526. if (item != null) {
  527. long remaining = Math.Max (0, (long)(item.AbsoluteExpiration - now).TotalMilliseconds);
  528. if (expirationTimerPeriod != remaining && remaining > 0) {
  529. expirationTimerPeriod = remaining;
  530. expirationTimer.Change (expirationTimerPeriod, expirationTimerPeriod);
  531. }
  532. return;
  533. }
  534. expirationTimer.Change (Timeout.Infinite, Timeout.Infinite);
  535. expirationTimerPeriod = 0;
  536. }
  537. internal void CheckDependencies ()
  538. {
  539. IList list;
  540. try {
  541. cacheLock.EnterWriteLock ();
  542. list = new List <CacheItem> ();
  543. foreach (CacheItem it in cache.Values)
  544. list.Add (it);
  545. foreach (CacheItem it in list) {
  546. if (it.Dependency != null && it.Dependency.HasChanged && !NeedsUpdate (it, CacheItemUpdateReason.DependencyChanged, false))
  547. Remove (it.Key, CacheItemRemovedReason.DependencyChanged, false, true);
  548. }
  549. } finally {
  550. // See comment at the top of the file, above cacheLock declaration
  551. cacheLock.ExitWriteLock ();
  552. }
  553. }
  554. internal DateTime GetKeyLastChange (string key)
  555. {
  556. try {
  557. cacheLock.EnterReadLock ();
  558. CacheItem it = GetCacheItem (key);
  559. if (it == null)
  560. return DateTime.MaxValue;
  561. return it.LastChange;
  562. } finally {
  563. // See comment at the top of the file, above cacheLock declaration
  564. cacheLock.ExitReadLock ();
  565. }
  566. }
  567. internal Cache DependencyCache {
  568. get {
  569. if (dependencyCache == null)
  570. return this;
  571. return dependencyCache;
  572. }
  573. set { dependencyCache = value; }
  574. }
  575. }
  576. }