| | | 1 | | using System; |
| | | 2 | | using System.Collections.Concurrent; |
| | | 3 | | using System.Collections.Generic; |
| | | 4 | | using System.Threading; |
| | | 5 | | using System.Threading.Tasks; |
| | | 6 | | |
| | | 7 | | namespace AmbientServices; |
| | | 8 | | |
| | | 9 | | /// <summary> |
| | | 10 | | /// Default in-process implementation of <see cref="IAmbientAtomicCache"/> using concurrent dictionaries and optimistic |
| | | 11 | | /// </summary> |
| | | 12 | | /// <remarks> |
| | | 13 | | /// <pitch>The zero-configuration atomic cache used unless overridden: a single-process realization (<see cref="IsShared |
| | | 14 | | /// <pledge><see cref="IAmbientAtomicCache"/></pledge> |
| | | 15 | | /// <pledge>Optimistic add/update retries are capped at thirty seconds of <see cref="AmbientClock"/> time, shortened fur |
| | | 16 | | /// <pledge>Because <see cref="IsShared"/> is false, disposable values are permitted, and dispose responsibility is infe |
| | | 17 | | /// <plan> |
| | | 18 | | /// One <see cref="ConcurrentDictionary{TKey,TValue}"/> holds both operation families, with single-character storage-key |
| | | 19 | | /// Installs are compare-and-swap loops over TryAdd/TryUpdate: factories run outside any lock, losers are disposed via a |
| | | 20 | | /// Disposal is driven off a type test taken when the entry is built, so the cache disposes a losing factory result, a r |
| | | 21 | | /// A displaced versioned entry is disposed once, awaited, outside the dictionary rather than inside an update delegate, |
| | | 22 | | /// Caller timeouts are realized by linking the caller's token to an <see cref="AmbientCancellationTokenSource"/> (an al |
| | | 23 | | /// Size is bounded by the same timed/untimed queue bookkeeping, cadence-driven ejection, and bounded <see cref="Clear"/ |
| | | 24 | | /// Trade-offs: no cross-process sharing or durability, approximate size bounds, and duplicate factory work under conten |
| | | 25 | | /// </plan> |
| | | 26 | | /// <priority> |
| | | 27 | | /// <see cref="IAmbientAtomicCache"/> |
| | | 28 | | /// 1. Sharing the local cache's bookkeeping over tuning its own: size bounding reuses <see cref="BasicAmbientLocalCache |
| | | 29 | | /// </priority> |
| | | 30 | | /// <para>Bounded size is enforced by ejecting timed and untimed bookkeeping rows on a configurable cadence. Settings u |
| | | 31 | | /// <para>Expiration comparisons and optimistic retry deadlines use <see cref="AmbientClock"/> so tests can pause or ski |
| | | 32 | | /// <para><see cref="Clear"/> snapshots the cache and ejects each entry in a bounded number of passes. Concurrent insta |
| | | 33 | | /// </remarks> |
| | | 34 | | [DefaultAmbientService] |
| | | 35 | | internal class BasicAmbientAtomicCache : IAmbientAtomicCache |
| | | 36 | | { |
| | | 37 | | /// <summary>Distinct one-char prefix so unversioned and versioned logical keys never share one dictionary slot.</su |
| | | 38 | | private const string UnversionedStorageKeyPrefix = "N"; |
| | | 39 | | |
| | | 40 | | /// <summary>Distinct one-char prefix so unversioned and versioned logical keys never share one dictionary slot.</su |
| | | 41 | | private const string VersionedStorageKeyPrefix = "V"; |
| | | 42 | | |
| | | 43 | | /// <summary>Wall-clock budget for optimistic <see cref="GetOrAdd{T}"/> / <see cref="AddOrUpdate{T}"/> CAS-style ret |
| | 2 | 44 | | private static readonly TimeSpan MaxOptimisticRetryDuration = TimeSpan.FromSeconds(30); |
| | | 45 | | |
| | | 46 | | private const string OptimisticRetryBudgetExceededMessage = "The atomic cache optimistic retry budget was exceeded." |
| | | 47 | | |
| | | 48 | | /// <summary>Caps stale-queue draining per eject call so a pathological queue cannot spin unbounded in one async con |
| | | 49 | | private const int MaxEjectQueueDrainSteps = 65536; |
| | | 50 | | |
| | | 51 | | /// <summary>Maximum snapshot-and-eject passes in <see cref="Clear"/> before giving up on reaching an empty cache.</ |
| | | 52 | | private const int MaxClearPasses = 8; |
| | | 53 | | |
| | 2 | 54 | | private static readonly AmbientService<IAmbientSettingsSet> _Settings = Ambient.GetService<IAmbientSettingsSet>(); |
| | | 55 | | |
| | | 56 | | private readonly IAmbientSetting<int> _callFrequencyToEject; |
| | | 57 | | private readonly IAmbientSetting<int> _countToEject; |
| | | 58 | | private readonly IAmbientSetting<int> _minCacheEntries; |
| | | 59 | | private int _expireCount; |
| | 2 | 60 | | private ConcurrentQueue<TimedQueueEntry> _timedQueue = new(); // interlocked (make readonly when we no longer supp |
| | 2 | 61 | | private ConcurrentQueue<string> _untimedQueue = new(); // interlocked (make readonly when we no longer supp |
| | 2 | 62 | | private readonly ConcurrentDictionary<string, CacheEntry> _cache = new(); |
| | 2 | 63 | | private readonly ConcurrentDictionary<string, VersionCounter> _versionCounters = new(); |
| | | 64 | | |
| | | 65 | | private sealed class VersionCounter |
| | | 66 | | { |
| | | 67 | | internal long Last; |
| | | 68 | | } |
| | | 69 | | |
| | | 70 | | public BasicAmbientAtomicCache() |
| | 2 | 71 | | : this(_Settings.Local) |
| | | 72 | | { |
| | 2 | 73 | | } |
| | | 74 | | |
| | 2 | 75 | | public BasicAmbientAtomicCache(IAmbientSettingsSet? settings) |
| | | 76 | | { |
| | 2 | 77 | | _callFrequencyToEject = AmbientSettings.GetSetting<int>(settings, nameof(BasicAmbientAtomicCache) + "-EjectFrequ |
| | 2 | 78 | | _countToEject = AmbientSettings.GetSetting<int>(settings, nameof(BasicAmbientAtomicCache) + "-MaximumItemCount", |
| | 2 | 79 | | _minCacheEntries = AmbientSettings.GetSetting<int>(settings, nameof(BasicAmbientAtomicCache) + "-MinimumItemCoun |
| | 2 | 80 | | } |
| | | 81 | | |
| | 2 | 82 | | private static string GetUnversionedStorageKey(string itemKey) => UnversionedStorageKeyPrefix + itemKey; |
| | | 83 | | |
| | 2 | 84 | | private static string GetVersionedStorageKey(string itemKey) => VersionedStorageKeyPrefix + itemKey; |
| | | 85 | | |
| | | 86 | | private struct TimedQueueEntry |
| | | 87 | | { |
| | | 88 | | public string Key; |
| | | 89 | | public DateTime Expiration; |
| | | 90 | | } |
| | | 91 | | |
| | | 92 | | private static bool ShouldDisposeWhenDiscarding(object entry) |
| | | 93 | | { |
| | 2 | 94 | | return entry is IAsyncDisposable || entry is IDisposable; |
| | | 95 | | } |
| | | 96 | | |
| | | 97 | | private readonly struct TimeoutCancellationRegistration : IDisposable |
| | | 98 | | { |
| | | 99 | | public CancellationToken Token { get; } |
| | | 100 | | private readonly IDisposable? _cleanup; |
| | | 101 | | |
| | | 102 | | public static TimeoutCancellationRegistration Create(TimeSpan? timeout, CancellationToken cancel) |
| | | 103 | | { |
| | 2 | 104 | | if (timeout is not TimeSpan to) |
| | | 105 | | { |
| | 2 | 106 | | return new TimeoutCancellationRegistration(null, cancel); |
| | | 107 | | } |
| | | 108 | | |
| | | 109 | | // System.Timers.Timer rejects non-positive intervals; use an already-cancelled CTS for "elapsed now" budget |
| | 2 | 110 | | if (to <= TimeSpan.Zero) |
| | | 111 | | { |
| | | 112 | | #pragma warning disable CA2000 // dispose ownership transferred to TimeoutCancellationRegistration or linked CTS |
| | 2 | 113 | | CancellationTokenSource immediateTimeout = new(); |
| | | 114 | | #pragma warning restore CA2000 |
| | 2 | 115 | | immediateTimeout.Cancel(); |
| | 2 | 116 | | if (!cancel.CanBeCanceled) |
| | | 117 | | { |
| | 2 | 118 | | return new TimeoutCancellationRegistration(immediateTimeout, immediateTimeout.Token); |
| | | 119 | | } |
| | | 120 | | |
| | 2 | 121 | | CancellationTokenSource linkedImmediateTimeout = CancellationTokenSource.CreateLinkedTokenSource(cancel, |
| | 2 | 122 | | return new TimeoutCancellationRegistration(linkedImmediateTimeout, linkedImmediateTimeout.Token); |
| | | 123 | | } |
| | | 124 | | |
| | 2 | 125 | | AmbientCancellationTokenSource ambientTimeout = new(to); |
| | 2 | 126 | | if (!cancel.CanBeCanceled) |
| | | 127 | | { |
| | 2 | 128 | | return new TimeoutCancellationRegistration(ambientTimeout, ambientTimeout.Token); |
| | | 129 | | } |
| | | 130 | | |
| | 2 | 131 | | CancellationTokenSource linkedAmbientTimeout = CancellationTokenSource.CreateLinkedTokenSource(cancel, ambie |
| | 2 | 132 | | return new TimeoutCancellationRegistration(new TimeoutCleanup(linkedAmbientTimeout, ambientTimeout), linkedA |
| | | 133 | | } |
| | | 134 | | |
| | | 135 | | private TimeoutCancellationRegistration(IDisposable? cleanup, CancellationToken token) |
| | | 136 | | { |
| | 2 | 137 | | Token = token; |
| | 2 | 138 | | _cleanup = cleanup; |
| | 2 | 139 | | } |
| | | 140 | | |
| | 2 | 141 | | public void Dispose() => _cleanup?.Dispose(); |
| | | 142 | | } |
| | | 143 | | |
| | | 144 | | private sealed class TimeoutCleanup : IDisposable |
| | | 145 | | { |
| | | 146 | | private readonly CancellationTokenSource _linked; |
| | | 147 | | private readonly AmbientCancellationTokenSource _ambient; |
| | | 148 | | |
| | 2 | 149 | | public TimeoutCleanup(CancellationTokenSource linked, AmbientCancellationTokenSource ambient) |
| | | 150 | | { |
| | 2 | 151 | | _linked = linked; |
| | 2 | 152 | | _ambient = ambient; |
| | 2 | 153 | | } |
| | | 154 | | |
| | | 155 | | public void Dispose() |
| | | 156 | | { |
| | 2 | 157 | | _linked.Dispose(); |
| | 2 | 158 | | _ambient.Dispose(); |
| | 2 | 159 | | } |
| | | 160 | | } |
| | | 161 | | |
| | | 162 | | private class CacheEntry |
| | | 163 | | { |
| | | 164 | | public bool DisposeWhenDiscarding { get; private set; } |
| | | 165 | | public string Key; |
| | | 166 | | public DateTime? Expiration; |
| | | 167 | | public object Entry; |
| | | 168 | | /// <summary>Monotonic revision for versioned storage keys; 0 for non-versioned atomic entries.</summary> |
| | | 169 | | public long MonotonicRevision; |
| | | 170 | | |
| | 2 | 171 | | public CacheEntry(string key, DateTime? expiration, object entry, bool disposeWhenDiscarding, long monotonicRevi |
| | | 172 | | { |
| | 2 | 173 | | DisposeWhenDiscarding = disposeWhenDiscarding; |
| | 2 | 174 | | Key = key; |
| | 2 | 175 | | Expiration = expiration; |
| | 2 | 176 | | Entry = entry; |
| | 2 | 177 | | MonotonicRevision = monotonicRevision; |
| | 2 | 178 | | } |
| | | 179 | | public async ValueTask Dispose() |
| | | 180 | | { |
| | | 181 | | // if the entry is disposable, dispose it after removing it |
| | 2 | 182 | | if (DisposeWhenDiscarding) await DisposeDiscardedValue(Entry); |
| | 2 | 183 | | } |
| | | 184 | | } |
| | | 185 | | |
| | | 186 | | private static DateTime? NormalizeExpiresInstant(DateTime? expires) |
| | | 187 | | { |
| | 2 | 188 | | if (expires == null) return null; |
| | 2 | 189 | | DateTime e = expires.Value; |
| | 2 | 190 | | if (e.Kind == DateTimeKind.Local) return e.ToUniversalTime(); |
| | 2 | 191 | | if (e.Kind == DateTimeKind.Unspecified) return DateTime.SpecifyKind(e, DateTimeKind.Utc); |
| | 2 | 192 | | return e; |
| | | 193 | | } |
| | | 194 | | |
| | 2 | 195 | | private static bool IsExpired(CacheEntry entry, DateTime nowUtc) => entry.Expiration < nowUtc; |
| | | 196 | | |
| | | 197 | | private static DateTime? ComputeActualExpiration(TimeSpan? maxCacheDuration, DateTime? expiration, DateTime utcNow) |
| | | 198 | | { |
| | 2 | 199 | | if (maxCacheDuration < TimeSpan.FromTicks(0)) return null; |
| | 2 | 200 | | DateTime? actualExpiration = null; |
| | 2 | 201 | | if (maxCacheDuration != null) actualExpiration = utcNow.Add(maxCacheDuration.Value); |
| | 2 | 202 | | if (expiration != null) |
| | | 203 | | { |
| | 2 | 204 | | DateTime exp = expiration.Value; |
| | 2 | 205 | | if (exp.Kind == DateTimeKind.Local) exp = exp.ToUniversalTime(); |
| | 2 | 206 | | else if (exp.Kind == DateTimeKind.Unspecified) exp = DateTime.SpecifyKind(exp, DateTimeKind.Utc); |
| | 2 | 207 | | if (actualExpiration == null || exp < actualExpiration) actualExpiration = exp; |
| | | 208 | | } |
| | 2 | 209 | | return actualExpiration; |
| | | 210 | | } |
| | | 211 | | |
| | | 212 | | private static DateTime ComputeOptimisticRetryDeadlineUtc(TimeSpan? operationTimeout) |
| | | 213 | | { |
| | 2 | 214 | | DateTime utcNow = AmbientClock.UtcNow; |
| | 2 | 215 | | DateTime deadline = utcNow.Add(MaxOptimisticRetryDuration); |
| | 2 | 216 | | if (operationTimeout is TimeSpan to) |
| | | 217 | | { |
| | 2 | 218 | | DateTime byCallerTimeout = utcNow.Add(to); |
| | 2 | 219 | | if (byCallerTimeout < deadline) |
| | | 220 | | { |
| | 2 | 221 | | deadline = byCallerTimeout; |
| | | 222 | | } |
| | | 223 | | } |
| | | 224 | | |
| | 2 | 225 | | return deadline; |
| | | 226 | | } |
| | | 227 | | |
| | | 228 | | private static void ThrowIfOptimisticRetryDeadlineExceeded(DateTime deadlineUtc) |
| | | 229 | | { |
| | 2 | 230 | | if (AmbientClock.UtcNow >= deadlineUtc) |
| | 2 | 231 | | throw new InvalidOperationException(OptimisticRetryBudgetExceededMessage); |
| | 2 | 232 | | } |
| | | 233 | | |
| | | 234 | | /// <summary> |
| | | 235 | | /// If the token is already canceled only because the optimistic-retry window elapsed (ambient clock), |
| | | 236 | | /// throw <see cref="InvalidOperationException"/> instead of <see cref="OperationCanceledException"/> so callers |
| | | 237 | | /// do not treat policy timeout as cooperative cancellation. |
| | | 238 | | /// </summary> |
| | | 239 | | private static void ThrowIfCancellationUnlessRetryDeadlineExceeded(DateTime optimisticRetryDeadlineUtc, Cancellation |
| | | 240 | | { |
| | 2 | 241 | | if (!effectiveCancel.IsCancellationRequested) return; |
| | 2 | 242 | | ThrowIfOptimisticRetryDeadlineExceeded(optimisticRetryDeadlineUtc); |
| | 2 | 243 | | effectiveCancel.ThrowIfCancellationRequested(); |
| | 0 | 244 | | } |
| | | 245 | | |
| | | 246 | | private void EnqueueExpiration(string storageKey, DateTime? actualExpiration) |
| | | 247 | | { |
| | 2 | 248 | | if (actualExpiration == null) |
| | | 249 | | { |
| | 2 | 250 | | _untimedQueue.Enqueue(storageKey); |
| | | 251 | | } |
| | | 252 | | else |
| | | 253 | | { |
| | 2 | 254 | | _timedQueue.Enqueue(new TimedQueueEntry { Key = storageKey, Expiration = actualExpiration.Value }); |
| | | 255 | | } |
| | 2 | 256 | | } |
| | | 257 | | |
| | | 258 | | /// <summary> |
| | | 259 | | /// Disposes a value the cache is discarding, preferring asynchronous disposal when the value implements both interf |
| | | 260 | | /// </summary> |
| | | 261 | | private static async ValueTask DisposeDiscardedValue(object value) |
| | | 262 | | { |
| | 2 | 263 | | if (value is IAsyncDisposable asyncDisposable) await asyncDisposable.DisposeAsync(); |
| | 2 | 264 | | else if (value is IDisposable disposable) disposable.Dispose(); |
| | 2 | 265 | | } |
| | | 266 | | |
| | | 267 | | /// <inheritdoc/> |
| | 2 | 268 | | public bool IsShared => false; |
| | | 269 | | |
| | | 270 | | /// <inheritdoc/> |
| | | 271 | | public async ValueTask<T> GetOrAdd<T>(string itemKey, Func<ValueTask<(T Item, DateTime? Expires)>> create, TimeSpan? |
| | | 272 | | { |
| | 2 | 273 | | using TimeoutCancellationRegistration timeoutRegistration = TimeoutCancellationRegistration.Create(timeout, canc |
| | 2 | 274 | | CancellationToken effectiveCancel = timeoutRegistration.Token; |
| | 2 | 275 | | string storageKey = GetUnversionedStorageKey(itemKey); |
| | 2 | 276 | | DateTime optimisticRetryDeadlineUtc = ComputeOptimisticRetryDeadlineUtc(timeout); |
| | 2 | 277 | | while (AmbientClock.UtcNow < optimisticRetryDeadlineUtc) |
| | | 278 | | { |
| | 2 | 279 | | ThrowIfOptimisticRetryDeadlineExceeded(optimisticRetryDeadlineUtc); |
| | 2 | 280 | | ThrowIfCancellationUnlessRetryDeadlineExceeded(optimisticRetryDeadlineUtc, effectiveCancel); |
| | 2 | 281 | | DateTime now = AmbientClock.UtcNow; |
| | 2 | 282 | | if (_cache.TryGetValue(storageKey, out CacheEntry? entry)) |
| | | 283 | | { |
| | 2 | 284 | | if (!IsExpired(entry, now)) |
| | | 285 | | { |
| | 2 | 286 | | if (refresh != null) |
| | | 287 | | { |
| | 2 | 288 | | DateTime newExpiration = now.Add(refresh.Value); |
| | 2 | 289 | | entry.Expiration = newExpiration; |
| | 2 | 290 | | _timedQueue.Enqueue(new TimedQueueEntry { Key = storageKey, Expiration = newExpiration }); |
| | | 291 | | } |
| | 2 | 292 | | await EjectIfNeeded(); |
| | 2 | 293 | | return (T)entry.Entry; |
| | | 294 | | } |
| | 2 | 295 | | await EjectEntry(entry); |
| | 2 | 296 | | ThrowIfOptimisticRetryDeadlineExceeded(optimisticRetryDeadlineUtc); |
| | | 297 | | } |
| | | 298 | | else |
| | | 299 | | { |
| | 2 | 300 | | await EjectIfNeeded(); |
| | 2 | 301 | | ThrowIfOptimisticRetryDeadlineExceeded(optimisticRetryDeadlineUtc); |
| | | 302 | | } |
| | | 303 | | |
| | 2 | 304 | | (T created, DateTime? expires) = await create(); |
| | 2 | 305 | | ThrowIfOptimisticRetryDeadlineExceeded(optimisticRetryDeadlineUtc); |
| | 2 | 306 | | DateTime? actualExpiration = NormalizeExpiresInstant(expires); |
| | 2 | 307 | | DateTime nowAfterCreate = AmbientClock.UtcNow; |
| | 2 | 308 | | if (actualExpiration < nowAfterCreate) |
| | | 309 | | { |
| | 2 | 310 | | await DisposeDiscardedValue(created); |
| | 2 | 311 | | ThrowIfOptimisticRetryDeadlineExceeded(optimisticRetryDeadlineUtc); |
| | 2 | 312 | | ThrowIfCancellationUnlessRetryDeadlineExceeded(optimisticRetryDeadlineUtc, effectiveCancel); |
| | 2 | 313 | | continue; |
| | | 314 | | } |
| | | 315 | | |
| | 2 | 316 | | CacheEntry newEntry = new(storageKey, actualExpiration, created, ShouldDisposeWhenDiscarding(created)); |
| | 2 | 317 | | if (_cache.TryAdd(storageKey, newEntry)) |
| | | 318 | | { |
| | 2 | 319 | | EnqueueExpiration(storageKey, actualExpiration); |
| | 2 | 320 | | await EjectIfNeeded(); |
| | 2 | 321 | | return created; |
| | | 322 | | } |
| | | 323 | | |
| | 0 | 324 | | await DisposeDiscardedValue(created); |
| | 0 | 325 | | } |
| | | 326 | | |
| | 2 | 327 | | throw new InvalidOperationException(OptimisticRetryBudgetExceededMessage); |
| | 2 | 328 | | } |
| | | 329 | | |
| | | 330 | | /// <inheritdoc/> |
| | | 331 | | public async ValueTask<T> AddOrUpdate<T>(string itemKey, Func<ValueTask<(T Item, DateTime? Expires)>> create, Func<T |
| | | 332 | | { |
| | 2 | 333 | | using TimeoutCancellationRegistration timeoutRegistration = TimeoutCancellationRegistration.Create(timeout, canc |
| | 2 | 334 | | CancellationToken effectiveCancel = timeoutRegistration.Token; |
| | 2 | 335 | | string storageKey = GetUnversionedStorageKey(itemKey); |
| | 2 | 336 | | DateTime optimisticRetryDeadlineUtc = ComputeOptimisticRetryDeadlineUtc(timeout); |
| | 2 | 337 | | while (AmbientClock.UtcNow < optimisticRetryDeadlineUtc) |
| | | 338 | | { |
| | 2 | 339 | | ThrowIfOptimisticRetryDeadlineExceeded(optimisticRetryDeadlineUtc); |
| | 2 | 340 | | ThrowIfCancellationUnlessRetryDeadlineExceeded(optimisticRetryDeadlineUtc, effectiveCancel); |
| | 2 | 341 | | DateTime now = AmbientClock.UtcNow; |
| | 2 | 342 | | if (_cache.TryGetValue(storageKey, out CacheEntry? existing)) |
| | | 343 | | { |
| | 2 | 344 | | if (IsExpired(existing, now)) |
| | | 345 | | { |
| | 2 | 346 | | await EjectEntry(existing); |
| | 2 | 347 | | ThrowIfOptimisticRetryDeadlineExceeded(optimisticRetryDeadlineUtc); |
| | | 348 | | } |
| | | 349 | | else |
| | | 350 | | { |
| | 2 | 351 | | T current = (T)existing.Entry; |
| | 2 | 352 | | (T updatedItem, DateTime? expires) = await update(current); |
| | 2 | 353 | | ThrowIfOptimisticRetryDeadlineExceeded(optimisticRetryDeadlineUtc); |
| | 2 | 354 | | DateTime? actualExpiration = NormalizeExpiresInstant(expires); |
| | 2 | 355 | | DateTime nowAfterUpdate = AmbientClock.UtcNow; |
| | 2 | 356 | | if (actualExpiration < nowAfterUpdate) |
| | | 357 | | { |
| | 2 | 358 | | await DisposeDiscardedValue(updatedItem); |
| | 2 | 359 | | await EjectEntry(existing); |
| | 2 | 360 | | ThrowIfOptimisticRetryDeadlineExceeded(optimisticRetryDeadlineUtc); |
| | 2 | 361 | | ThrowIfCancellationUnlessRetryDeadlineExceeded(optimisticRetryDeadlineUtc, effectiveCancel); |
| | 2 | 362 | | continue; |
| | | 363 | | } |
| | | 364 | | |
| | 2 | 365 | | CacheEntry replacement = new(storageKey, actualExpiration, updatedItem, ShouldDisposeWhenDiscarding( |
| | 2 | 366 | | if (_cache.TryUpdate(storageKey, replacement, existing)) |
| | | 367 | | { |
| | 2 | 368 | | EnqueueExpiration(storageKey, actualExpiration); |
| | 2 | 369 | | await EjectIfNeeded(); |
| | 2 | 370 | | return updatedItem; |
| | | 371 | | } |
| | 2 | 372 | | await DisposeDiscardedValue(updatedItem); |
| | 2 | 373 | | continue; |
| | | 374 | | } |
| | | 375 | | } |
| | | 376 | | |
| | 2 | 377 | | (T created, DateTime? createExpires) = await create(); |
| | 2 | 378 | | ThrowIfOptimisticRetryDeadlineExceeded(optimisticRetryDeadlineUtc); |
| | 2 | 379 | | DateTime? createActualExpiration = NormalizeExpiresInstant(createExpires); |
| | 2 | 380 | | DateTime nowAfterCreate = AmbientClock.UtcNow; |
| | 2 | 381 | | if (createActualExpiration < nowAfterCreate) |
| | | 382 | | { |
| | 2 | 383 | | await DisposeDiscardedValue(created); |
| | 2 | 384 | | ThrowIfOptimisticRetryDeadlineExceeded(optimisticRetryDeadlineUtc); |
| | 2 | 385 | | ThrowIfCancellationUnlessRetryDeadlineExceeded(optimisticRetryDeadlineUtc, effectiveCancel); |
| | 2 | 386 | | continue; |
| | | 387 | | } |
| | | 388 | | |
| | 2 | 389 | | CacheEntry newEntry = new(storageKey, createActualExpiration, created, ShouldDisposeWhenDiscarding(created)) |
| | 2 | 390 | | if (_cache.TryAdd(storageKey, newEntry)) |
| | | 391 | | { |
| | 2 | 392 | | EnqueueExpiration(storageKey, createActualExpiration); |
| | 2 | 393 | | await EjectIfNeeded(); |
| | 2 | 394 | | return created; |
| | | 395 | | } |
| | | 396 | | |
| | 2 | 397 | | await DisposeDiscardedValue(created); |
| | 2 | 398 | | } |
| | | 399 | | |
| | 2 | 400 | | throw new InvalidOperationException(OptimisticRetryBudgetExceededMessage); |
| | 2 | 401 | | } |
| | | 402 | | |
| | | 403 | | /// <inheritdoc/> |
| | | 404 | | public async ValueTask Remove<T>(string itemKey, CancellationToken cancel = default) |
| | | 405 | | { |
| | 2 | 406 | | if (_cache.TryRemove(GetUnversionedStorageKey(itemKey), out CacheEntry? disposeEntry)) |
| | | 407 | | { |
| | 2 | 408 | | await disposeEntry!.Dispose(); |
| | | 409 | | } |
| | 2 | 410 | | } |
| | | 411 | | |
| | | 412 | | /// <inheritdoc/> |
| | | 413 | | public async ValueTask VersionedRemove<T>(string itemKey, CancellationToken cancel = default) |
| | | 414 | | { |
| | 2 | 415 | | if (_cache.TryRemove(GetVersionedStorageKey(itemKey), out CacheEntry? disposeEntry)) |
| | | 416 | | { |
| | 2 | 417 | | await disposeEntry!.Dispose(); |
| | | 418 | | } |
| | 2 | 419 | | } |
| | | 420 | | |
| | | 421 | | /// <inheritdoc/> |
| | | 422 | | public async ValueTask<(T? Value, long Version)> VersionedGet<T>(string itemKey, long minVersion = -1, TimeSpan? ref |
| | | 423 | | { |
| | 2 | 424 | | string storageKey = GetVersionedStorageKey(itemKey); |
| | 2 | 425 | | using TimeoutCancellationRegistration timeoutRegistration = TimeoutCancellationRegistration.Create(timeout, canc |
| | 2 | 426 | | CancellationToken effectiveCancel = timeoutRegistration.Token; |
| | 2 | 427 | | if (!_cache.TryGetValue(storageKey, out CacheEntry? entry)) |
| | | 428 | | { |
| | 2 | 429 | | return (default, 0); |
| | | 430 | | } |
| | | 431 | | |
| | 2 | 432 | | effectiveCancel.ThrowIfCancellationRequested(); |
| | 2 | 433 | | DateTime now = AmbientClock.UtcNow; |
| | 2 | 434 | | if (IsExpired(entry, now)) |
| | | 435 | | { |
| | 2 | 436 | | await EjectEntry(entry); |
| | 2 | 437 | | return (default, 0); |
| | | 438 | | } |
| | | 439 | | |
| | 2 | 440 | | if (entry.MonotonicRevision < minVersion) |
| | | 441 | | { |
| | 2 | 442 | | return (default, entry.MonotonicRevision); |
| | | 443 | | } |
| | | 444 | | |
| | 2 | 445 | | if (refresh != null) |
| | | 446 | | { |
| | 2 | 447 | | DateTime newExpiration = now.Add(refresh.Value); |
| | 2 | 448 | | entry.Expiration = newExpiration; |
| | 2 | 449 | | _timedQueue.Enqueue(new TimedQueueEntry { Key = storageKey, Expiration = newExpiration }); |
| | | 450 | | } |
| | 2 | 451 | | await EjectIfNeeded(); |
| | 2 | 452 | | return ((T)entry.Entry, entry.MonotonicRevision); |
| | 2 | 453 | | } |
| | | 454 | | |
| | | 455 | | /// <inheritdoc/> |
| | | 456 | | public async ValueTask<long> VersionedPut<T>(string itemKey, T value, TimeSpan? maxCacheDuration = null, DateTime? e |
| | | 457 | | { |
| | 2 | 458 | | if (maxCacheDuration < TimeSpan.FromTicks(0)) |
| | | 459 | | { |
| | 2 | 460 | | await DisposeDiscardedValue(value); |
| | 2 | 461 | | return 0; |
| | | 462 | | } |
| | | 463 | | |
| | 2 | 464 | | string storageKey = GetVersionedStorageKey(itemKey); |
| | 2 | 465 | | using TimeoutCancellationRegistration timeoutRegistration = TimeoutCancellationRegistration.Create(timeout, canc |
| | 2 | 466 | | CancellationToken effectiveCancel = timeoutRegistration.Token; |
| | 2 | 467 | | DateTime utcNow = AmbientClock.UtcNow; |
| | 2 | 468 | | DateTime? actualExpiration = ComputeActualExpiration(maxCacheDuration, expiration, utcNow); |
| | 2 | 469 | | if (actualExpiration < utcNow) |
| | | 470 | | { |
| | 2 | 471 | | await DisposeDiscardedValue(value); |
| | 2 | 472 | | return 0; |
| | | 473 | | } |
| | | 474 | | |
| | 2 | 475 | | effectiveCancel.ThrowIfCancellationRequested(); |
| | | 476 | | |
| | 2 | 477 | | VersionCounter vc = _versionCounters.GetOrAdd(itemKey, _ => new VersionCounter()); |
| | 2 | 478 | | long revision = Interlocked.Increment(ref vc.Last); |
| | 2 | 479 | | CacheEntry newEntry = new(storageKey, actualExpiration, value, ShouldDisposeWhenDiscarding(value), revision); |
| | | 480 | | // ConcurrentDictionary has no async update delegate, and its update delegate may run more than |
| | | 481 | | // once under contention — so a disposing side effect there can double-dispose. Do the compare-and-swap |
| | | 482 | | // ourselves: this displaces exactly one entry, which we then dispose once, awaited, outside the dictionary. |
| | 2 | 483 | | CacheEntry? displaced = null; |
| | | 484 | | while (true) |
| | | 485 | | { |
| | 2 | 486 | | if (_cache.TryGetValue(storageKey, out CacheEntry? old)) |
| | | 487 | | { |
| | 2 | 488 | | if (_cache.TryUpdate(storageKey, newEntry, old)) |
| | | 489 | | { |
| | 2 | 490 | | displaced = old; |
| | 2 | 491 | | break; |
| | | 492 | | } |
| | | 493 | | } |
| | 2 | 494 | | else if (_cache.TryAdd(storageKey, newEntry)) |
| | | 495 | | { |
| | | 496 | | break; |
| | | 497 | | } |
| | | 498 | | // another writer changed the slot first; re-read and retry |
| | | 499 | | } |
| | 2 | 500 | | if (displaced != null) await displaced.Dispose(); |
| | 2 | 501 | | EnqueueExpiration(storageKey, actualExpiration); |
| | 2 | 502 | | await EjectIfNeeded(); |
| | 2 | 503 | | return revision; |
| | 2 | 504 | | } |
| | | 505 | | |
| | | 506 | | private async ValueTask EjectIfNeeded() |
| | | 507 | | { |
| | 2 | 508 | | int callFrequencyToEject = _callFrequencyToEject.Value; |
| | 2 | 509 | | if (callFrequencyToEject <= 0) |
| | 2 | 510 | | callFrequencyToEject = 1; |
| | | 511 | | |
| | 2 | 512 | | int countToEject = _countToEject.Value; |
| | | 513 | | // One increment per cache operation (not per inner eject round): the previous loop incremented on every |
| | | 514 | | // pressure-driven iteration which inflated the cadence counter and could spin unbounded under concurrency |
| | | 515 | | // when queue bookkeeping lagged cache entries (ConcurrentQueue.Count is approximate) or ejection could not |
| | | 516 | | // shrink the queues in one pass. |
| | 2 | 517 | | int opSerial = Interlocked.Increment(ref _expireCount); |
| | 2 | 518 | | bool onCadence = (opSerial % callFrequencyToEject) == 0; |
| | 2 | 519 | | int queueSum = _untimedQueue.Count + _timedQueue.Count; |
| | 2 | 520 | | bool overCapacity = queueSum > countToEject; |
| | 2 | 521 | | if (!onCadence && !overCapacity) |
| | 2 | 522 | | return; |
| | | 523 | | |
| | 2 | 524 | | int maxRounds = Math.Max(32, Math.Min(131072, 4 + 2 * Math.Max(queueSum, countToEject + 1))); |
| | 2 | 525 | | for (int round = 0; round < maxRounds; round++) |
| | | 526 | | { |
| | 2 | 527 | | await EjectOneTimed(); |
| | 2 | 528 | | await EjectOneUntimed(); |
| | | 529 | | |
| | 2 | 530 | | queueSum = _untimedQueue.Count + _timedQueue.Count; |
| | 2 | 531 | | if (queueSum <= countToEject) |
| | | 532 | | break; |
| | | 533 | | } |
| | 2 | 534 | | } |
| | | 535 | | |
| | | 536 | | private async ValueTask EjectOneTimed() |
| | | 537 | | { |
| | | 538 | | // have we hit the minimum number of items? |
| | 2 | 539 | | if (_timedQueue.Count <= _minCacheEntries.Value) return; |
| | | 540 | | // removing at least one timed item (as well as any expired items we come across) |
| | 2 | 541 | | bool unexpiredItemEjected = false; |
| | 2 | 542 | | int steps = 0; |
| | 2 | 543 | | while (steps++ < MaxEjectQueueDrainSteps && _timedQueue.TryDequeue(out TimedQueueEntry qEntry)) |
| | | 544 | | { |
| | | 545 | | // Eject only when the cache still has this key with the same expiration as when the row was enqueued (other |
| | 2 | 546 | | if (_cache.TryGetValue(qEntry.Key, out CacheEntry? entry) && qEntry.Expiration == entry.Expiration) |
| | | 547 | | { |
| | | 548 | | // remove it from the cache, even though it may not have expired yet because it's time to eject somethin |
| | 2 | 549 | | await EjectEntry(entry); |
| | | 550 | | // fall through and check to see if the next item is already expired |
| | 2 | 551 | | unexpiredItemEjected = true; |
| | | 552 | | } |
| | | 553 | | // stale queue row or missing cache entry: if we have already ejected an unexpired item, check for another e |
| | 2 | 554 | | else if (!unexpiredItemEjected) |
| | | 555 | | { |
| | | 556 | | continue; |
| | | 557 | | } |
| | | 558 | | // peek at the next entry |
| | 2 | 559 | | if (_timedQueue.TryPeek(out qEntry)) |
| | | 560 | | { |
| | | 561 | | // has this entry expired? continue looping so that we remove this one too, even though we didn't *have* |
| | 2 | 562 | | if (qEntry.Expiration < AmbientClock.UtcNow) continue; |
| | | 563 | | // else the entry hasn't expired and we either removed an entry above or skipped this code, so we can ju |
| | | 564 | | } |
| | | 565 | | // if we get here, there is no reason to look at another timed entry |
| | | 566 | | break; |
| | | 567 | | } |
| | 2 | 568 | | } |
| | | 569 | | |
| | | 570 | | private async ValueTask EjectOneUntimed() |
| | | 571 | | { |
| | | 572 | | // have we hit the minimum number of items? |
| | 2 | 573 | | if (_untimedQueue.Count <= _minCacheEntries.Value) return; |
| | | 574 | | // remove one untimed entry |
| | 2 | 575 | | int steps = 0; |
| | 2 | 576 | | while (steps++ < MaxEjectQueueDrainSteps && _untimedQueue.TryDequeue(out string? key)) |
| | | 577 | | { |
| | | 578 | | // can we find this item in the cache? |
| | 2 | 579 | | if (_cache.TryGetValue(key, out CacheEntry? entry)) |
| | | 580 | | { |
| | | 581 | | // is the expiration still the same (ie. untimed)? |
| | 2 | 582 | | if (entry.Expiration == null) |
| | | 583 | | { |
| | | 584 | | // remove it from the cache |
| | 2 | 585 | | await EjectEntry(entry); |
| | | 586 | | // fall through and stop looping |
| | | 587 | | } |
| | | 588 | | else // else the item was refreshed, so we should ignore this entry and go around again to remove anothe |
| | | 589 | | { |
| | | 590 | | continue; |
| | | 591 | | } |
| | | 592 | | } |
| | | 593 | | // else we couldn't find the entry in the cache, so just move to the next entry |
| | | 594 | | else |
| | | 595 | | { |
| | | 596 | | continue; |
| | | 597 | | } |
| | | 598 | | // if we get here, there is no reason to look at another untimed entry |
| | | 599 | | break; |
| | | 600 | | } |
| | 2 | 601 | | } |
| | | 602 | | |
| | | 603 | | private async ValueTask EjectEntry(CacheEntry entry) |
| | | 604 | | { |
| | | 605 | | // race to remove the item from the cache--did we win the race? |
| | 2 | 606 | | if (_cache.TryRemove(entry.Key, out CacheEntry? disposeEntry)) |
| | | 607 | | { |
| | 2 | 608 | | await disposeEntry.Dispose(); |
| | | 609 | | } |
| | 2 | 610 | | } |
| | | 611 | | |
| | | 612 | | /// <inheritdoc/> |
| | | 613 | | public async ValueTask Clear(CancellationToken cancel = default) |
| | | 614 | | { |
| | 2 | 615 | | Interlocked.Exchange(ref _untimedQueue, new ConcurrentQueue<string>()); |
| | 2 | 616 | | Interlocked.Exchange(ref _timedQueue, new ConcurrentQueue<TimedQueueEntry>()); |
| | 2 | 617 | | _versionCounters.Clear(); |
| | | 618 | | |
| | 2 | 619 | | for (int pass = 0; pass < MaxClearPasses; pass++) |
| | | 620 | | { |
| | 2 | 621 | | KeyValuePair<string, CacheEntry>[] snapshot = _cache.ToArray(); |
| | 2 | 622 | | if (snapshot.Length == 0) |
| | | 623 | | break; |
| | | 624 | | |
| | 2 | 625 | | foreach (KeyValuePair<string, CacheEntry> kv in snapshot) |
| | | 626 | | { |
| | 2 | 627 | | cancel.ThrowIfCancellationRequested(); |
| | 2 | 628 | | await EjectEntry(kv.Value); |
| | | 629 | | } |
| | | 630 | | |
| | 2 | 631 | | if (_cache.IsEmpty) |
| | | 632 | | break; |
| | | 633 | | } |
| | 2 | 634 | | } |
| | | 635 | | } |