CacheExpires.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. //
  2. // System.Web.Caching
  3. //
  4. // Author:
  5. // Patrik Torstensson
  6. // Daniel Cazzulino ([email protected])
  7. //
  8. //
  9. // Permission is hereby granted, free of charge, to any person obtaining
  10. // a copy of this software and associated documentation files (the
  11. // "Software"), to deal in the Software without restriction, including
  12. // without limitation the rights to use, copy, modify, merge, publish,
  13. // distribute, sublicense, and/or sell copies of the Software, and to
  14. // permit persons to whom the Software is furnished to do so, subject to
  15. // the following conditions:
  16. //
  17. // The above copyright notice and this permission notice shall be
  18. // included in all copies or substantial portions of the Software.
  19. //
  20. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  21. // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  22. // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  23. // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  24. // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  25. // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  26. // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  27. //
  28. using System;
  29. using System.Collections;
  30. using System.Threading;
  31. namespace System.Web.Caching {
  32. /// <summary>
  33. /// Class responsible for handling time based flushing of entries in the cache. The class creates
  34. /// and manages 60 buckets each holding every item that expires that minute. The bucket calculated
  35. /// for an entry is one minute more than the timeout just to make sure that the item end up in the
  36. /// bucket where it should be flushed.
  37. /// </summary>
  38. internal class CacheExpires : IDisposable {
  39. static int _intFlush;
  40. /// <summary>
  41. /// 1 bucket == 1 minute == 10M ticks (1 second) * 60
  42. /// </summary>
  43. static long _ticksPerBucket = 600000000;
  44. /// <summary>
  45. /// 1 cycle == 1 hour
  46. /// </summary>
  47. static long _ticksPerCycle = _ticksPerBucket * 60;
  48. private ExpiresBucket[] _arrBuckets;
  49. private Timer _objTimer;
  50. private Cache _objManager;
  51. private object _lockObj = new object ();
  52. internal CacheExpires (Cache objManager) {
  53. _objManager = objManager;
  54. Initialize();
  55. }
  56. private void Initialize () {
  57. // Create one bucket per minute
  58. _arrBuckets = new ExpiresBucket [60];
  59. byte bytePos = 0;
  60. do {
  61. _arrBuckets [bytePos] = new ExpiresBucket (bytePos, _objManager);
  62. bytePos++;
  63. } while (bytePos < 60);
  64. // GC Bucket controller
  65. _intFlush = System.DateTime.UtcNow.Minute - 1;
  66. _objTimer = new System.Threading.Timer (new System.Threading.TimerCallback (GarbageCleanup), null, 10000, 60000);
  67. }
  68. /// <summary>
  69. /// Adds a Cache entry to the correct flush bucket.
  70. /// </summary>
  71. /// <param name="objEntry">Cache entry to add.</param>
  72. internal void Add (CacheEntry objEntry) {
  73. long now = DateTime.UtcNow.Ticks;
  74. if (objEntry.Expires < now)
  75. objEntry.Expires = now;
  76. _arrBuckets [GetHashBucket (objEntry.Expires)].Add (objEntry);
  77. }
  78. internal void Remove (CacheEntry objEntry) {
  79. if (objEntry.ExpiresBucket != CacheEntry.NoBucketHash)
  80. _arrBuckets [objEntry.ExpiresBucket].Remove (objEntry);
  81. }
  82. internal void Update (CacheEntry objEntry, long ticksExpires) {
  83. // If the entry doesn't have a expires time we assume that the entry is due to expire now.
  84. int oldBucket = objEntry.ExpiresBucket;
  85. int newBucket = GetHashBucket (ticksExpires);
  86. if (oldBucket == CacheEntry.NoBucketHash)
  87. return;
  88. // Check if we need to move the item
  89. if (oldBucket != newBucket) {
  90. _arrBuckets [oldBucket].Remove (objEntry);
  91. objEntry.Expires = ticksExpires;
  92. _arrBuckets [newBucket].Add (objEntry);
  93. } else
  94. _arrBuckets [oldBucket].Update (objEntry, ticksExpires);
  95. }
  96. internal void GarbageCleanup (object State) {
  97. int bucket;
  98. // We lock here if FlushExpiredItems take time
  99. lock (_lockObj) {
  100. bucket = (++_intFlush) % 60;
  101. }
  102. // Flush expired items in the current bucket (defined by _intFlush)
  103. _arrBuckets [bucket].FlushExpiredItems ();
  104. }
  105. private int GetHashBucket (long ticks) {
  106. // Get bucket to add expire item into, add one minute to the bucket just to make sure that we get it in the bucket gc
  107. return (int) (((((ticks + 60000) % _ticksPerCycle) / _ticksPerBucket) + 1) % 60);
  108. }
  109. /// <summary>
  110. /// Called by the cache for cleanup.
  111. /// </summary>
  112. public void Dispose () {
  113. // Cleanup the internal timer
  114. if (_objTimer != null) {
  115. _objTimer.Dispose();
  116. _objTimer = null;
  117. }
  118. }
  119. }
  120. }