AutoCache 3.2.2-alpha
dotnet add package AutoCache --version 3.2.2-alpha
NuGet\Install-Package AutoCache -Version 3.2.2-alpha
<PackageReference Include="AutoCache" Version="3.2.2-alpha" />
paket add AutoCache --version 3.2.2-alpha
#r "nuget: AutoCache, 3.2.2-alpha"
// Install AutoCache as a Cake Addin #addin nuget:?package=AutoCache&version=3.2.2-alpha&prerelease // Install AutoCache as a Cake Tool #tool nuget:?package=AutoCache&version=3.2.2-alpha&prerelease
AutoCache
AutoCache is a .NET library that optimizes source caching in high traffic applications. It utilizes a cache-aside approach, but in practice, it works similar to a write-through method and updates the cache before the next request. This library also includes cache coalescing which eliminates real cache misses and improves system performance.
Problem
When caching data from a resource, there are two common approaches: cache-aside or lazy loading and write-through. Cache-aside or lazy loading is a reactive approach, where the cache is updated after the data is requested. Write-through is a proactive approach, where the cache is updated immediately when the primary database is updated.
The disadvantage of cache-aside is that cache misses often cause many requests to be referred to the resource simultaneously until the data is cached again, reducing system performance and functionality.
Solution
AutoCache solves this problem by adding 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 are updated after each refresh.
AutoCache also includes cache coalescing which means that on a cache key miss, only the first request will fire the cache update task. All other requests will wait for the result to be ready.
Usage
Installation
First, install NuGet. Then, install AutoCache from the package manager console:
PM> Install-Package AutoCache
Getting Started
Create your cache adapter by implementing the ICacheAdapter interface:
public interface IMyCacheAdapter: ICacheAdapter{} public class MyCacheAdapter : CacheAdapter,IMyCacheAdapter{ // Override abstract methods }
Inject your adapter in ConfigureServices:
services.AddSingleton<IMyCacheAdapter>(provider => new MyCacheAdapter( TimeSpan.FromMinutes(2), // DefaultRefreshAt TimeSpan.FromHours(1), //DefaultExpiredAt TimeSpan.FromSeconds(30) //DefaultSourceFetchTimeout ));
Use it in your services:
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 () => { try { var value = await toDoService.GetAsync(); return (value, true); } catch (Exception) { return (0, false); } }); }
Configuration
You can configure the default refreshAt, expireAt and timeout for the CacheAdapter and adjust them to your needs.
Tips
- Depending on your business, by choosing a long time for expiration and a short time for refreshing, you can avoid cache misses and consecutive waits.
- This library is currently being used in a heavy-load application that receives more than 30 million requests per day and handles them with redis using AutoCache.
Changelog
Product | Versions 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. |
-
.NETStandard 2.1
- Microsoft.AspNetCore.Http (>= 2.2.2)
- Microsoft.Extensions.Logging (>= 6.0.0)
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 |