CacheItemPriorityQueue.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  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. ReaderWriterLockSlim queueLock;
  43. public int Count {
  44. get { return heapCount; }
  45. }
  46. public int Size {
  47. get { return heapSize; }
  48. }
  49. public CacheItemPriorityQueue ()
  50. {
  51. queueLock = new ReaderWriterLockSlim ();
  52. InitDebugMode ();
  53. }
  54. CacheItem[] GetHeapWithGrow ()
  55. {
  56. if (heap == null) {
  57. heap = new CacheItem [INITIAL_HEAP_SIZE];
  58. heapSize = INITIAL_HEAP_SIZE;
  59. heapCount = 0;
  60. return heap;
  61. }
  62. if (heapCount >= heapSize) {
  63. heapSize <<= 1;
  64. Array.Resize <CacheItem> (ref heap, heapSize);
  65. }
  66. return heap;
  67. }
  68. CacheItem[] GetHeapWithShrink ()
  69. {
  70. if (heap == null)
  71. return null;
  72. if (heapSize > HEAP_RESIZE_THRESHOLD) {
  73. int halfTheSize = heapSize >> 1;
  74. if (heapCount < halfTheSize)
  75. Array.Resize <CacheItem> (ref heap, halfTheSize + (heapCount / 3));
  76. }
  77. return heap;
  78. }
  79. public void Enqueue (CacheItem item)
  80. {
  81. if (item == null)
  82. return;
  83. bool locked = false;
  84. CacheItem[] heap;
  85. try {
  86. queueLock.EnterWriteLock ();
  87. locked = true;
  88. heap = GetHeapWithGrow ();
  89. heap [heapCount++] = item;
  90. BubbleUp (heap);
  91. AddSequenceEntry (item, EDSequenceEntryType.Enqueue);
  92. } finally {
  93. if (locked)
  94. queueLock.ExitWriteLock ();
  95. }
  96. }
  97. public CacheItem Dequeue ()
  98. {
  99. CacheItem ret = null;
  100. CacheItem[] heap;
  101. bool locked = false;
  102. int index;
  103. try {
  104. queueLock.EnterWriteLock ();
  105. locked = true;
  106. heap = GetHeapWithShrink ();
  107. if (heap == null || heapCount == 0)
  108. return null;
  109. ret = heap [0];
  110. index = --heapCount;
  111. heap [0] = heap [index];
  112. heap [index] = null;
  113. if (heapCount > 0)
  114. BubbleDown (heap);
  115. AddSequenceEntry (ret, EDSequenceEntryType.Dequeue);
  116. return ret;
  117. } finally {
  118. if (locked)
  119. queueLock.ExitWriteLock ();
  120. }
  121. }
  122. public CacheItem Peek ()
  123. {
  124. bool locked = false;
  125. CacheItem ret;
  126. try {
  127. queueLock.EnterReadLock ();
  128. locked = true;
  129. if (heap == null || heapCount == 0)
  130. return null;
  131. ret = heap [0];
  132. AddSequenceEntry (ret, EDSequenceEntryType.Peek);
  133. return ret;
  134. } finally {
  135. if (locked)
  136. queueLock.ExitReadLock ();
  137. }
  138. }
  139. void BubbleDown (CacheItem[] heap)
  140. {
  141. int index = 0;
  142. int left = 1;
  143. int right = 2;
  144. CacheItem item = heap [0];
  145. int selected = (right < heapCount && heap [right].ExpiresAt < heap [left].ExpiresAt) ? 2 : 1;
  146. while (selected < heapCount && heap [selected].ExpiresAt < item.ExpiresAt) {
  147. heap [index] = heap [selected];
  148. index = selected;
  149. left = (index << 1) + 1;
  150. right = left + 1;
  151. selected = right < heapCount && heap [right].ExpiresAt < heap [left].ExpiresAt ? right : left;
  152. }
  153. heap [index] = item;
  154. }
  155. void BubbleUp (CacheItem[] heap)
  156. {
  157. int index, parentIndex;
  158. CacheItem parent, item;
  159. if (heapCount <= 1)
  160. return;
  161. index = heapCount - 1;
  162. parentIndex = (index - 1) >> 1;
  163. item = heap [index];
  164. while (index > 0) {
  165. parent = heap [parentIndex];
  166. if (heap [index].ExpiresAt >= parent.ExpiresAt)
  167. break;
  168. heap [index] = parent;
  169. index = parentIndex;
  170. parentIndex = (index - 1) >> 1;
  171. }
  172. heap [index] = item;
  173. }
  174. }
  175. }