CacheItemPriorityQueue.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204
  1. //
  2. // Author(s):
  3. // Marek Habersack <[email protected]>
  4. //
  5. // (C) 2009-2010 Novell, Inc (http://novell.com)
  6. //
  7. //
  8. // Permission is hereby granted, free of charge, to any person obtaining
  9. // a copy of this software and associated documentation files (the
  10. // "Software"), to deal in the Software without restriction, including
  11. // without limitation the rights to use, copy, modify, merge, publish,
  12. // distribute, sublicense, and/or sell copies of the Software, and to
  13. // permit persons to whom the Software is furnished to do so, subject to
  14. // the following conditions:
  15. //
  16. // The above copyright notice and this permission notice shall be
  17. // included in all copies or substantial portions of the Software.
  18. //
  19. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  20. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  21. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  22. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  23. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  24. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  25. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  26. //
  27. using System;
  28. using System.Collections.Generic;
  29. using System.IO;
  30. using System.Text;
  31. using System.Threading;
  32. using System.Xml;
  33. namespace System.Web.Caching
  34. {
  35. sealed partial class CacheItemPriorityQueue
  36. {
  37. const int INITIAL_HEAP_SIZE = 32;
  38. const int HEAP_RESIZE_THRESHOLD = 8192;
  39. CacheItem[] heap;
  40. int heapSize = 0;
  41. int heapCount = 0;
  42. // See comment for the cacheLock field at top of System.Web.Caching/Cache.cs
  43. ReaderWriterLockSlim queueLock;
  44. public int Count {
  45. get { return heapCount; }
  46. }
  47. public int Size {
  48. get { return heapSize; }
  49. }
  50. public CacheItemPriorityQueue ()
  51. {
  52. queueLock = new ReaderWriterLockSlim ();
  53. InitDebugMode ();
  54. }
  55. CacheItem[] GetHeapWithGrow ()
  56. {
  57. if (heap == null) {
  58. heap = new CacheItem [INITIAL_HEAP_SIZE];
  59. heapSize = INITIAL_HEAP_SIZE;
  60. heapCount = 0;
  61. return heap;
  62. }
  63. if (heapCount >= heapSize) {
  64. heapSize <<= 1;
  65. Array.Resize <CacheItem> (ref heap, heapSize);
  66. }
  67. return heap;
  68. }
  69. CacheItem[] GetHeapWithShrink ()
  70. {
  71. if (heap == null)
  72. return null;
  73. if (heapSize > HEAP_RESIZE_THRESHOLD) {
  74. int halfTheSize = heapSize >> 1;
  75. if (heapCount < halfTheSize)
  76. Array.Resize <CacheItem> (ref heap, halfTheSize + (heapCount / 3));
  77. }
  78. return heap;
  79. }
  80. public void Enqueue (CacheItem item)
  81. {
  82. if (item == null)
  83. return;
  84. CacheItem[] heap;
  85. try {
  86. queueLock.EnterWriteLock ();
  87. heap = GetHeapWithGrow ();
  88. heap [heapCount++] = item;
  89. BubbleUp (heap);
  90. AddSequenceEntry (item, EDSequenceEntryType.Enqueue);
  91. } finally {
  92. // See comment at the top of the file, above queueLock declaration
  93. queueLock.ExitWriteLock ();
  94. }
  95. }
  96. public CacheItem Dequeue ()
  97. {
  98. CacheItem ret = null;
  99. CacheItem[] heap;
  100. int index;
  101. try {
  102. queueLock.EnterWriteLock ();
  103. heap = GetHeapWithShrink ();
  104. if (heap == null || heapCount == 0)
  105. return null;
  106. ret = heap [0];
  107. index = --heapCount;
  108. heap [0] = heap [index];
  109. heap [index] = null;
  110. if (heapCount > 0)
  111. BubbleDown (heap);
  112. AddSequenceEntry (ret, EDSequenceEntryType.Dequeue);
  113. return ret;
  114. } finally {
  115. // See comment at the top of the file, above queueLock declaration
  116. queueLock.ExitWriteLock ();
  117. }
  118. }
  119. public CacheItem Peek ()
  120. {
  121. CacheItem ret;
  122. try {
  123. queueLock.EnterReadLock ();
  124. if (heap == null || heapCount == 0)
  125. return null;
  126. ret = heap [0];
  127. AddSequenceEntry (ret, EDSequenceEntryType.Peek);
  128. return ret;
  129. } finally {
  130. // See comment at the top of the file, above queueLock declaration
  131. queueLock.ExitReadLock ();
  132. }
  133. }
  134. void BubbleDown (CacheItem[] heap)
  135. {
  136. int index = 0;
  137. int left = 1;
  138. int right = 2;
  139. CacheItem item = heap [0];
  140. int selected = (right < heapCount && heap [right].ExpiresAt < heap [left].ExpiresAt) ? 2 : 1;
  141. while (selected < heapCount && heap [selected].ExpiresAt < item.ExpiresAt) {
  142. heap [index] = heap [selected];
  143. index = selected;
  144. left = (index << 1) + 1;
  145. right = left + 1;
  146. selected = right < heapCount && heap [right].ExpiresAt < heap [left].ExpiresAt ? right : left;
  147. }
  148. heap [index] = item;
  149. }
  150. void BubbleUp (CacheItem[] heap)
  151. {
  152. int index, parentIndex;
  153. CacheItem parent, item;
  154. if (heapCount <= 1)
  155. return;
  156. index = heapCount - 1;
  157. parentIndex = (index - 1) >> 1;
  158. item = heap [index];
  159. while (index > 0) {
  160. parent = heap [parentIndex];
  161. if (heap [index].ExpiresAt >= parent.ExpiresAt)
  162. break;
  163. heap [index] = parent;
  164. index = parentIndex;
  165. parentIndex = (index - 1) >> 1;
  166. }
  167. heap [index] = item;
  168. }
  169. }
  170. }