|
| 1 | +// This file contains portions of code released by Microsoft under the MIT license as part |
| 2 | +// of an open-sourcing initiative in 2014 of the C# core libraries. |
| 3 | +// The original source was submitted to https://github.com/Microsoft/referencesource |
| 4 | + |
| 5 | +using System.Caching.Timer; |
| 6 | +using System.Threading; |
| 7 | + |
| 8 | +namespace System.Caching |
| 9 | +{ |
| 10 | + sealed class CacheExpires |
| 11 | + { |
| 12 | + public static readonly TimeSpan MIN_UPDATE_DELTA = TimeSpan(0, 0, 1); |
| 13 | + public static readonly TimeSpan MIN_FLUSH_INTERVAL = TimeSpan(0, 0, 1); |
| 14 | + public static readonly TimeSpan _tsPerBucket = TimeSpan(0, 0, 20); |
| 15 | + private const int NUMBUCKETS = 30; |
| 16 | + private static readonly TimeSpan s_tsPerCycle = TimeSpan(30L * _tsPerBucket.Ticks); |
| 17 | + private readonly MemoryCacheStore _cacheStore; |
| 18 | + private readonly ExpiresBucket[] _buckets = new .[30] ~ DeleteContainerAndItems!(_); |
| 19 | + private PeriodicCallback _timer = null ~ if (_ != null) delete _; |
| 20 | + private DateTime _utcLastFlush; |
| 21 | + private int _inFlush; |
| 22 | + |
| 23 | + public this(MemoryCacheStore cacheStore) |
| 24 | + { |
| 25 | + DateTime utcNow = DateTime.UtcNow; |
| 26 | + _cacheStore = cacheStore; |
| 27 | + uint8 b = 0; |
| 28 | + |
| 29 | + while ((int)b < _buckets.Count) |
| 30 | + { |
| 31 | + _buckets[(int)b] = new .(this, b, utcNow); |
| 32 | + b += 1; |
| 33 | + } |
| 34 | + } |
| 35 | + |
| 36 | + private int UtcCalcExpiresBucket(DateTime utcDate) => |
| 37 | + (int)(((utcDate.Ticks % s_tsPerCycle.Ticks) / _tsPerBucket.Ticks + 1L) % 30L); |
| 38 | + |
| 39 | + private int FlushExpiredItems(bool checkDelta, bool useInsertBlock) |
| 40 | + { |
| 41 | + int flushedCount = 0; |
| 42 | + |
| 43 | + if (Interlocked.Exchange(ref _inFlush, 1) == 0) |
| 44 | + { |
| 45 | + if (_timer == null) |
| 46 | + return 0; |
| 47 | + |
| 48 | + DateTime utcNow = DateTime.UtcNow; |
| 49 | + |
| 50 | + if (!checkDelta || (utcNow - _utcLastFlush) >= MIN_FLUSH_INTERVAL || utcNow < _utcLastFlush) |
| 51 | + { |
| 52 | + _utcLastFlush = utcNow; |
| 53 | + |
| 54 | + for (ExpiresBucket expiresBucket in _buckets) |
| 55 | + flushedCount += expiresBucket.FlushExpiredItems(utcNow, useInsertBlock); |
| 56 | + } |
| 57 | + |
| 58 | + Interlocked.Exchange(ref _inFlush, 0); |
| 59 | + } |
| 60 | + |
| 61 | + return flushedCount; |
| 62 | + } |
| 63 | + |
| 64 | + public int FlushExpiredItems(bool useInsertBlock) => |
| 65 | + FlushExpiredItems(true, useInsertBlock); |
| 66 | + |
| 67 | + private void TimerCallback(PeriodicCallback state) => |
| 68 | + FlushExpiredItems(false, false); |
| 69 | + |
| 70 | + public void EnableExpirationTimer(bool enable) |
| 71 | + { |
| 72 | + if (enable) |
| 73 | + { |
| 74 | + if (_timer == null) |
| 75 | + { |
| 76 | + DateTime utcNow = DateTime.UtcNow; |
| 77 | + TimeSpan timeSpan = _tsPerBucket - TimeSpan(utcNow.Ticks % _tsPerBucket.Ticks); |
| 78 | + _timer = new .(new => TimerCallback, timeSpan.Ticks / 10000L); |
| 79 | + return; |
| 80 | + } |
| 81 | + } |
| 82 | + else |
| 83 | + { |
| 84 | + PeriodicCallback timer = _timer; |
| 85 | + |
| 86 | + if (timer != null && Interlocked.CompareExchange(ref _timer, null, timer) == timer) |
| 87 | + { |
| 88 | + timer.Dispose(); |
| 89 | + |
| 90 | + while (_inFlush != 0) |
| 91 | + Thread.Sleep(100); |
| 92 | + } |
| 93 | + } |
| 94 | + } |
| 95 | + |
| 96 | + public MemoryCacheStore MemoryCacheStore |
| 97 | + { |
| 98 | + get { return _cacheStore; } |
| 99 | + } |
| 100 | + |
| 101 | + public void Add(MemoryCacheEntry cacheEntry) |
| 102 | + { |
| 103 | + DateTime utcNow = DateTime.UtcNow; |
| 104 | + |
| 105 | + if (utcNow > cacheEntry.UtcAbsExp) |
| 106 | + cacheEntry.UtcAbsExp = utcNow; |
| 107 | + |
| 108 | + _buckets[UtcCalcExpiresBucket(cacheEntry.UtcAbsExp)].AddCacheEntry(cacheEntry); |
| 109 | + } |
| 110 | + |
| 111 | + public void Remove(MemoryCacheEntry cacheEntry) |
| 112 | + { |
| 113 | + uint8 expiresBucket = cacheEntry.ExpiresBucket; |
| 114 | + |
| 115 | + if (expiresBucket != 255) |
| 116 | + _buckets[(int)expiresBucket].RemoveCacheEntry(cacheEntry); |
| 117 | + } |
| 118 | + |
| 119 | + public void UtcUpdate(MemoryCacheEntry cacheEntry, DateTime utcNewExpires) |
| 120 | + { |
| 121 | + int expiresBucket = (int)cacheEntry.ExpiresBucket; |
| 122 | + int bucketIdx = UtcCalcExpiresBucket(utcNewExpires); |
| 123 | + |
| 124 | + if (expiresBucket != bucketIdx) |
| 125 | + { |
| 126 | + if (expiresBucket != 255) |
| 127 | + { |
| 128 | + _buckets[expiresBucket].RemoveCacheEntry(cacheEntry); |
| 129 | + cacheEntry.UtcAbsExp = utcNewExpires; |
| 130 | + _buckets[bucketIdx].AddCacheEntry(cacheEntry); |
| 131 | + return; |
| 132 | + } |
| 133 | + } |
| 134 | + else if (expiresBucket != 255) |
| 135 | + { |
| 136 | + _buckets[expiresBucket].UtcUpdateCacheEntry(cacheEntry, utcNewExpires); |
| 137 | + } |
| 138 | + } |
| 139 | + } |
| 140 | +} |
0 commit comments