AutoCache 3.2.0

.NET Standard 2.1
dotnet add package AutoCache --version 3.2.0
NuGet\Install-Package AutoCache -Version 3.2.0
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="AutoCache" Version="3.2.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add AutoCache --version 3.2.0
#r "nuget: AutoCache, 3.2.0"
#r directive can be used in F# Interactive, C# scripting and .NET Interactive. Copy this into the interactive tool or source code of the script to reference the package.
// Install AutoCache as a Cake Addin
#addin nuget:?package=AutoCache&version=3.2.0

// Install AutoCache as a Cake Tool
#tool nuget:?package=AutoCache&version=3.2.0

Problem and the solution

Caching patterns

When you are caching data from a resource, there are caching patterns that you can implement, including proactive and reactive approaches. Two common approaches are cache-aside or lazy loading (a reactive approach) and write-through (a proactive approach). A cache-aside cache is updated after the data is requested. A write-through cache is updated immediately when the primary database is updated.

Cache-Aside (Lazy Loading) Disadvantage

Cache misses often cause many requests to be referred to the resource, simultaneously until the data is cached again. It can reduce system performance and functionality.


Why AutoCache?

With cache coalescing and using a two-level response, there are no real cache misses. It is a cache-aside approache, but in practice, it works similar to the write-through method and the cache is updated before the next request.

I am currently using this library for a heavy-load application. This program receives more than 30 million requests per day and handles them with redis using AutoCache.

How it works?

AutoCache adds a "refresh" time to each key. When it's time to refresh a key, the cache update starts with the first incoming request. All requests receive the response without waiting for the update.

The "refresh" and "expiration" times get updated after each refresh.


Depending on the type of business, by choosing a long time for expiration and a short time for refreshing, it avoided cache misses and consecutive waits.


On the cache key missing, only the first request will fire the cache update task. All other requests wait for the result to be ready.




First, install NuGet. Then, install AutoCache from the package manager console:

PM> Install-Package AutoCache

How do I get started?

"CacheAdapter" class implements "ICacheAdapter" interface and has tree abstract methods. You must implement them to have the fourth method.

public interface ICacheAdapter
    public abstract Task SetAsync<T>(string key, T value, TimeSpan expireAt);
    public abstract Task<(T, bool)> GetAsync<T>(string key);
    public abstract Task RemoveAsync(string key);

    public Task<T> GetOrCreateAsync<T>(string key,
        Func<Task<(T, bool)>> resourceFetch,
        TimeSpan? refreshAt = null,
        TimeSpan? expireAt = null,
        TimeSpan? timeout = null);

First create your cache adapter:

public interface IMyCacheAdapter: ICacheAdapter{}
public class MyCacheAdapter : CacheAdapter,IMyCacheAdapter{
    // Override abstract methods

Then inject your adapter in ConfigureServices:

services.AddSingleton<IMyCacheAdapter>(provider =>
    new MyCacheAdapter(
        TimeSpan.FromMinutes(2), // DefaultRefreshAt
        TimeSpan.FromHours(1), //DefaultExpiredAt
        TimeSpan.FromSeconds(30) //DefaultSourceFetchTimeout

Now you can use it:

public interface IToDoService
    Task<int> GetAsync();

public class ToDoService: IToDoService
    public virtual async Task<int> GetAsync() {
        // read from resource (database,api, etc.), service or resource ...
        throw new NotImplementedException();

public class CachedTodoService:ToDoService
    private readonly IMyCacheAdapter _cache;
    public CachedTodoService(IMyCacheAdapter cache) => _cache = cache;

    public override async Task<int> GetAsync() =>
        await _cache.GetOrCreateAsync<int>("todo_service_cache_key",
            async () =>
                    var value = await toDoService.GetAsync();
                    return (value, true);
                catch (Exception)
                    return (0, false);


Learn about the latest improvements.

Product Versions
.NET net5.0 net5.0-windows net6.0 net6.0-android net6.0-ios net6.0-maccatalyst net6.0-macos net6.0-tvos net6.0-windows net7.0 net7.0-android net7.0-ios net7.0-maccatalyst net7.0-macos net7.0-tvos net7.0-windows
.NET Core netcoreapp3.0 netcoreapp3.1
.NET Standard netstandard2.1
MonoAndroid monoandroid
MonoMac monomac
MonoTouch monotouch
Tizen tizen60
Xamarin.iOS xamarinios
Xamarin.Mac xamarinmac
Xamarin.TVOS xamarintvos
Xamarin.WatchOS xamarinwatchos
Compatible target framework(s)
Additional computed target framework(s)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
3.2.0 149 11/22/2022
3.2.0-alpha 59 11/19/2022
3.1.0 216 10/15/2022
3.0.1 204 10/14/2022
3.0.0 225 8/23/2022
3.0.0-alpha 79 8/11/2022
2.0.0 246 6/28/2022
2.0.0-alpha 92 6/26/2022
1.0.4 254 3/17/2022
1.0.3 260 3/12/2022
1.0.3-alpha 84 3/12/2022
1.0.2-alpha 84 3/12/2022
1.0.1-alpha 81 3/10/2022
1.0.0 234 3/12/2022
1.0.0-alpha 80 3/10/2022