Skip to content

Commit 0748cf7

Browse files
authored
getoradd (#167)
1 parent 32b6835 commit 0748cf7

File tree

4 files changed

+29
-25
lines changed

4 files changed

+29
-25
lines changed

BitFaster.Caching.Benchmarks/Lru/LruAsyncGet.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public class LruAsyncGet
2020
{
2121
// if the cache value is a value type, value task has no effect - so use string to repro.
2222
private static readonly IAsyncCache<int, string> concurrentLru = new ConcurrentLruBuilder<int, string>().AsAsyncCache().Build();
23-
private static readonly IAsyncCache<int, string> atomicConcurrentLru = new ConcurrentLruBuilder<int, string>().AsAsyncCache().WithAtomicValueFactory().Build();
23+
private static readonly IAsyncCache<int, string> atomicConcurrentLru = new ConcurrentLruBuilder<int, string>().AsAsyncCache().WithAtomicGetOrAdd().Build();
2424

2525
private static Task<string> returnTask = Task.FromResult("1");
2626

BitFaster.Caching.Benchmarks/Lru/LruJustGetOrAdd.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public class LruJustGetOrAdd
4444
private static readonly FastConcurrentLru<int, int> fastConcurrentLru = new FastConcurrentLru<int, int>(8, 9, EqualityComparer<int>.Default);
4545
private static readonly FastConcurrentTLru<int, int> fastConcurrentTLru = new FastConcurrentTLru<int, int>(8, 9, EqualityComparer<int>.Default, TimeSpan.FromMinutes(1));
4646

47-
private static readonly ICache<int, int> atomicFastLru = new ConcurrentLruBuilder<int, int>().WithConcurrencyLevel(8).WithCapacity(9).WithAtomicValueFactory().Build();
47+
private static readonly ICache<int, int> atomicFastLru = new ConcurrentLruBuilder<int, int>().WithConcurrencyLevel(8).WithCapacity(9).WithAtomicGetOrAdd().Build();
4848

4949
private static readonly int key = 1;
5050
private static System.Runtime.Caching.MemoryCache memoryCache = System.Runtime.Caching.MemoryCache.Default;

BitFaster.Caching.UnitTests/Lru/ConcurrentLruBuilderTests.cs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ public void WithScopedValues()
209209
public void WithAtomicFactory()
210210
{
211211
ICache<int, int> lru = new ConcurrentLruBuilder<int, int>()
212-
.WithAtomicValueFactory()
212+
.WithAtomicGetOrAdd()
213213
.WithCapacity(3)
214214
.Build();
215215

@@ -233,7 +233,7 @@ public void AsAsync()
233233
public void WithAtomicWithScope()
234234
{
235235
IScopedCache<int, Disposable> lru = new ConcurrentLruBuilder<int, Disposable>()
236-
.WithAtomicValueFactory()
236+
.WithAtomicGetOrAdd()
237237
.AsScopedCache()
238238
.WithCapacity(3)
239239
.Build();
@@ -248,7 +248,7 @@ public void WithScopedWithAtomic()
248248
{
249249
IScopedCache<int, Disposable> lru = new ConcurrentLruBuilder<int, Disposable>()
250250
.AsScopedCache()
251-
.WithAtomicValueFactory()
251+
.WithAtomicGetOrAdd()
252252
.WithCapacity(3)
253253
.Build();
254254

@@ -290,7 +290,7 @@ public void WithScopedAsAsync()
290290
public void WithAtomicAsAsync()
291291
{
292292
IAsyncCache<int, int> lru = new ConcurrentLruBuilder<int, int>()
293-
.WithAtomicValueFactory()
293+
.WithAtomicGetOrAdd()
294294
.AsAsyncCache()
295295
.WithCapacity(3)
296296
.Build();
@@ -304,7 +304,7 @@ public void AsAsyncWithAtomic()
304304
{
305305
IAsyncCache<int, int> lru = new ConcurrentLruBuilder<int, int>()
306306
.AsAsyncCache()
307-
.WithAtomicValueFactory()
307+
.WithAtomicGetOrAdd()
308308
.WithCapacity(3)
309309
.Build();
310310

@@ -316,7 +316,7 @@ public void AsAsyncWithAtomic()
316316
public void WithAtomicWithScopedAsAsync()
317317
{
318318
IScopedAsyncCache<int, Disposable> lru = new ConcurrentLruBuilder<int, Disposable>()
319-
.WithAtomicValueFactory()
319+
.WithAtomicGetOrAdd()
320320
.AsScopedCache()
321321
.AsAsyncCache()
322322
.WithCapacity(3)
@@ -330,7 +330,7 @@ public void WithAtomicWithScopedAsAsync()
330330
public void WithAtomicAsAsyncWithScoped()
331331
{
332332
IScopedAsyncCache<int, Disposable> lru = new ConcurrentLruBuilder<int, Disposable>()
333-
.WithAtomicValueFactory()
333+
.WithAtomicGetOrAdd()
334334
.AsAsyncCache()
335335
.AsScopedCache()
336336
.WithCapacity(3)
@@ -345,7 +345,7 @@ public void WithScopedWithAtomicAsAsync()
345345
{
346346
IScopedAsyncCache<int, Disposable> lru = new ConcurrentLruBuilder<int, Disposable>()
347347
.AsScopedCache()
348-
.WithAtomicValueFactory()
348+
.WithAtomicGetOrAdd()
349349
.AsAsyncCache()
350350
.WithCapacity(3)
351351
.Build();
@@ -360,7 +360,7 @@ public void WithScopedAsAsyncWithAtomic()
360360
IScopedAsyncCache<int, Disposable> lru = new ConcurrentLruBuilder<int, Disposable>()
361361
.AsScopedCache()
362362
.AsAsyncCache()
363-
.WithAtomicValueFactory()
363+
.WithAtomicGetOrAdd()
364364
.WithCapacity(3)
365365
.Build();
366366

@@ -374,7 +374,7 @@ public void AsAsyncWithScopedWithAtomic()
374374
IScopedAsyncCache<int, Disposable> lru = new ConcurrentLruBuilder<int, Disposable>()
375375
.AsAsyncCache()
376376
.AsScopedCache()
377-
.WithAtomicValueFactory()
377+
.WithAtomicGetOrAdd()
378378
.WithCapacity(3)
379379
.Build();
380380

@@ -387,7 +387,7 @@ public void AsAsyncWithAtomicWithScoped()
387387
{
388388
IScopedAsyncCache<int, Disposable> lru = new ConcurrentLruBuilder<int, Disposable>()
389389
.AsAsyncCache()
390-
.WithAtomicValueFactory()
390+
.WithAtomicGetOrAdd()
391391
.AsScopedCache()
392392
.WithCapacity(3)
393393
.Build();

BitFaster.Caching/Lru/ConcurrentLruBuilderExtensions.cs

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -67,57 +67,61 @@ public static AtomicScopedAsyncConcurrentLruBuilder<K, V> AsScopedCache<K, V>(th
6767
}
6868

6969
/// <summary>
70-
/// Execute the cache's GetOrAdd value factory atomically, such that it is applied at most once per key. Other threads
71-
/// attempting to update the same key will be blocked until value factory completes.
70+
/// Execute the cache's GetOrAdd method atomically, such that it is applied at most once per key. Other threads
71+
/// attempting to update the same key will be blocked until value factory completes. Incurs a small performance
72+
/// penalty.
7273
/// </summary>
7374
/// <typeparam name="K">The type of keys in the cache.</typeparam>
7475
/// <typeparam name="V">The type of values in the cache.</typeparam>
7576
/// <param name="builder">The ConcurrentLruBuilder to chain method calls onto.</param>
7677
/// <returns>An AtomicConcurrentLruBuilder.</returns>
77-
public static AtomicConcurrentLruBuilder<K, V> WithAtomicValueFactory<K, V>(this ConcurrentLruBuilder<K, V> builder)
78+
public static AtomicConcurrentLruBuilder<K, V> WithAtomicGetOrAdd<K, V>(this ConcurrentLruBuilder<K, V> builder)
7879
{
7980
var convertBuilder = new ConcurrentLruBuilder<K, AtomicFactory<K, V>>(builder.info);
8081
return new AtomicConcurrentLruBuilder<K, V>(convertBuilder);
8182
}
8283

8384
/// <summary>
84-
/// Execute the cache's ScopedGetOrAdd value factory atomically, such that it is applied at most once per key. Other threads
85-
/// attempting to update the same key will be blocked until value factory completes.
85+
/// Execute the cache's GetOrAdd method atomically, such that it is applied at most once per key. Other threads
86+
/// attempting to update the same key will be blocked until value factory completes. Incurs a small performance
87+
/// penalty.
8688
/// </summary>
8789
/// <typeparam name="K">The type of keys in the cache.</typeparam>
8890
/// <typeparam name="V">The type of values in the cache.</typeparam>
8991
/// <typeparam name="W">The wrapped value type.</typeparam>
9092
/// <param name="builder">The ScopedConcurrentLruBuilder to chain method calls onto.</param>
9193
/// <returns>An AtomicScopedConcurrentLruBuilder.</returns>
92-
public static AtomicScopedConcurrentLruBuilder<K, V> WithAtomicValueFactory<K, V, W>(this ScopedConcurrentLruBuilder<K, V, W> builder) where V : IDisposable where W : IScoped<V>
94+
public static AtomicScopedConcurrentLruBuilder<K, V> WithAtomicGetOrAdd<K, V, W>(this ScopedConcurrentLruBuilder<K, V, W> builder) where V : IDisposable where W : IScoped<V>
9395
{
9496
var convertBuilder = new ConcurrentLruBuilder<K, ScopedAtomicFactory<K, V>>(builder.info);
9597
return new AtomicScopedConcurrentLruBuilder<K, V>(convertBuilder);
9698
}
9799

98100
/// <summary>
99-
/// Execute the cache's GetOrAddAsync value factory atomically, such that it is applied at most once per key. Other threads
100-
/// attempting to update the same key will wait on the same value factory task.
101+
/// Execute the cache's GetOrAdd method atomically, such that it is applied at most once per key. Other threads
102+
/// attempting to update the same key will be blocked until value factory completes. Incurs a small performance
103+
/// penalty.
101104
/// </summary>
102105
/// <typeparam name="K">The type of keys in the cache.</typeparam>
103106
/// <typeparam name="V">The type of values in the cache.</typeparam>
104107
/// <param name="builder">The AsyncConcurrentLruBuilder to chain method calls onto.</param>
105108
/// <returns>An AtomicAsyncConcurrentLruBuilder.</returns>
106-
public static AtomicAsyncConcurrentLruBuilder<K, V> WithAtomicValueFactory<K, V>(this AsyncConcurrentLruBuilder<K, V> builder)
109+
public static AtomicAsyncConcurrentLruBuilder<K, V> WithAtomicGetOrAdd<K, V>(this AsyncConcurrentLruBuilder<K, V> builder)
107110
{
108111
var convertBuilder = new ConcurrentLruBuilder<K, AsyncAtomicFactory<K, V>>(builder.info);
109112
return new AtomicAsyncConcurrentLruBuilder<K, V>(convertBuilder);
110113
}
111114

112115
/// <summary>
113-
/// Execute the cache's ScopedGetOrAddAsync value factory atomically, such that it is applied at most once per key. Other threads
114-
/// attempting to update the same key will wait on the same value factory task.
116+
/// Execute the cache's GetOrAdd method atomically, such that it is applied at most once per key. Other threads
117+
/// attempting to update the same key will be blocked until value factory completes. Incurs a small performance
118+
/// penalty.
115119
/// </summary>
116120
/// <typeparam name="K">The type of keys in the cache.</typeparam>
117121
/// <typeparam name="V">The type of values in the cache.</typeparam>
118122
/// <param name="builder">The ScopedAsyncConcurrentLruBuilder to chain method calls onto.</param>
119123
/// <returns>An AtomicScopedAsyncConcurrentLruBuilder.</returns>
120-
public static AtomicScopedAsyncConcurrentLruBuilder<K, V> WithAtomicValueFactory<K, V>(this ScopedAsyncConcurrentLruBuilder<K, V> builder) where V : IDisposable
124+
public static AtomicScopedAsyncConcurrentLruBuilder<K, V> WithAtomicGetOrAdd<K, V>(this ScopedAsyncConcurrentLruBuilder<K, V> builder) where V : IDisposable
121125
{
122126
var convertBuilder = new AsyncConcurrentLruBuilder<K, ScopedAsyncAtomicFactory<K, V>>(builder.info);
123127
return new AtomicScopedAsyncConcurrentLruBuilder<K, V>(convertBuilder);

0 commit comments

Comments
 (0)