Skip to content

Conversation

@bitfaster
Copy link
Owner

@bitfaster bitfaster commented Jul 9, 2022

Atomic builders do not support mixing sync and async (at least not in a way that would be safe if the caller mixes sync/async calls). Therefore, segregate the factory methods on ICache and IScopedCache so that sync and async calls cannot be mixed:

interface ICache { V GetOrAdd(K key, Func<K, V> valueFactory); }
interface IAsyncCache { Task<V> GetOrAddAsync(K key, Func<K, Task<V>> valueFactory); }
interface IScopedCache { Lifetime<V> ScopedGetOrAdd(K key, Func<K, Scoped<V>> valueFactory); }
interface IScopedAsyncCache { Task<Lifetime<V>> ScopedGetOrAddAsync(K key, Func<K, Task<Scoped<V>>> valueFactory); }

Extend LRU builder to support all combinations of:

  • Metrics
  • Expire after write (TLru)
  • Scoped IDisposable values
  • Synchronized factory method
  • Async factory method

Use of builder now looks like this (methods can be called in any order with the same result):

var cache = new LruBuilder<int, Disposable>()
    .WithCapacity(666)
    .WithKeyComparer(EqualityComparer<int>.Default)
    .WithConcurrencyLevel(4)
    .WithAtomicCreate()
    .WithMetrics()
    .WithScopedValues()
    .AsAsyncCache()
    .Build();

@bitfaster bitfaster mentioned this pull request Jul 20, 2022
@bitfaster bitfaster changed the title Users/alexpeck/builder builder redux Jul 20, 2022
@bitfaster bitfaster changed the title builder redux build async+atomic+scoped Jul 24, 2022
@bitfaster bitfaster marked this pull request as ready for review July 24, 2022 02:24
@bitfaster bitfaster merged commit 7bdd0e8 into main Jul 24, 2022
@bitfaster bitfaster deleted the users/alexpeck/builder branch July 24, 2022 03:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants