AutoCache 1.0.1-alpha

This is a prerelease version of AutoCache.
There is a newer version of this package available.
See the version list below for details.
dotnet add package AutoCache --version 1.0.1-alpha                
NuGet\Install-Package AutoCache -Version 1.0.1-alpha                
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="1.0.1-alpha" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add AutoCache --version 1.0.1-alpha                
#r "nuget: AutoCache, 1.0.1-alpha"                
#r directive can be used in F# Interactive and Polyglot Notebooks. 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=1.0.1-alpha&prerelease

// Install AutoCache as a Cake Tool
#tool nuget:?package=AutoCache&version=1.0.1-alpha&prerelease                

Why AutoCache?

Cache misses often causes a large number of requests being referred to the database at the same time, until the data is cached again. This can reduce system performance and functionality.

How it works?

With AutoCache, outdated cache keys will remain alive until they are expired. Suppose hundreds of requests arived at same time, looking for an outdated cache item. Instead of referring them to the database, all requests will get outdated data from cache and cache update task will be triggered (The database is called only once to update the cache).

With the cache key data, the expire (ttl) and outdate time of cache key, updated too.

Installation

NuGet

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

PM> Install-Package AutoCache

How do I get started?

public abstract class CacheAdapter
{
    public abstract Task RemoveAsync(string key);
    public abstract Task SetAsync<T>(string key, T value, DateTime expireAt);
    public abstract Task<(T, bool)> GetAsync<T>(string key);
}

First create an adapter for your caching service (or database), by inheriting from the "BaseCache" abstract class and implement abstract methods.

public class MyCacheAdapter : CacheAdapter{
    ...
}

Then instantiate your cache adapter:

ICacheAdapter cache = new MyCacheAdapter(
        serviceScopeFactory, //IServiceScopeFactory
        60000, //defaulOutdatedAtMiliSecond
        3600000); //defaultExpireAtMiliSecond

You can inject it in ConfigureServices:

services.AddSingleton<ICacheAdapter, cache>(); // your cache adapter

Now you can use it:

public interface IToDoService
{
    Task<int> GetAsync();
}

public class ToDoService: IToDoService
{
    public virtual async Task<int> GetAsync() {
        // read from DB
        throw new NotImplementedException();
    };
}

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

    public override async Task<int> GetAsync() =>
        await _cache.GetOrCreateAsync<int, IToDoService>("todo_service_cache_key",
            async (toDoService, updateIsInProgress) =>
            {
                try
                {
                    var value = await toDoService.GetAsync();
                    return (value, true);
                }
                catch (Exception ex)
                {
                    return (0, false);
                }
            });
}
Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
.NET Core netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.1 is compatible. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
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.2-alpha 171 5/10/2023
3.2.1 274 3/29/2023
3.2.0 359 11/22/2022
3.2.0-alpha 160 11/19/2022
3.1.0 428 10/15/2022
3.0.1 406 10/14/2022
3.0.0 418 8/23/2022
3.0.0-alpha 197 8/11/2022
2.0.0 466 6/28/2022
2.0.0-alpha 202 6/26/2022
1.0.4 463 3/17/2022
1.0.3 464 3/12/2022
1.0.3-alpha 188 3/12/2022
1.0.2-alpha 212 3/12/2022
1.0.1-alpha 207 3/10/2022
1.0.0 430 3/12/2022
1.0.0-alpha 199 3/10/2022