CacheItemLRU.cs 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. //
  2. // A simple LRU cache used for tracking the cache items
  3. //
  4. // Authors:
  5. // Miguel de Icaza ([email protected])
  6. //
  7. // Copyright 2010 Miguel de Icaza
  8. //
  9. // Permission is hereby granted, free of charge, to any person obtaining a copy
  10. // of this software and associated documentation files (the "Software"), to deal
  11. // in the Software without restriction, including without limitation the rights
  12. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  13. // copies of the Software, and to permit persons to whom the Software is
  14. // furnished to do so, subject to the following conditions:
  15. //
  16. // The above copyright notice and this permission notice shall be included in
  17. // all copies or substantial portions of the Software.
  18. //
  19. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  22. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  23. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  24. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  25. // THE SOFTWARE.
  26. //
  27. using System;
  28. using System.Collections.Generic;
  29. namespace System.Web.Caching
  30. {
  31. sealed class CacheItemLRU
  32. {
  33. public delegate bool SelectItemsQualifier (CacheItem item);
  34. Dictionary<string, LinkedListNode <CacheItem>> dict;
  35. Dictionary<LinkedListNode<CacheItem>, string> revdict;
  36. LinkedList<CacheItem> list;
  37. Cache owner;
  38. // High/Low water mark is here to avoid situations when we hit a limit, evict an
  39. // entry, add another one and have to evict again because the limit was hit. When we
  40. // hit the high water limit, we evict until we reach the low water mark to avoid the
  41. // situation.
  42. int highWaterMark, lowWaterMark;
  43. bool needsEviction;
  44. public int Count {
  45. get { return dict.Count; }
  46. }
  47. public CacheItemLRU (Cache owner, int highWaterMark, int lowWaterMark)
  48. {
  49. list = new LinkedList<CacheItem> ();
  50. dict = new Dictionary<string, LinkedListNode<CacheItem>> (StringComparer.Ordinal);
  51. revdict = new Dictionary<LinkedListNode<CacheItem>, string> ();
  52. this.highWaterMark = highWaterMark;
  53. this.lowWaterMark = lowWaterMark;
  54. this.owner = owner;
  55. }
  56. public bool TryGetValue (string key, out CacheItem value)
  57. {
  58. LinkedListNode <CacheItem> item;
  59. if (dict.TryGetValue (key, out item)) {
  60. value = item.Value;
  61. return true;
  62. }
  63. value = null;
  64. return false;
  65. }
  66. // Must ALWAYS be called with the owner's write lock held
  67. public void EvictIfNecessary ()
  68. {
  69. if (!needsEviction)
  70. return;
  71. for (int i = dict.Count; i > lowWaterMark; i--) {
  72. var key = revdict [list.Last];
  73. owner.Remove (key, CacheItemRemovedReason.Underused, false, true);
  74. }
  75. }
  76. // Must ALWAYS be called with the owner's read lock held
  77. public void InvokePrivateCallbacks ()
  78. {
  79. foreach (var de in dict) {
  80. CacheItem item = de.Value.Value;
  81. if (item == null || item.Disabled)
  82. continue;
  83. if (item.OnRemoveCallback != null) {
  84. try {
  85. item.OnRemoveCallback (de.Key, item.Value, CacheItemRemovedReason.Removed);
  86. } catch {
  87. //TODO: anything to be done here?
  88. }
  89. }
  90. }
  91. }
  92. // Must ALWAYS be called with the owner's write lock held
  93. public List <CacheItem> SelectItems (SelectItemsQualifier qualifier)
  94. {
  95. var ret = new List <CacheItem> ();
  96. foreach (LinkedListNode <CacheItem> node in dict.Values) {
  97. CacheItem item = node.Value;
  98. if (qualifier (item))
  99. ret.Add (item);
  100. }
  101. return ret;
  102. }
  103. // Must ALWAYS be called with the owner's read lock held
  104. public List <CacheItem> ToList ()
  105. {
  106. var ret = new List <CacheItem> ();
  107. if (dict.Count == 0)
  108. return ret;
  109. foreach (LinkedListNode <CacheItem> node in dict.Values)
  110. ret.Add (node.Value);
  111. return ret;
  112. }
  113. public void Remove (string key)
  114. {
  115. if (key == null)
  116. return;
  117. LinkedListNode <CacheItem> node;
  118. if (!dict.TryGetValue (key, out node))
  119. return;
  120. CacheItem item = node.Value;
  121. dict.Remove (key);
  122. if (item == null || item.Priority != CacheItemPriority.NotRemovable) {
  123. revdict.Remove (node);
  124. list.Remove (node);
  125. }
  126. }
  127. public CacheItem this [string key] {
  128. get {
  129. if (key == null)
  130. return null;
  131. LinkedListNode<CacheItem> node;
  132. CacheItem item;
  133. if (dict.TryGetValue (key, out node)){
  134. item = node.Value;
  135. if (item == null || item.Priority != CacheItemPriority.NotRemovable) {
  136. list.Remove (node);
  137. list.AddFirst (node);
  138. }
  139. return item;
  140. }
  141. return null;
  142. }
  143. set {
  144. LinkedListNode<CacheItem> node;
  145. if (dict.TryGetValue (key, out node)){
  146. // If we already have a key, move it to the front
  147. list.Remove (node);
  148. if (value == null || value.Priority != CacheItemPriority.NotRemovable)
  149. list.AddFirst (node);
  150. else
  151. revdict.Remove (node);
  152. node.Value = value;
  153. return;
  154. }
  155. needsEviction = dict.Count >= highWaterMark;
  156. // Adding new node
  157. node = new LinkedListNode<CacheItem> (value);
  158. if (value == null || value.Priority != CacheItemPriority.NotRemovable) {
  159. list.AddFirst (node);
  160. revdict [node] = key;
  161. }
  162. dict [key] = node;
  163. }
  164. }
  165. }
  166. }