AutoCache 1.0.4
See the version list below for details.
dotnet add package AutoCache --version 1.0.4
NuGet\Install-Package AutoCache -Version 1.0.4
<PackageReference Include="AutoCache" Version="1.0.4" />
paket add AutoCache --version 1.0.4
#r "nuget: AutoCache, 1.0.4"
// Install AutoCache as a Cake Addin #addin nuget:?package=AutoCache&version=1.0.4 // Install AutoCache as a Cake Tool #tool nuget:?package=AutoCache&version=1.0.4
Why AutoCache?
Cache misses often causes a large number of requests being referred to the database, service or resource at the same time, until the data is cached again. This can reduce system performance and functionality.
With AutoCache, there is no real cache missess.
How it works?
Each cache keys have outdate and expir times. When the outdate time expires, the cache starts updating with the first request. But the request does not wait, and receives the outdated data.
Suppose hundreds of requests arived at same time, looking for an outdated cache item. Instead of referring all of them to the database, all requests will get outdated data from cache and the database is called only once (to update the cache).
Installation
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, TService>(string key,
Func<TService, bool, Task<(T, bool)>> DbFetch,
TimeSpan? outdatedAt = null,
TimeSpan? expireAt = null);
}
First create an adapter for your cache service (or database):
public interface IMyCacheAdapter: ICacheAdapter{}
public class MyCacheAdapter : CacheAdapter,IMyCacheAdapter{
// Override abstract methods
}
Then inject your adapter in ConfigureServices:
services.AddSingleton<IMyCacheAdapter>(provider =>
new MyCacheAdapter(
provider.GetService<IServiceScopeFactory>(),
TimeSpan.FromMinutes(2), // DefaultOutdatedAt
TimeSpan.FromHours(1) //DefaultExpiredAt
));
Now you can use it:
public interface IToDoService
{
Task<int> GetAsync();
}
public class ToDoService: IToDoService
{
public virtual async Task<int> GetAsync() {
// read from DB, 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, IToDoService>("todo_service_cache_key",
async (toDoService, isInUpdate) =>
{
try
{
var value = await toDoService.GetAsync();
return (value, true);
}
catch (Exception)
{
return (0, false);
}
});
}
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. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.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)
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 | 177 | 5/10/2023 |
3.2.1 | 287 | 3/29/2023 |
3.2.0 | 367 | 11/22/2022 |
3.2.0-alpha | 164 | 11/19/2022 |
3.1.0 | 436 | 10/15/2022 |
3.0.1 | 414 | 10/14/2022 |
3.0.0 | 426 | 8/23/2022 |
3.0.0-alpha | 201 | 8/11/2022 |
2.0.0 | 474 | 6/28/2022 |
2.0.0-alpha | 206 | 6/26/2022 |
1.0.4 | 471 | 3/17/2022 |
1.0.3 | 472 | 3/12/2022 |
1.0.3-alpha | 192 | 3/12/2022 |
1.0.2-alpha | 216 | 3/12/2022 |
1.0.1-alpha | 211 | 3/10/2022 |
1.0.0 | 438 | 3/12/2022 |
1.0.0-alpha | 203 | 3/10/2022 |