• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

bitfaster / BitFaster.Caching / 25269777226

03 May 2026 04:24AM UTC coverage: 99.156% (-0.05%) from 99.21%
25269777226

Pull #801

github

web-flow
Merge 7f7035599 into b202a2474
Pull Request #801: Update gate workflow actions for Node.js 24

1386 of 1418 branches covered (97.74%)

Branch coverage included in aggregate %.

5895 of 5925 relevant lines covered (99.49%)

86840123.99 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

99.4
/BitFaster.Caching/Lru/ConcurrentLruCore.cs
1
using System;
2
using System.Collections;
3
using System.Collections.Concurrent;
4
using System.Collections.Generic;
5
using System.Diagnostics;
6
using System.Diagnostics.CodeAnalysis;
7
using System.Linq;
8
using System.Runtime.CompilerServices;
9
using System.Threading;
10
using System.Threading.Tasks;
11

12
namespace BitFaster.Caching.Lru
13
{
14
    /// <summary>
15
    /// A pseudo LRU based on the TU-Q eviction policy. The LRU list is composed of 3 segments: hot, warm and cold. 
16
    /// Cost of maintaining segments is amortized across requests. Items are only cycled when capacity is exceeded. 
17
    /// Pure read does not cycle items if all segments are within capacity constraints. There are no global locks. 
18
    /// On cache miss, a new item is added. Tail items in each segment are dequeued, examined, and are either enqueued 
19
    /// or discarded.
20
    /// The TU-Q scheme of hot, warm and cold is similar to that used in MemCached (https://memcached.org/blog/modern-lru/)
21
    /// and OpenBSD (https://flak.tedunangst.com/post/2Q-buffer-cache-algorithm), but does not use a background thread
22
    /// to maintain the internal queues.
23
    /// </summary>
24
    /// <remarks>
25
    /// Each segment has a capacity. When segment capacity is exceeded, items are moved as follows:
26
    /// <list type="number">
27
    ///   <item><description>New items are added to hot, WasAccessed = false.</description></item>
28
    ///   <item><description>When items are accessed, update WasAccessed = true.</description></item>
29
    ///   <item><description>When items are moved WasAccessed is set to false.</description></item>
30
    ///   <item><description>When hot is full, hot tail is moved to either Warm or Cold depending on WasAccessed.</description></item>
31
    ///   <item><description>When warm is full, warm tail is moved to warm head or cold depending on WasAccessed.</description></item>
32
    ///   <item><description>When cold is full, cold tail is moved to warm head or removed from dictionary on depending on WasAccessed.</description></item>
33
    ///</list>
34
    /// </remarks>
35
    public class ConcurrentLruCore<K, V, I, P, T> : ICacheExt<K, V>, IAsyncCacheExt<K, V>, IEnumerable<KeyValuePair<K, V>>
36
        where K : notnull
37
        where I : LruItem<K, V>
38
        where P : struct, IItemPolicy<K, V, I>
39
        where T : struct, ITelemetryPolicy<K, V>
40
    {
41
        private readonly ConcurrentDictionary<K, I> dictionary;
42

43
        private readonly ConcurrentQueue<I> hotQueue;
44
        private readonly ConcurrentQueue<I> warmQueue;
45
        private readonly ConcurrentQueue<I> coldQueue;
46

47
        // maintain count outside ConcurrentQueue, since ConcurrentQueue.Count holds a global lock
48
        private PaddedQueueCount counter;
49

50
        private readonly ICapacityPartition capacity;
51

52
        private readonly P itemPolicy;
53
        private bool isWarm = false;
4,198✔
54

55
        /// <summary>
56
        /// The telemetry policy.
57
        /// </summary>
58
        /// <remarks>
59
        /// Since T is a struct, making it readonly will force the runtime to make defensive copies
60
        /// if mutate methods are called. Therefore, field must be mutable to maintain count.
61
        /// </remarks>
62
        protected T telemetryPolicy;
63

64
        /// <summary>
65
        /// Initializes a new instance of the ConcurrentLruCore class with the specified concurrencyLevel, capacity, equality comparer, item policy and telemetry policy.
66
        /// </summary>
67
        /// <param name="concurrencyLevel">The concurrency level.</param>
68
        /// <param name="capacity">The capacity.</param>
69
        /// <param name="comparer">The equality comparer.</param>
70
        /// <param name="itemPolicy">The item policy.</param>
71
        /// <param name="telemetryPolicy">The telemetry policy.</param>
72
        /// <exception cref="ArgumentNullException"></exception>
73
        public ConcurrentLruCore(
4,198✔
74
            int concurrencyLevel,
4,198✔
75
            ICapacityPartition capacity,
4,198✔
76
            IEqualityComparer<K> comparer,
4,198✔
77
            P itemPolicy,
4,198✔
78
            T telemetryPolicy)
4,198✔
79
        {
4,198✔
80
            if (capacity == null)
4,198✔
81
                Throw.ArgNull(ExceptionArgument.capacity);
7✔
82

83
            if (comparer == null)
4,191✔
84
                Throw.ArgNull(ExceptionArgument.comparer);
7✔
85

86
            capacity.Validate();
4,184✔
87
            this.capacity = capacity;
4,177✔
88

89
            this.hotQueue = new ConcurrentQueue<I>();
4,177✔
90
            this.warmQueue = new ConcurrentQueue<I>();
4,177✔
91
            this.coldQueue = new ConcurrentQueue<I>();
4,177✔
92

93
            int dictionaryCapacity = ConcurrentDictionarySize.Estimate(this.Capacity);
4,177✔
94

95
            this.dictionary = new ConcurrentDictionary<K, I>(concurrencyLevel, dictionaryCapacity, comparer);
4,177✔
96
            this.itemPolicy = itemPolicy;
4,163✔
97
            this.telemetryPolicy = telemetryPolicy;
4,163✔
98
            this.telemetryPolicy.SetEventSource(this);
4,163✔
99
        }
4,163✔
100

101
        // No lock count: https://arbel.net/2013/02/03/best-practices-for-using-concurrentdictionary/
102
        ///<inheritdoc/>
103
        public int Count => this.dictionary.Where(i => !itemPolicy.ShouldDiscard(i.Value)).Count();
24,445,304✔
104

105
        ///<inheritdoc/>
106
        public int Capacity => this.capacity.Hot + this.capacity.Warm + this.capacity.Cold;
2,800,006,336✔
107

108
        ///<inheritdoc/>
109
        public Optional<ICacheMetrics> Metrics => CreateMetrics(this);
231✔
110

111
        ///<inheritdoc/>
112
        public Optional<ICacheEvents<K, V>> Events => CreateEvents(this);
2,624✔
113

114
        ///<inheritdoc/>
115
        public CachePolicy Policy => CreatePolicy(this);
483✔
116

117
        /// <summary>
118
        /// Gets the number of hot items.
119
        /// </summary>
120
        public int HotCount => Volatile.Read(ref this.counter.hot);
2,220,861✔
121

122
        /// <summary>
123
        /// Gets the number of warm items.
124
        /// </summary>
125
        public int WarmCount => Volatile.Read(ref this.counter.warm);
2,220,861✔
126

127
        /// <summary>
128
        /// Gets the number of cold items.
129
        /// </summary>
130
        public int ColdCount => Volatile.Read(ref this.counter.cold);
2,220,868✔
131

132
        /// <summary>
133
        /// Gets a collection containing the keys in the cache.
134
        /// </summary>
135
        public ICollection<K> Keys => this.dictionary.Keys;
1,550✔
136

137
#if NET9_0_OR_GREATER
138
        /// <inheritdoc/>
139
        public IEqualityComparer<K> Comparer => this.dictionary.Comparer;
276✔
140
#endif
141

142
        /// <summary>Returns an enumerator that iterates through the cache.</summary>
143
        /// <returns>An enumerator for the cache.</returns>
144
        /// <remarks>
145
        /// The enumerator returned from the cache is safe to use concurrently with
146
        /// reads and writes, however it does not represent a moment-in-time snapshot.  
147
        /// The contents exposed through the enumerator may contain modifications
148
        /// made after <see cref="GetEnumerator"/> was called.
149
        /// </remarks>
150
        public IEnumerator<KeyValuePair<K, V>> GetEnumerator()
151
        {
441✔
152
            foreach (var kvp in this.dictionary)
2,268✔
153
            {
476✔
154
                if (!itemPolicy.ShouldDiscard(kvp.Value))
476✔
155
                {
455✔
156
                    yield return new KeyValuePair<K, V>(kvp.Key, kvp.Value.Value);
455✔
157
                }
448✔
158
            }
469✔
159
        }
434✔
160

161
        ///<inheritdoc/>
162
        public bool TryGet(K key, [MaybeNullWhen(false)] out V value)
163
        {
3,190,539,094✔
164
            if (dictionary.TryGetValue(key, out var item))
3,190,539,094✔
165
            {
2,833,938,016✔
166
                return GetOrDiscard(item, out value);
2,833,938,016✔
167
            }
168

169
            value = default;
356,601,078✔
170
            this.telemetryPolicy.IncrementMiss();
356,601,078✔
171
            return false;
356,601,078✔
172
        }
3,190,539,094✔
173

174
        // AggressiveInlining forces the JIT to inline policy.ShouldDiscard(). For LRU policy 
175
        // the first branch is completely eliminated due to JIT time constant propogation.
176
        [MethodImpl(MethodImplOptions.AggressiveInlining)]
177
        private bool GetOrDiscard(I item, [MaybeNullWhen(false)] out V value)
178
        {
2,855,113,300✔
179
            if (this.itemPolicy.ShouldDiscard(item))
2,855,113,300✔
180
            {
6,333,457✔
181
                this.Move(item, ItemDestination.Remove, ItemRemovedReason.Evicted);
6,333,457✔
182
                this.telemetryPolicy.IncrementMiss();
6,333,457✔
183
                value = default;
6,333,457✔
184
                return false;
6,333,457✔
185
            }
186

187
            value = item.Value;
2,848,779,843✔
188

189
            this.itemPolicy.Touch(item);
2,848,779,843✔
190
            this.telemetryPolicy.IncrementHit();
2,848,779,843✔
191
            return true;
2,848,779,843✔
192
        }
2,855,113,300✔
193

194
        private bool TryAdd(K key, V value)
195
        {
479,441,834✔
196
            var newItem = this.itemPolicy.CreateItem(key, value);
479,441,834✔
197

198
            if (this.dictionary.TryAdd(key, newItem))
479,441,834✔
199
            {
466,112,182✔
200
                this.hotQueue.Enqueue(newItem);
466,112,182✔
201
                Cycle(Interlocked.Increment(ref counter.hot));
466,112,182✔
202
                return true;
466,112,182✔
203
            }
204

205
            Disposer<V>.Dispose(newItem.Value);
13,329,652✔
206
            return false;
13,329,652✔
207
        }
479,441,834✔
208

209
        ///<inheritdoc/>
210
        public V GetOrAdd(K key, Func<K, V> valueFactory)
211
        {
3,059,919,992✔
212
            while (true)
3,069,859,069✔
213
            {
3,069,859,069✔
214
                if (this.TryGet(key, out var value))
3,069,859,069✔
215
                {
2,814,932,912✔
216
                    return value;
2,814,932,912✔
217
                }
218

219
                // The value factory may be called concurrently for the same key, but the first write to the dictionary wins.
220
                value = valueFactory(key);
254,926,157✔
221

222
                if (TryAdd(key, value))
254,926,157✔
223
                {
244,987,080✔
224
                    return value;
244,987,080✔
225
                }
226
            }
9,939,077✔
227
        }
3,059,919,992✔
228

229
        /// <summary>
230
        /// Adds a key/value pair to the cache if the key does not already exist. Returns the new value, or the 
231
        /// existing value if the key already exists.
232
        /// </summary>
233
        /// <typeparam name="TArg">The type of an argument to pass into valueFactory.</typeparam>
234
        /// <param name="key">The key of the element to add.</param>
235
        /// <param name="valueFactory">The factory function used to generate a value for the key.</param>
236
        /// <param name="factoryArgument">An argument value to pass into valueFactory.</param>
237
        /// <returns>The value for the key. This will be either the existing value for the key if the key is already 
238
        /// in the cache, or the new value if the key was not in the cache.</returns>
239
        public V GetOrAdd<TArg>(K key, Func<K, TArg, V> valueFactory, TArg factoryArgument)
240
#if NET9_0_OR_GREATER
241
            where TArg : allows ref struct
242
#endif
243
        {
55,090,654✔
244
            while (true)
55,608,842✔
245
            {
55,608,842✔
246
                if (this.TryGet(key, out var value))
55,608,842✔
247
                {
1,735,045✔
248
                    return value;
1,735,045✔
249
                }
250

251
                // The value factory may be called concurrently for the same key, but the first write to the dictionary wins.
252
                value = valueFactory(key, factoryArgument);
53,873,797✔
253

254
                if (TryAdd(key, value))
53,873,797✔
255
                {
53,355,609✔
256
                    return value;
53,355,609✔
257
                }
258
            }
518,188✔
259
        }
55,090,654✔
260

261
        ///<inheritdoc/>
262
        public async ValueTask<V> GetOrAddAsync(K key, Func<K, Task<V>> valueFactory)
263
        {
28,000,544✔
264
            while (true)
28,457,330✔
265
            {
28,457,330✔
266
                if (this.TryGet(key, out var value))
28,457,330✔
267
                {
1,376,636✔
268
                    return value;
1,376,636✔
269
                }
270

271
                // The value factory may be called concurrently for the same key, but the first write to the dictionary wins.
272
                // This is identical logic in ConcurrentDictionary.GetOrAdd method.
273
                value = await valueFactory(key).ConfigureAwait(false);
27,080,694✔
274

275
                if (TryAdd(key, value))
27,080,673✔
276
                {
26,623,887✔
277
                    return value;
26,623,887✔
278
                }
279
            }
456,786✔
280
        }
28,000,523✔
281

282
        /// <summary>
283
        /// Adds a key/value pair to the cache if the key does not already exist. Returns the new value, or the 
284
        /// existing value if the key already exists.
285
        /// </summary>
286
        /// <typeparam name="TArg">The type of an argument to pass into valueFactory.</typeparam>
287
        /// <param name="key">The key of the element to add.</param>
288
        /// <param name="valueFactory">The factory function used to asynchronously generate a value for the key.</param>
289
        /// <param name="factoryArgument">An argument value to pass into valueFactory.</param>
290
        /// <returns>A task that represents the asynchronous GetOrAdd operation.</returns>
291
        public async ValueTask<V> GetOrAddAsync<TArg>(K key, Func<K, TArg, Task<V>> valueFactory, TArg factoryArgument)
292
        {
28,000,687✔
293
            while (true)
28,476,963✔
294
            {
28,476,963✔
295
                if (this.TryGet(key, out var value))
28,476,963✔
296
                {
1,423,464✔
297
                    return value;
1,423,464✔
298
                }
299

300
                // The value factory may be called concurrently for the same key, but the first write to the dictionary wins.
301
                value = await valueFactory(key, factoryArgument).ConfigureAwait(false);
27,053,499✔
302

303
                if (TryAdd(key, value))
27,053,499✔
304
                {
26,577,223✔
305
                    return value;
26,577,223✔
306
                }
307
            }
476,276✔
308
        }
28,000,687✔
309

310
        /// <summary>
311
        /// Attempts to remove the specified key value pair.
312
        /// </summary>
313
        /// <param name="item">The item to remove.</param>
314
        /// <returns>true if the item was removed successfully; otherwise, false.</returns>
315
        public bool TryRemove(KeyValuePair<K, V> item)
316
        {
33,045,491✔
317
            if (this.dictionary.TryGetValue(item.Key, out var existing))
33,045,491✔
318
            {
3,303,225✔
319
                lock (existing)
3,303,225✔
320
                {
3,303,225✔
321
                    if (EqualityComparer<V>.Default.Equals(existing.Value, item.Value))
3,303,225✔
322
                    {
293,955✔
323
                        var kvp = new KeyValuePair<K, I>(item.Key, existing);
293,955✔
324
#if NET6_0_OR_GREATER
325
                    if (this.dictionary.TryRemove(kvp))
209,855✔
326
#else
327
                        // https://devblogs.microsoft.com/pfxteam/little-known-gems-atomic-conditional-removals-from-concurrentdictionary/
328
                        if (((ICollection<KeyValuePair<K, I>>)this.dictionary).Remove(kvp))
84,100✔
329
#endif
330
                        {
286,067✔
331
                            OnRemove(item.Key, kvp.Value, ItemRemovedReason.Removed);
286,067✔
332
                            return true;
286,067✔
333
                        }
334
                    }
7,888✔
335
                }
3,017,158✔
336

337
                // it existed, but we couldn't remove - this means value was replaced afer the TryGetValue (a race)
338
            }
3,017,158✔
339

340
            return false;
32,759,424✔
341
        }
33,045,491✔
342

343
        /// <summary>
344
        /// Attempts to remove and return the value that has the specified key.
345
        /// </summary>
346
        /// <param name="key">The key of the element to remove.</param>
347
        /// <param name="value">When this method returns, contains the object removed, or the default value of the value type if key does not exist.</param>
348
        /// <returns>true if the object was removed successfully; otherwise, false.</returns>
349
        public bool TryRemove(K key, [MaybeNullWhen(false)] out V value)
350
        {
28,000,418✔
351
            if (this.dictionary.TryRemove(key, out var item))
28,000,418✔
352
            {
60,895✔
353
                OnRemove(key, item, ItemRemovedReason.Removed);
60,895✔
354
                value = item.Value;
60,895✔
355
                return true;
60,895✔
356
            }
357

358
            value = default;
27,939,523✔
359
            return false;
27,939,523✔
360
        }
28,000,418✔
361

362
        ///<inheritdoc/>
363
        public bool TryRemove(K key)
364
        {
28,000,308✔
365
            return TryRemove(key, out _);
28,000,308✔
366
        }
28,000,308✔
367

368
        [MethodImpl(MethodImplOptions.AggressiveInlining)]
369
        private void OnRemove(K key, I item, ItemRemovedReason reason)
370
        {
492,103,402✔
371
            // Mark as not accessed, it will later be cycled out of the queues because it can never be fetched 
372
            // from the dictionary. Note: Hot/Warm/Cold count will reflect the removed item until it is cycled 
373
            // from the queue.
374
            item.WasAccessed = false;
492,103,402✔
375
            item.WasRemoved = true;
492,103,402✔
376

377
            this.telemetryPolicy.OnItemRemoved(key, item.Value, reason);
492,103,402✔
378

379
            // serialize dispose (common case dispose not thread safe)
380
            lock (item)
492,103,402✔
381
            {
492,103,402✔
382
                Disposer<V>.Dispose(item.Value);
492,103,402✔
383
            }
492,103,402✔
384
        }
492,103,402✔
385

386
        ///<inheritdoc/>
387
        ///<remarks>Note: Calling this method does not affect LRU order.</remarks>
388
        public bool TryUpdate(K key, V value)
389
        {
88,376,423✔
390
            if (this.dictionary.TryGetValue(key, out var existing))
88,376,423✔
391
            {
7,933,469✔
392
                return this.TryUpdateValue(existing, value);
7,933,469✔
393
            }
394

395
            return false;
80,442,954✔
396
        }
88,376,423✔
397

398
        [MethodImpl(MethodImplOptions.AggressiveInlining)]
399
        private bool TryUpdateValue(I existing, V value)
400
        {
7,933,511✔
401
            lock (existing)
7,933,511✔
402
            {
7,933,511✔
403
                if (!existing.WasRemoved)
7,933,511✔
404
                {
7,688,583✔
405
                    V oldValue = existing.Value;
7,688,583✔
406

407
                    existing.Value = value;
7,688,583✔
408

409
                    this.itemPolicy.Update(existing);
7,688,583✔
410
                    // backcompat: remove conditional compile
411
#if NETCOREAPP3_0_OR_GREATER
412
                    this.telemetryPolicy.OnItemUpdated(existing.Key, oldValue, existing.Value);
7,688,583✔
413
#endif
414
                    Disposer<V>.Dispose(oldValue);
7,688,583✔
415

416
                    return true;
7,688,583✔
417
                }
418
            }
244,928✔
419

420
            return false;
244,928✔
421
        }
7,933,511✔
422

423
        ///<inheritdoc/>
424
        ///<remarks>Note: Updates to existing items do not affect LRU order. Added items are at the top of the LRU.</remarks>
425
        public void AddOrUpdate(K key, V value)
426
        {
32,163,249✔
427
            while (true)
32,376,248✔
428
            {
32,376,248✔
429
                // first, try to update
430
                if (this.TryUpdate(key, value))
32,376,248✔
431
                {
6,122,998✔
432
                    return;
6,122,998✔
433
                }
434

435
                // then try add
436
                var newItem = this.itemPolicy.CreateItem(key, value);
26,253,250✔
437

438
                if (this.dictionary.TryAdd(key, newItem))
26,253,250✔
439
                {
26,040,251✔
440
                    this.hotQueue.Enqueue(newItem);
26,040,251✔
441
                    Cycle(Interlocked.Increment(ref counter.hot));
26,040,251✔
442
                    return;
26,040,251✔
443
                }
444

445
                // if both update and add failed there was a race, try again
446
            }
212,999✔
447
        }
32,163,249✔
448

449
        ///<inheritdoc/>
450
        public void Clear()
451
        {
2,187,724✔
452
            // don't overlap Clear/Trim/TrimExpired
453
            lock (this.dictionary)
2,187,724✔
454
            {
2,187,724✔
455
                // evaluate queue count, remove everything including items removed from the dictionary but
456
                // not the queues. This also avoids the expensive o(n) no lock count, or locking the dictionary.
457
                int queueCount = this.HotCount + this.WarmCount + this.ColdCount;
2,187,724✔
458
                this.TrimLiveItems(itemsRemoved: 0, queueCount, ItemRemovedReason.Cleared);
2,187,724✔
459
            }
2,187,724✔
460
        }
2,187,724✔
461

462
        /// <summary>
463
        /// Trim the specified number of items from the cache. Removes all discardable items per IItemPolicy.ShouldDiscard(), then 
464
        /// itemCount-discarded items in LRU order, if any.
465
        /// </summary>
466
        /// <param name="itemCount">The number of items to remove.</param>
467
        /// <returns>The number of items removed from the cache.</returns>
468
        /// <exception cref="ArgumentOutOfRangeException"><paramref name="itemCount"/> is less than 0./</exception>
469
        /// <exception cref="ArgumentOutOfRangeException"><paramref name="itemCount"/> is greater than capacity./</exception>
470
        /// <remarks>
471
        /// Note: Trim affects LRU order. Calling Trim resets the internal accessed status of items.
472
        /// </remarks>
473
        public void Trim(int itemCount)
474
        {
427✔
475
            int capacity = this.Capacity;
427✔
476

477
            if (itemCount < 1 || itemCount > capacity)
427✔
478
                Throw.ArgOutOfRange(nameof(itemCount), "itemCount must be greater than or equal to one, and less than the capacity of the cache.");
14✔
479

480
            // clamp itemCount to number of items actually in the cache
481
            itemCount = Math.Min(itemCount, this.HotCount + this.WarmCount + this.ColdCount);
413✔
482

483
            // don't overlap Clear/Trim/TrimExpired
484
            lock (this.dictionary)
413✔
485
            {
413✔
486
                // first scan each queue for discardable items and remove them immediately. Note this can remove > itemCount items.
487
                int itemsRemoved = TrimAllDiscardedItems();
413✔
488

489
                TrimLiveItems(itemsRemoved, itemCount, ItemRemovedReason.Trimmed);
413✔
490
            }
413✔
491
        }
413✔
492

493
        private void TrimExpired()
494
        {
63✔
495
            if (this.itemPolicy.CanDiscard())
63✔
496
            {
63✔
497
                lock (this.dictionary)
63✔
498
                {
63✔
499
                    this.TrimAllDiscardedItems();
63✔
500
                }
63✔
501
            }
63✔
502
        }
63✔
503

504
        /// <summary>
505
        /// Trim discarded items from all queues.
506
        /// </summary>
507
        /// <returns>The number of items removed.</returns>
508
        // backcompat: make internal
509
        protected int TrimAllDiscardedItems()
510
        {
476✔
511
            // don't overlap Clear/Trim/TrimExpired
512
            lock (this.dictionary)
476✔
513
            {
476✔
514
                int RemoveDiscardableItems(ConcurrentQueue<I> q, ref int queueCounter)
515
                {
1,428✔
516
                    int itemsRemoved = 0;
1,428✔
517
                    int localCount = queueCounter;
1,428✔
518

519
                    for (int i = 0; i < localCount; i++)
8,708✔
520
                    {
2,926✔
521
                        if (q.TryDequeue(out var item))
2,926✔
522
                        {
2,926✔
523
                            if (this.itemPolicy.ShouldDiscard(item))
2,926✔
524
                            {
399✔
525
                                Interlocked.Decrement(ref queueCounter);
399✔
526
                                this.Move(item, ItemDestination.Remove, ItemRemovedReason.Trimmed);
399✔
527
                                itemsRemoved++;
399✔
528
                            }
399✔
529
                            else if (item.WasRemoved)
2,527✔
530
                            {
42✔
531
                                Interlocked.Decrement(ref queueCounter);
42✔
532
                            }
42✔
533
                            else
534
                            {
2,485✔
535
                                q.Enqueue(item);
2,485✔
536
                            }
2,485✔
537
                        }
2,926✔
538
                    }
2,926✔
539

540
                    return itemsRemoved;
1,428✔
541
                }
1,428✔
542

543
                int coldRem = RemoveDiscardableItems(coldQueue, ref this.counter.cold);
476✔
544
                int warmRem = RemoveDiscardableItems(warmQueue, ref this.counter.warm);
476✔
545
                int hotRem = RemoveDiscardableItems(hotQueue, ref this.counter.hot);
476✔
546

547
                if (warmRem > 0)
476✔
548
                {
56✔
549
                    Volatile.Write(ref this.isWarm, false);
56✔
550
                }
56✔
551

552
                return coldRem + warmRem + hotRem;
476✔
553
            }
554
        }
476✔
555

556
        private void TrimLiveItems(int itemsRemoved, int itemCount, ItemRemovedReason reason)
557
        {
2,188,137✔
558
            // When items are touched, they are moved to warm by cycling. Therefore, to guarantee 
559
            // that we can remove itemCount items, we must cycle (2 * capacity.Warm) + capacity.Hot times.
560
            // If clear is called during trimming, it would be possible to get stuck in an infinite
561
            // loop here. The warm + hot limit also guards against this case.
562
            int trimWarmAttempts = 0;
2,188,137✔
563
            int maxWarmHotAttempts = (this.capacity.Warm * 2) + this.capacity.Hot;
2,188,137✔
564

565
            while (itemsRemoved < itemCount && trimWarmAttempts < maxWarmHotAttempts)
22,214,400✔
566
            {
20,026,263✔
567
                if (Volatile.Read(ref this.counter.cold) > 0)
20,026,263✔
568
                {
15,437,000✔
569
                    if (TryRemoveCold(reason) == (ItemDestination.Remove, 0))
15,437,000✔
570
                    {
15,436,041✔
571
                        itemsRemoved++;
15,436,041✔
572
                        trimWarmAttempts = 0;
15,436,041✔
573
                    }
15,436,041✔
574
                    else
575
                    {
959✔
576
                        TrimWarmOrHot(reason);
959✔
577
                    }
959✔
578
                }
15,437,000✔
579
                else
580
                {
4,589,263✔
581
                    TrimWarmOrHot(reason);
4,589,263✔
582
                    trimWarmAttempts++;
4,589,263✔
583
                }
4,589,263✔
584
            }
20,026,263✔
585

586
            if (Volatile.Read(ref this.counter.warm) < this.capacity.Warm)
2,188,137✔
587
            {
1,222,919✔
588
                Volatile.Write(ref this.isWarm, false);
1,222,919✔
589
            }
1,222,919✔
590
        }
2,188,137✔
591

592
        private void TrimWarmOrHot(ItemRemovedReason reason)
593
        {
4,590,222✔
594
            if (Volatile.Read(ref this.counter.warm) > 0)
4,590,222✔
595
            {
1,523,292✔
596
                CycleWarmUnchecked(reason);
1,523,292✔
597
            }
1,523,292✔
598
            else if (Volatile.Read(ref this.counter.hot) > 0)
3,066,930✔
599
            {
3,066,344✔
600
                CycleHotUnchecked(reason);
3,066,344✔
601
            }
3,066,344✔
602
        }
4,590,222✔
603

604
        private void Cycle(int hotCount)
605
        {
492,152,433✔
606
            if (isWarm)
492,152,433✔
607
            {
486,917,290✔
608
                (var dest, var count) = CycleHot(hotCount);
486,917,290✔
609

610
                int cycles = 0;
486,917,290✔
611
                while (cycles++ < 3 && dest != ItemDestination.Remove)
1,053,805,069✔
612
                {
566,887,779✔
613
                    if (dest == ItemDestination.Warm)
566,887,779✔
614
                    {
75,559,936✔
615
                        (dest, count) = CycleWarm(count);
75,559,936✔
616
                    }
75,559,936✔
617
                    else if (dest == ItemDestination.Cold)
491,327,843✔
618
                    {
491,327,843✔
619
                        (dest, count) = CycleCold(count);
491,327,843✔
620
                    }
491,327,843✔
621
                }
566,887,779✔
622

623
                // If nothing was removed yet, constrain the size of warm and cold by discarding the coldest item.
624
                if (dest != ItemDestination.Remove)
486,917,290✔
625
                {
13,271,085✔
626
                    if (dest == ItemDestination.Warm && count > this.capacity.Warm)
13,271,085✔
627
                    {
8,141,779✔
628
                        count = LastWarmToCold();
8,141,779✔
629
                    }
8,141,779✔
630

631
                    ConstrainCold(count, ItemRemovedReason.Evicted);
13,271,085✔
632
                }
13,271,085✔
633
            }
486,917,290✔
634
            else
635
            {
5,235,143✔
636
                // fill up the warm queue with new items until warm is full.
637
                // else during warmup the cache will only use the hot + cold queues until any item is requested twice.
638
                CycleDuringWarmup(hotCount);
5,235,143✔
639
            }
5,235,143✔
640
        }
492,152,433✔
641

642
        [MethodImpl(MethodImplOptions.NoInlining)]
643
        private void CycleDuringWarmup(int hotCount)
644
        {
5,235,143✔
645
            // do nothing until hot is full
646
            if (hotCount > this.capacity.Hot)
5,235,143✔
647
            {
2,157,694✔
648
                Interlocked.Decrement(ref this.counter.hot);
2,157,694✔
649

650
                if (this.hotQueue.TryDequeue(out var item))
2,157,694✔
651
                {
2,157,693✔
652
                    // special case: removed during warmup
653
                    if (item.WasRemoved)
2,157,693✔
654
                    {
244,888✔
655
                        return;
244,888✔
656
                    }
657

658
                    int count = this.Move(item, ItemDestination.Warm, ItemRemovedReason.Evicted);
1,912,805✔
659

660
                    // if warm is now full, overflow to cold and mark as warm
661
                    if (count > this.capacity.Warm)
1,912,805✔
662
                    {
414,890✔
663
                        Volatile.Write(ref this.isWarm, true);
414,890✔
664
                        count = LastWarmToCold();
414,890✔
665
                        ConstrainCold(count, ItemRemovedReason.Evicted);
414,890✔
666
                    }
414,890✔
667
                }
1,912,805✔
668
                else
669
                {
1✔
670
                    Interlocked.Increment(ref this.counter.hot);
1✔
671
                }
1✔
672
            }
1,912,806✔
673
        }
5,235,143✔
674

675
        private (ItemDestination, int) CycleHot(int hotCount)
676
        {
486,917,290✔
677
            if (hotCount > this.capacity.Hot)
486,917,290✔
678
            {
486,915,730✔
679
                return CycleHotUnchecked(ItemRemovedReason.Evicted);
486,915,730✔
680
            }
681

682
            return (ItemDestination.Remove, 0);
1,560✔
683
        }
486,917,290✔
684

685
        [MethodImpl(MethodImplOptions.AggressiveInlining)]
686
        private (ItemDestination, int) CycleHotUnchecked(ItemRemovedReason removedReason)
687
        {
489,982,074✔
688
            Interlocked.Decrement(ref this.counter.hot);
489,982,074✔
689

690
            if (this.hotQueue.TryDequeue(out var item))
489,982,074✔
691
            {
489,982,063✔
692
                var where = this.itemPolicy.RouteHot(item);
489,982,063✔
693
                return (where, this.Move(item, where, removedReason));
489,982,063✔
694
            }
695
            else
696
            {
11✔
697
                Interlocked.Increment(ref this.counter.hot);
11✔
698
                return (ItemDestination.Remove, 0);
11✔
699
            }
700
        }
489,982,074✔
701

702
        private (ItemDestination, int) CycleWarm(int count)
703
        {
75,559,936✔
704
            if (count > this.capacity.Warm)
75,559,936✔
705
            {
75,513,805✔
706
                return CycleWarmUnchecked(ItemRemovedReason.Evicted);
75,513,805✔
707
            }
708

709
            return (ItemDestination.Remove, 0);
46,131✔
710
        }
75,559,936✔
711

712
        [MethodImpl(MethodImplOptions.AggressiveInlining)]
713
        private (ItemDestination, int) CycleWarmUnchecked(ItemRemovedReason removedReason)
714
        {
77,037,097✔
715
            int wc = Interlocked.Decrement(ref this.counter.warm);
77,037,097✔
716

717
            if (this.warmQueue.TryDequeue(out var item))
77,037,097✔
718
            {
77,037,095✔
719
                if (item.WasRemoved)
77,037,095✔
720
                {
1,126,347✔
721
                    return (ItemDestination.Remove, 0);
1,126,347✔
722
                }
723

724
                var where = this.itemPolicy.RouteWarm(item);
75,910,748✔
725

726
                // When the warm queue is full, we allow an overflow of 1 item before redirecting warm items to cold.
727
                // This only happens when hit rate is high, in which case we can consider all items relatively equal in
728
                // terms of which was least recently used.
729
                if (where == ItemDestination.Warm && wc <= this.capacity.Warm)
75,910,748✔
730
                {
16,874,989✔
731
                    return (ItemDestination.Warm, this.Move(item, where, removedReason));
16,874,989✔
732
                }
733
                else
734
                {
59,035,759✔
735
                    return (ItemDestination.Cold, this.Move(item, ItemDestination.Cold, removedReason));
59,035,759✔
736
                }
737
            }
738
            else
739
            {
2✔
740
                Interlocked.Increment(ref this.counter.warm);
2✔
741
                return (ItemDestination.Remove, 0);
2✔
742
            }
743
        }
77,037,097✔
744

745
        private (ItemDestination, int) CycleCold(int count)
746
        {
491,327,843✔
747
            if (count > this.capacity.Cold)
491,327,843✔
748
            {
480,746,403✔
749
                return TryRemoveCold(ItemRemovedReason.Evicted);
480,746,403✔
750
            }
751

752
            return (ItemDestination.Remove, 0);
10,581,440✔
753
        }
491,327,843✔
754

755
        [MethodImpl(MethodImplOptions.AggressiveInlining)]
756
        private (ItemDestination, int) TryRemoveCold(ItemRemovedReason removedReason)
757
        {
496,183,403✔
758
            Interlocked.Decrement(ref this.counter.cold);
496,183,403✔
759

760
            if (this.coldQueue.TryDequeue(out var item))
496,183,403✔
761
            {
496,183,052✔
762
                var where = this.itemPolicy.RouteCold(item);
496,183,052✔
763

764
                if (where == ItemDestination.Warm && Volatile.Read(ref this.counter.warm) <= this.capacity.Warm)
496,183,052✔
765
                {
20,007,572✔
766
                    return (ItemDestination.Warm, this.Move(item, where, removedReason));
20,007,572✔
767
                }
768
                else
769
                {
476,175,480✔
770
                    this.Move(item, ItemDestination.Remove, removedReason);
476,175,480✔
771
                    return (ItemDestination.Remove, 0);
476,175,480✔
772
                }
773
            }
774
            else
775
            {
351✔
776
                return (ItemDestination.Cold, Interlocked.Increment(ref this.counter.cold));
351✔
777
            }
778
        }
496,183,403✔
779

780
        [MethodImpl(MethodImplOptions.AggressiveInlining)]
781
        private int LastWarmToCold()
782
        {
8,556,669✔
783
            Interlocked.Decrement(ref this.counter.warm);
8,556,669✔
784

785
            if (this.warmQueue.TryDequeue(out var item))
8,556,669!
786
            {
8,556,669✔
787
                var destination = item.WasRemoved ? ItemDestination.Remove : ItemDestination.Cold;
8,556,669✔
788
                return this.Move(item, destination, ItemRemovedReason.Evicted);
8,556,669✔
789
            }
790
            else
791
            {
×
792
                Interlocked.Increment(ref this.counter.warm);
×
793
                return 0;
×
794
            }
795
        }
8,556,669✔
796

797
        [MethodImpl(MethodImplOptions.AggressiveInlining)]
798
        private void ConstrainCold(int coldCount, ItemRemovedReason removedReason)
799
        {
13,685,975✔
800
            if (coldCount > this.capacity.Cold && this.coldQueue.TryDequeue(out var item))
13,685,975✔
801
            {
13,231,371✔
802
                Interlocked.Decrement(ref this.counter.cold);
13,231,371✔
803
                this.Move(item, ItemDestination.Remove, removedReason);
13,231,371✔
804
            }
13,231,371✔
805
        }
13,685,975✔
806

807
        [MethodImpl(MethodImplOptions.AggressiveInlining)]
808
        private int Move(I item, ItemDestination where, ItemRemovedReason removedReason)
809
        {
1,092,110,564✔
810
            item.WasAccessed = false;
1,092,110,564✔
811

812
            switch (where)
1,092,110,564✔
813
            {
814
                case ItemDestination.Warm:
815
                    this.warmQueue.Enqueue(item);
85,620,952✔
816
                    return Interlocked.Increment(ref this.counter.warm);
85,620,952✔
817
                case ItemDestination.Cold:
818
                    this.coldQueue.Enqueue(item);
509,424,746✔
819
                    return Interlocked.Increment(ref this.counter.cold);
509,424,746✔
820
                case ItemDestination.Remove:
821

822
                    var kvp = new KeyValuePair<K, I>(item.Key, item);
497,064,866✔
823

824
#if NET6_0_OR_GREATER
825
                    if (this.dictionary.TryRemove(kvp))
442,506,517✔
826
#else
827
                    // https://devblogs.microsoft.com/pfxteam/little-known-gems-atomic-conditional-removals-from-concurrentdictionary/
828
                    if (((ICollection<KeyValuePair<K, I>>)this.dictionary).Remove(kvp))
54,558,349✔
829
#endif
830
                    {
491,739,942✔
831
                        OnRemove(item.Key, item, removedReason);
491,739,942✔
832
                    }
491,739,942✔
833
                    break;
497,064,866✔
834
            }
835

836
            return 0;
497,064,866✔
837
        }
1,092,110,564✔
838

839
        /// <summary>Returns an enumerator that iterates through the cache.</summary>
840
        /// <returns>An enumerator for the cache.</returns>
841
        /// <remarks>
842
        /// The enumerator returned from the cache is safe to use concurrently with
843
        /// reads and writes, however it does not represent a moment-in-time snapshot.  
844
        /// The contents exposed through the enumerator may contain modifications
845
        /// made after <see cref="GetEnumerator"/> was called.
846
        /// </remarks>
847
        IEnumerator IEnumerable.GetEnumerator()
848
        {
21✔
849
            return ((ConcurrentLruCore<K, V, I, P, T>)this).GetEnumerator();
21✔
850
        }
21✔
851

852
#if DEBUG
853
        /// <summary>
854
        /// Format the LRU as a string by converting all the keys to strings.
855
        /// </summary>
856
        /// <returns>The LRU formatted as a string.</returns>
857
        internal string FormatLruString()
858
        {
105✔
859
            var sb = new System.Text.StringBuilder();
105✔
860

861
            sb.Append("Hot [");
105✔
862
            sb.Append(string.Join(",", this.hotQueue.Select(n => n.Key.ToString())));
392✔
863
            sb.Append("] Warm [");
105✔
864
            sb.Append(string.Join(",", this.warmQueue.Select(n => n.Key.ToString())));
350✔
865
            sb.Append("] Cold [");
105✔
866
            sb.Append(string.Join(",", this.coldQueue.Select(n => n.Key.ToString())));
315✔
867
            sb.Append(']');
105✔
868

869
            return sb.ToString();
105✔
870
        }
105✔
871
#endif
872

873
        private static CachePolicy CreatePolicy(ConcurrentLruCore<K, V, I, P, T> lru)
874
        {
483✔
875
            var p = new Proxy(lru);
483✔
876

877
            if (typeof(P) == typeof(AfterAccessPolicy<K, V>))
483✔
878
            {
77✔
879
                return new CachePolicy(new Optional<IBoundedPolicy>(p), Optional<ITimePolicy>.None(), new Optional<ITimePolicy>(p), Optional<IDiscreteTimePolicy>.None());
77✔
880
            }
881

882
            // IsAssignableFrom is a jit intrinsic https://github.com/dotnet/runtime/issues/4920
883
            if (typeof(IDiscreteItemPolicy<K, V>).IsAssignableFrom(typeof(P)))
406✔
884
            {
105✔
885
                return new CachePolicy(new Optional<IBoundedPolicy>(p), Optional<ITimePolicy>.None(), Optional<ITimePolicy>.None(), new Optional<IDiscreteTimePolicy>(new DiscreteExpiryProxy(lru)));
105✔
886
            }
887

888
            return new CachePolicy(new Optional<IBoundedPolicy>(p), lru.itemPolicy.CanDiscard() ? new Optional<ITimePolicy>(p) : Optional<ITimePolicy>.None());
301✔
889
        }
483✔
890

891
        private static Optional<ICacheMetrics> CreateMetrics(ConcurrentLruCore<K, V, I, P, T> lru)
892
        {
231✔
893
            if (typeof(T) == typeof(NoTelemetryPolicy<K, V>))
231✔
894
            {
35✔
895
                return Optional<ICacheMetrics>.None();
35✔
896
            }
897

898
            return new(new Proxy(lru));
196✔
899
        }
231✔
900

901
        private static Optional<ICacheEvents<K, V>> CreateEvents(ConcurrentLruCore<K, V, I, P, T> lru)
902
        {
2,624✔
903
            if (typeof(T) == typeof(NoTelemetryPolicy<K, V>))
2,624✔
904
            {
91✔
905
                return Optional<ICacheEvents<K, V>>.None();
91✔
906
            }
907

908
            return new(new Proxy(lru));
2,533✔
909
        }
2,624✔
910

911
#if NET9_0_OR_GREATER
912
        ///<inheritdoc/>
913
        [MethodImpl(MethodImplOptions.AggressiveInlining)]
914
        public IAlternateLookup<TAlternateKey, K, V> GetAlternateLookup<TAlternateKey>()
915
            where TAlternateKey : notnull, allows ref struct
916
        {
267✔
917
            if (!this.dictionary.IsCompatibleKey<TAlternateKey, K, I>())
267✔
918
            {
18✔
919
                Throw.IncompatibleComparer();
18✔
920
            }
921

922
            return new AlternateLookup<TAlternateKey>(this);
249✔
923
        }
249✔
924

925
        ///<inheritdoc/>
926
        public bool TryGetAlternateLookup<TAlternateKey>([MaybeNullWhen(false)] out IAlternateLookup<TAlternateKey, K, V> lookup)
927
            where TAlternateKey : notnull, allows ref struct
928
        {
36✔
929
            if (this.dictionary.IsCompatibleKey<TAlternateKey, K, I>())
36✔
930
            {
18✔
931
                lookup = new AlternateLookup<TAlternateKey>(this);
18✔
932
                return true;
18✔
933
            }
934

935
            lookup = default;
18✔
936
            return false;
18✔
937
        }
36✔
938

939
        ///<inheritdoc/>
940
        [MethodImpl(MethodImplOptions.AggressiveInlining)]
941
        public IAsyncAlternateLookup<TAlternateKey, K, V> GetAsyncAlternateLookup<TAlternateKey>()
942
            where TAlternateKey : notnull, allows ref struct
943
        {
51✔
944
            if (!this.dictionary.IsCompatibleKey<TAlternateKey, K, I>())
51✔
945
            {
6✔
946
                Throw.IncompatibleComparer();
6✔
947
            }
948

949
            return new AlternateLookup<TAlternateKey>(this);
45✔
950
        }
45✔
951

952
        ///<inheritdoc/>
953
        public bool TryGetAsyncAlternateLookup<TAlternateKey>([MaybeNullWhen(false)] out IAsyncAlternateLookup<TAlternateKey, K, V> lookup)
954
            where TAlternateKey : notnull, allows ref struct
955
        {
12✔
956
            if (this.dictionary.IsCompatibleKey<TAlternateKey, K, I>())
12✔
957
            {
6✔
958
                lookup = new AlternateLookup<TAlternateKey>(this);
6✔
959
                return true;
6✔
960
            }
961

962
            lookup = default;
6✔
963
            return false;
6✔
964
        }
12✔
965

966
        internal readonly struct AlternateLookup<TAlternateKey> : IAlternateLookup<TAlternateKey, K, V>, IAsyncAlternateLookup<TAlternateKey, K, V>
967
            where TAlternateKey : notnull, allows ref struct
968
        {
969
            internal AlternateLookup(ConcurrentLruCore<K, V, I, P, T> lru)
970
            {
318✔
971
                Debug.Assert(lru is not null);
318✔
972
                Debug.Assert(lru.dictionary.IsCompatibleKey<TAlternateKey, K, I>());
318✔
973
                this.Lru = lru;
318✔
974
                this.Alternate = lru.dictionary.GetAlternateLookup<TAlternateKey>();
318✔
975
                this.Comparer = lru.dictionary.GetAlternateComparer<TAlternateKey, K, I>();
318✔
976
            }
318✔
977

978
            internal ConcurrentLruCore<K, V, I, P, T> Lru { get; }
254,642,507✔
979

980
            internal ConcurrentDictionary<K, I>.AlternateLookup<TAlternateKey> Alternate { get; }
149,683,092✔
981

982
            internal IAlternateEqualityComparer<TAlternateKey, K> Comparer { get; }
116,507,676✔
983

984
            public bool TryGet(TAlternateKey key, [MaybeNullWhen(false)] out V value)
985
            {
137,682,951✔
986
                if (this.Alternate.TryGetValue(key, out var item))
137,682,951✔
987
                {
21,175,284✔
988
                    return this.Lru.GetOrDiscard(item, out value);
21,175,284✔
989
                }
990

991
                value = default;
116,507,667✔
992
                this.Lru.telemetryPolicy.IncrementMiss();
116,507,667✔
993
                return false;
116,507,667✔
994
            }
137,682,951✔
995

996
            public bool TryRemove(TAlternateKey key, [MaybeNullWhen(false)] out K actualKey, [MaybeNullWhen(false)] out V value)
997
            {
12,000,051✔
998
                if (this.Alternate.TryRemove(key, out actualKey, out var item))
12,000,051✔
999
                {
16,498✔
1000
                    this.Lru.OnRemove(actualKey, item, ItemRemovedReason.Removed);
16,498✔
1001
                    value = item.Value;
16,498✔
1002
                    return true;
16,498✔
1003
                }
1004

1005
                actualKey = default;
11,983,553✔
1006
                value = default;
11,983,553✔
1007
                return false;
11,983,553✔
1008
            }
12,000,051✔
1009

1010
            public bool TryUpdate(TAlternateKey key, V value)
1011
            {
90✔
1012
                if (this.Alternate.TryGetValue(key, out var existing))
90✔
1013
                {
42✔
1014
                    return this.Lru.TryUpdateValue(existing, value);
42✔
1015
                }
1016

1017
                return false;
48✔
1018
            }
90✔
1019

1020
            public void AddOrUpdate(TAlternateKey key, V value)
1021
            {
48✔
1022
                K actualKey = default!;
48✔
1023
                bool hasActualKey = false;
48✔
1024

1025
                while (true)
48✔
1026
                {
48✔
1027
                    if (this.TryUpdate(key, value))
48✔
1028
                    {
21✔
1029
                        return;
21✔
1030
                    }
1031

1032
                    if (!hasActualKey)
27✔
1033
                    {
27✔
1034
                        actualKey = this.Comparer.Create(key);
27✔
1035
                        hasActualKey = true;
27✔
1036
                    }
27✔
1037

1038
                    if (this.Lru.TryAdd(actualKey, value))
27!
1039
                    {
27✔
1040
                        return;
27✔
1041
                    }
1042
                }
1043
            }
48✔
1044

1045
            public V GetOrAdd(TAlternateKey key, Func<K, V> valueFactory)
1046
            {
86,363,510✔
1047
                while (true)
87,637,259✔
1048
                {
87,637,259✔
1049
                    if (this.TryGet(key, out var value))
87,637,259✔
1050
                    {
19,832,126✔
1051
                        return value;
19,832,126✔
1052
                    }
1053

1054
                    K actualKey = this.Comparer.Create(key);
67,805,133✔
1055

1056
                    value = valueFactory(actualKey);
67,805,133✔
1057
                    if (this.Lru.TryAdd(actualKey, value))
67,805,133✔
1058
                    {
66,531,384✔
1059
                        return value;
66,531,384✔
1060
                    }
1061
                }
1,273,749✔
1062
            }
86,363,510✔
1063

1064
            public V GetOrAdd<TArg>(TAlternateKey key, Func<K, TArg, V> valueFactory, TArg factoryArgument)
1065
                where TArg : allows ref struct
1066
            {
25,815,310✔
1067
                while (true)
26,045,578✔
1068
                {
26,045,578✔
1069
                    if (this.TryGet(key, out var value))
26,045,578✔
1070
                    {
703,586✔
1071
                        return value;
703,586✔
1072
                    }
1073

1074
                    K actualKey = this.Comparer.Create(key);
25,341,992✔
1075

1076
                    value = valueFactory(actualKey, factoryArgument);
25,341,992✔
1077
                    if (this.Lru.TryAdd(actualKey, value))
25,341,992✔
1078
                    {
25,111,724✔
1079
                        return value;
25,111,724✔
1080
                    }
1081
                }
230,268✔
1082
            }
25,815,310✔
1083

1084
            public ValueTask<V> GetOrAddAsync(TAlternateKey key, Func<K, Task<V>> valueFactory)
1085
            {
12,000,006✔
1086
                if (this.TryGet(key, out var value))
12,000,006✔
1087
                {
370,796✔
1088
                    return new ValueTask<V>(value);
370,796✔
1089
                }
1090

1091
                K actualKey = this.Comparer.Create(key);
11,629,210✔
1092
                Task<V> task = valueFactory(actualKey);
11,629,210✔
1093

1094
                return GetOrAddAsyncSlow(actualKey, task);
11,629,210✔
1095
            }
12,000,006✔
1096

1097
            public ValueTask<V> GetOrAddAsync<TArg>(TAlternateKey key, Func<K, TArg, Task<V>> valueFactory, TArg factoryArgument)
1098
            {
12,000,006✔
1099
                if (this.TryGet(key, out var value))
12,000,006✔
1100
                {
268,692✔
1101
                    return new ValueTask<V>(value);
268,692✔
1102
                }
1103

1104
                K actualKey = this.Comparer.Create(key);
11,731,314✔
1105
                Task<V> task = valueFactory(actualKey, factoryArgument);
11,731,314✔
1106

1107
                return GetOrAddAsyncSlow(actualKey, task);
11,731,314✔
1108
            }
12,000,006✔
1109

1110
            // Since TAlternateKey can be a ref struct, we can't use async/await in the public GetOrAddAsync methods,
1111
            // so we delegate to this private async method after the value factory is invoked.
1112
            private async ValueTask<V> GetOrAddAsyncSlow(K actualKey, Task<V> task)
1113
            {
23,360,524✔
1114
                V value = await task.ConfigureAwait(false);
23,360,524✔
1115

1116
                while (true)
23,360,556✔
1117
                {
23,360,556✔
1118
                    if (this.Lru.TryAdd(actualKey, value))
23,360,556✔
1119
                    {
22,925,248✔
1120
                        return value;
22,925,248✔
1121
                    }
1122

1123
                    // Another thread added a value for this key first, retrieve it.
1124
                    if (this.Lru.TryGet(actualKey, out V? existing))
435,308✔
1125
                    {
435,276✔
1126
                        return existing;
435,276✔
1127
                    }
1128
                }
32✔
1129
            }
23,360,524✔
1130
        }
1131
#endif
1132

1133
        // To get JIT optimizations, policies must be structs.
1134
        // If the structs are returned directly via properties, they will be copied. Since  
1135
        // telemetryPolicy is a mutable struct, copy is bad. One workaround is to store the 
1136
        // state within the struct in an object. Since the struct points to the same object
1137
        // it becomes immutable. However, this object is then somewhere else on the 
1138
        // heap, which slows down the policies with hit counter logic in benchmarks. Likely
1139
        // this approach keeps the structs data members in the same CPU cache line as the LRU.
1140
        // backcompat: remove conditional compile
1141
#if NETCOREAPP3_0_OR_GREATER
1142
        [DebuggerDisplay("Hit = {Hits}, Miss = {Misses}, Upd = {Updated}, Evict = {Evicted}")]
1143
#else
1144
        [DebuggerDisplay("Hit = {Hits}, Miss = {Misses}, Evict = {Evicted}")]
1145
#endif
1146
        private class Proxy : ICacheMetrics, ICacheEvents<K, V>, IBoundedPolicy, ITimePolicy
1147
        {
1148
            private readonly ConcurrentLruCore<K, V, I, P, T> lru;
1149

1150
            public Proxy(ConcurrentLruCore<K, V, I, P, T> lru)
3,212✔
1151
            {
3,212✔
1152
                this.lru = lru;
3,212✔
1153
            }
3,212✔
1154

1155
            public double HitRatio => lru.telemetryPolicy.HitRatio;
14✔
1156

1157
            public long Total => lru.telemetryPolicy.Total;
7✔
1158

1159
            public long Hits => lru.telemetryPolicy.Hits;
42✔
1160

1161
            public long Misses => lru.telemetryPolicy.Misses;
42✔
1162

1163
            public long Evicted => lru.telemetryPolicy.Evicted;
56✔
1164

1165
            // backcompat: remove conditional compile
1166
#if NETCOREAPP3_0_OR_GREATER
1167
            public long Updated => lru.telemetryPolicy.Updated;
14✔
1168
#endif
1169
            public int Capacity => lru.Capacity;
189✔
1170

1171
            public TimeSpan TimeToLive => lru.itemPolicy.TimeToLive;
28✔
1172

1173
            public event EventHandler<ItemRemovedEventArgs<K, V>> ItemRemoved
1174
            {
1175
                add { this.lru.telemetryPolicy.ItemRemoved += value; }
315✔
1176
                remove { this.lru.telemetryPolicy.ItemRemoved -= value; }
42✔
1177
            }
1178

1179
            // backcompat: remove conditional compile
1180
#if NETCOREAPP3_0_OR_GREATER
1181
            public event EventHandler<ItemUpdatedEventArgs<K, V>> ItemUpdated
1182
            {
1183
                add { this.lru.telemetryPolicy.ItemUpdated += value; }
186✔
1184
                remove { this.lru.telemetryPolicy.ItemUpdated -= value; }
21✔
1185
            }
1186
#endif
1187
            public void Trim(int itemCount)
1188
            {
70✔
1189
                lru.Trim(itemCount);
70✔
1190
            }
70✔
1191

1192
            public void TrimExpired()
1193
            {
49✔
1194
                lru.TrimExpired();
49✔
1195
            }
49✔
1196
        }
1197

1198
        private class DiscreteExpiryProxy : IDiscreteTimePolicy
1199
        {
1200
            private readonly ConcurrentLruCore<K, V, I, P, T> lru;
1201

1202
            public DiscreteExpiryProxy(ConcurrentLruCore<K, V, I, P, T> lru)
105✔
1203
            {
105✔
1204
                this.lru = lru;
105✔
1205
            }
105✔
1206

1207
            public void TrimExpired()
1208
            {
14✔
1209
                lru.TrimExpired();
14✔
1210
            }
14✔
1211

1212
            public bool TryGetTimeToExpire<TKey>(TKey key, out TimeSpan timeToLive)
1213
            {
21✔
1214
                if (key is K k && lru.dictionary.TryGetValue(k, out var item))
21✔
1215
                {
7✔
1216
                    LongTickCountLruItem<K, V>? tickItem = item as LongTickCountLruItem<K, V>;
7✔
1217
                    timeToLive = (new Duration(tickItem!.TickCount) - Duration.SinceEpoch()).ToTimeSpan();
7✔
1218
                    return true;
7✔
1219
                }
1220

1221
                timeToLive = default;
14✔
1222
                return false;
14✔
1223
            }
21✔
1224
        }
1225
    }
1226
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc