CacheExpires.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. //
  2. // System.Web.Caching
  3. //
  4. // Author:
  5. // Patrik Torstensson
  6. // Daniel Cazzulino ([email protected])
  7. //
  8. using System;
  9. using System.Collections;
  10. using System.Threading;
  11. namespace System.Web.Caching {
  12. /// <summary>
  13. /// Class responsible for handling time based flushing of entries in the cache. The class creates
  14. /// and manages 60 buckets each holding every item that expires that minute. The bucket calculated
  15. /// for an entry is one minute more than the timeout just to make sure that the item end up in the
  16. /// bucket where it should be flushed.
  17. /// </summary>
  18. internal class CacheExpires : IDisposable {
  19. static int _intFlush;
  20. /// <summary>
  21. /// 1 bucket == 1 minute == 10M ticks (1 second) * 60
  22. /// </summary>
  23. static long _ticksPerBucket = 600000000;
  24. /// <summary>
  25. /// 1 cycle == 1 hour
  26. /// </summary>
  27. static long _ticksPerCycle = _ticksPerBucket * 60;
  28. private ExpiresBucket[] _arrBuckets;
  29. private Timer _objTimer;
  30. private Cache _objManager;
  31. private object _lockObj = new object ();
  32. internal CacheExpires (Cache objManager) {
  33. _objManager = objManager;
  34. Initialize();
  35. }
  36. private void Initialize () {
  37. // Create one bucket per minute
  38. _arrBuckets = new ExpiresBucket [60];
  39. byte bytePos = 0;
  40. do {
  41. _arrBuckets [bytePos] = new ExpiresBucket (bytePos, _objManager);
  42. bytePos++;
  43. } while (bytePos < 60);
  44. // GC Bucket controller
  45. _intFlush = System.DateTime.Now.Minute - 1;
  46. _objTimer = new System.Threading.Timer (new System.Threading.TimerCallback (GarbageCleanup), null, 10000, 60000);
  47. }
  48. /// <summary>
  49. /// Adds a Cache entry to the correct flush bucket.
  50. /// </summary>
  51. /// <param name="objEntry">Cache entry to add.</param>
  52. internal void Add (CacheEntry objEntry) {
  53. long now = DateTime.Now.Ticks;
  54. if (objEntry.Expires < now)
  55. objEntry.Expires = now;
  56. _arrBuckets [GetHashBucket (objEntry.Expires)].Add (objEntry);
  57. }
  58. internal void Remove (CacheEntry objEntry) {
  59. if (objEntry.ExpiresBucket != CacheEntry.NoBucketHash)
  60. _arrBuckets [objEntry.ExpiresBucket].Remove (objEntry);
  61. }
  62. internal void Update (CacheEntry objEntry, long ticksExpires) {
  63. // If the entry doesn't have a expires time we assume that the entry is due to expire now.
  64. int oldBucket = objEntry.ExpiresBucket;
  65. int newBucket = GetHashBucket (ticksExpires);
  66. if (oldBucket == CacheEntry.NoBucketHash)
  67. return;
  68. // Check if we need to move the item
  69. if (oldBucket != newBucket) {
  70. _arrBuckets [oldBucket].Remove (objEntry);
  71. objEntry.Expires = ticksExpires;
  72. _arrBuckets [newBucket].Add (objEntry);
  73. } else
  74. _arrBuckets [oldBucket].Update (objEntry, ticksExpires);
  75. }
  76. internal void GarbageCleanup (object State) {
  77. ExpiresBucket objBucket;
  78. int bucket;
  79. // We lock here if FlushExpiredItems take time
  80. lock (_lockObj) {
  81. bucket = (++_intFlush) % 60;
  82. }
  83. // Flush expired items in the current bucket (defined by _intFlush)
  84. _arrBuckets [bucket].FlushExpiredItems ();
  85. }
  86. private int GetHashBucket (long ticks) {
  87. // 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
  88. return (int) (((((ticks + 60000) % _ticksPerCycle) / _ticksPerBucket) + 1) % 60);
  89. }
  90. /// <summary>
  91. /// Called by the cache for cleanup.
  92. /// </summary>
  93. public void Dispose () {
  94. // Cleanup the internal timer
  95. if (_objTimer != null) {
  96. _objTimer.Dispose();
  97. _objTimer = null;
  98. }
  99. }
  100. }
  101. }