CacheItemPriorityQueue.cs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. //
  2. // System.Web.Caching.CacheItem
  3. //
  4. // Author(s):
  5. // Marek Habersack <[email protected]>
  6. //
  7. // (C) 2009 Novell, Inc (http://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;
  30. using System.Text;
  31. using System.Threading;
  32. namespace System.Web.Caching
  33. {
  34. sealed class CacheItemPriorityQueue
  35. {
  36. sealed class Node
  37. {
  38. public CacheItem Data;
  39. public Node Left;
  40. public Node Right;
  41. public Node Parent;
  42. public Node Next;
  43. public Node Prev;
  44. public CacheItem SwapData (CacheItem newData)
  45. {
  46. CacheItem ret = Data;
  47. Data = newData;
  48. return ret;
  49. }
  50. public Node (CacheItem data)
  51. {
  52. Data = data;
  53. }
  54. }
  55. Node root;
  56. Node lastAdded;
  57. Node firstParent;
  58. Node lastParent;
  59. #if SYSTEMCORE_DEP
  60. ReaderWriterLockSlim queueLock;
  61. #endif
  62. public CacheItemPriorityQueue ()
  63. {
  64. #if SYSTEMCORE_DEP
  65. queueLock = new ReaderWriterLockSlim ();
  66. #endif
  67. }
  68. public void Enqueue (CacheItem item)
  69. {
  70. if (item == null)
  71. return;
  72. #if SYSTEMCORE_DEP
  73. bool locked = false;
  74. try {
  75. queueLock.EnterWriteLock ();
  76. locked = true;
  77. #endif
  78. Node node = new Node (item);
  79. if (root == null) {
  80. root = lastAdded = lastParent = firstParent = node;
  81. return;
  82. }
  83. if (lastParent.Left != null && lastParent.Right != null) {
  84. lastParent = lastParent.Next;
  85. if (lastParent == null) {
  86. lastParent = firstParent = firstParent.Left;
  87. lastAdded = null;
  88. }
  89. }
  90. node.Parent = lastParent;
  91. if (lastParent.Left == null)
  92. lastParent.Left = node;
  93. else
  94. lastParent.Right = node;
  95. if (lastAdded != null) {
  96. lastAdded.Next = node;
  97. node.Prev = lastAdded;
  98. }
  99. lastAdded = node;
  100. BubbleUp (node);
  101. #if SYSTEMCORE_DEP
  102. } finally {
  103. if (locked)
  104. queueLock.ExitWriteLock ();
  105. }
  106. #endif
  107. }
  108. public CacheItem Dequeue ()
  109. {
  110. CacheItem ret = null;
  111. #if SYSTEMCORE_DEP
  112. bool locked = false;
  113. try {
  114. queueLock.EnterWriteLock ();
  115. locked = true;
  116. #endif
  117. if (root == null)
  118. return null;
  119. if (root.Left == null && root.Right == null) {
  120. ret = root.Data;
  121. root = null;
  122. if (ret.Disabled)
  123. return null;
  124. return ret;
  125. }
  126. ret = root.Data;
  127. do {
  128. Node last = lastAdded;
  129. if (last == null)
  130. return null;
  131. if (last.Prev == null) {
  132. Node parent = last.Parent;
  133. while (true) {
  134. if (parent.Next == null)
  135. break;
  136. parent = parent.Next;
  137. }
  138. lastAdded = parent;
  139. } else {
  140. lastAdded = last.Prev;
  141. lastAdded.Next = null;
  142. }
  143. if (last.Parent.Left == last)
  144. last.Parent.Left = null;
  145. else
  146. last.Parent.Right = null;
  147. root.Data = last.Data;
  148. BubbleDown (root);
  149. } while (ret.Disabled);
  150. #if SYSTEMCORE_DEP
  151. } finally {
  152. if (locked)
  153. queueLock.ExitWriteLock ();
  154. }
  155. #endif
  156. return ret;
  157. }
  158. public CacheItem Peek ()
  159. {
  160. #if SYSTEMCORE_DEP
  161. bool locked = false;
  162. try {
  163. queueLock.EnterReadLock ();
  164. locked = true;
  165. #endif
  166. if (root == null)
  167. return null;
  168. #if SYSTEMCORE_DEP
  169. } finally {
  170. if (locked)
  171. queueLock.ExitReadLock ();
  172. }
  173. #endif
  174. return root.Data;
  175. }
  176. void BubbleDown (Node item)
  177. {
  178. if (item == null || (item.Left == null && item.Right == null))
  179. return;
  180. if (item.Left == null)
  181. SwapBubbleDown (item, item.Right);
  182. else if (item.Right == null)
  183. SwapBubbleDown (item, item.Left);
  184. else {
  185. if (item.Left.Data.ExpiresAt < item.Right.Data.ExpiresAt)
  186. SwapBubbleDown (item, item.Left);
  187. else
  188. SwapBubbleDown (item, item.Right);
  189. }
  190. }
  191. void SwapBubbleDown (Node item, Node otherItem)
  192. {
  193. if (otherItem.Data.ExpiresAt < item.Data.ExpiresAt) {
  194. item.Data = otherItem.SwapData (item.Data);
  195. BubbleDown (otherItem);
  196. }
  197. }
  198. void BubbleUp (Node item)
  199. {
  200. if (item == null || item.Data == null)
  201. return;
  202. Node parent = item.Parent;
  203. if (parent == null)
  204. return;
  205. if (item.Data.ExpiresAt > parent.Data.ExpiresAt)
  206. return;
  207. item.Data = parent.SwapData (item.Data);
  208. BubbleUp (parent);
  209. }
  210. public string GetDotScript ()
  211. {
  212. StringBuilder sb = new StringBuilder ();
  213. sb.Append ("graph CacheItemPriorityQueue {\n");
  214. sb.Append ("\tnode [color=lightblue, style=filled];\n");
  215. if (root != null) {
  216. if (root.Left == null && root.Right == null)
  217. sb.AppendFormat ("\t{0};", root.Data.ExpiresAt);
  218. else
  219. TraverseTree (sb, root);
  220. }
  221. sb.Append ("}\n");
  222. return sb.ToString ();
  223. }
  224. void TraverseTree (StringBuilder sb, Node root)
  225. {
  226. if (root.Left != null) {
  227. sb.AppendFormat ("\t{0} -- {1};\n", root.Data.ExpiresAt, root.Left.Data.ExpiresAt);
  228. TraverseTree (sb, root.Left);
  229. }
  230. if (root.Right != null) {
  231. sb.AppendFormat ("\t{0} -- {1};\n", root.Data.ExpiresAt, root.Right.Data.ExpiresAt);
  232. TraverseTree (sb, root.Right);
  233. }
  234. }
  235. }
  236. }