Skip to content
Alex Peck edited this page Nov 25, 2023 · 11 revisions

BitFaster.Caching provides in memory caches that implement the following interfaces:

  • ICache
  • IAsyncCache
  • IScopedCache
  • IScopedAsyncCache

ICache provides an API surface that should be intuitive if you are familiar with ConcurrentDictionary, but with the addition of a cache policy, metrics and events. IAsyncCache is essentially the same, but with a GetOrAddAsync method in place of GetOrAdd. IAsyncCache is distinct to avoid the pitfalls of Sync over Async (deadlocks) and Async over Sync (scalability).

IScopedCache makes it easy to work with IDisposable values. In place of ICache.GetOrAdd it instead provides ScopedGetOrAdd which returns a lifetime rather than a value. Internally, ScopedGetOrAdd safely resolves races between concurrent reads and deletes. IScopedAsyncCache is the async equivalent.

The ConcurrentLru and ConcurrentLfu classes implement both ICache and IAsyncCache, since internally they do not mix sync and async logic. This gives the flexibility to mix sync and async, but only when it is safe to do so.

Use cache builders to instantiate scoped versions of ConcurrentLru and ConcurrentLfu, or to enable atomic value creation.