CoreApp.DefensiveCache
2.0.0
dotnet add package CoreApp.DefensiveCache --version 2.0.0
NuGet\Install-Package CoreApp.DefensiveCache -Version 2.0.0
<PackageReference Include="CoreApp.DefensiveCache" Version="2.0.0" />
<PackageVersion Include="CoreApp.DefensiveCache" Version="2.0.0" />
<PackageReference Include="CoreApp.DefensiveCache" />
paket add CoreApp.DefensiveCache --version 2.0.0
#r "nuget: CoreApp.DefensiveCache, 2.0.0"
#addin nuget:?package=CoreApp.DefensiveCache&version=2.0.0
#tool nuget:?package=CoreApp.DefensiveCache&version=2.0.0
DefensiveCache
É uma biblioteca para geração de implementação de cache baseado em interfaces. Sua missão é reduzir aquele bom e velho trecho de código replicado:
public async Task<Entity> GetObjectAsync(int id)
{
var value = await _distributedCache.GetAsync<Entity>("GetObjectAsync" + id);
if(value == null)
{
value = await _entityRepository.GetObjectAsync(id);
var cacheEntryOptions = new DistributedCacheEntryOptions();
cacheEntryOptions.SetAbsoluteExpiration(TimeSpan.FromSeconds(1000));
await _distributedCache.SetAsync("GetObjectAsync" + id, value, cacheEntryOptions);
}
return value;
}
Funcionamento
O DefensiveCache utiliza Reflection para mapear a interface do serviço concreto, a partir dessa estrutura é realizado a compilação de uma classe concreta utilizando o compilador Roslyn, a implementação gerada é baseado design pattern Decorator, configurada por injeção de dependência utilizando o Scrutor. Os métodos que não possuirem cache, serão direcionados diretamente para implementação concreta.
Importante
O DefensiveCache está preparado para mapear interfaces que tenham apenas métodos, interfaces com qualquer propriedade NÃO serão mapeadas nessa versão.
Como usar
No Startup da aplicação, realizamos a configuração do serviço de serialização de cache ICacheSerializer e o mapeamento dos métodos de cache. Utilize {chaves} para realizar a interpolação de valores na criação das chaves de cache.
public void ConfigureServices(IServiceCollection services)
{
services.AddDistributedMemoryCache();
services.AddScoped<ICacheSerializer, JsonNetCacheSerializer>();
services.DecorateWithCacheGenerated<EnitityRepository>((config) =>
{
config.AddMethod(x => nameof(x.GetObjectAsync), "GetObjectAsync{id}", 60);
});
}
Extensibilidade
No momento a biblioteca possui suporte para dois tipos de serialização de cache, para implementar serializadores personalizados, basta implementar a interface ICacheSerializer.
- BinaryNetCacheSerializer - Text/JsonSerializer
- JsonNetCacheSerializer - Binary/BinaryFormatter
Benchmark
BenchmarkDotNet=v0.12.1, OS=Windows 10.0.18362.959 (1903/May2019Update/19H1)
Intel Core i7-8565U CPU 1.80GHz (Whiskey Lake), 1 CPU, 8 logical and 4 physical cores
.NET Core SDK=3.1.302
[Host] : .NET Core 3.1.6 (CoreCLR 4.700.20.26901, CoreFX 4.700.20.31603), X64 RyuJIT
DefaultJob : .NET Core 3.1.6 (CoreCLR 4.700.20.26901, CoreFX 4.700.20.31603), X64 RyuJIT
Method | Mean | Error | StdDev | Completed Work Items | Lock Contentions | Gen 0 | Gen 1 | Gen 2 | Allocated | Allocated native memory | Native memory leak |
---|---|---|---|---|---|---|---|---|---|---|---|
ProcessDefensiveCacheImplemented | 140.24 μs | 2.799 μs | 4.677 μs | 0.0005 | - | 28.0762 | - | - | 116.13 KB | - | - |
ProcessDefensiveCacheGenerated | 45.96 μs | 0.406 μs | 0.360 μs | 0.0001 | - | 4.8828 | - | - | 20.18 KB | - | - |
ProcessDefensiveCacheDynamicProxy | 730.32 μs | 5.215 μs | 4.878 μs | 0.0039 | - | 70.3125 | - | - | 293.76 KB | 0 KB | - |
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. net10.0 was computed. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
.NET Core | netcoreapp3.0 is compatible. netcoreapp3.1 was computed. |
-
.NETCoreApp 3.0
- CoreApp.DefensiveCache.Generator (>= 1.0.0)
- Microsoft.Extensions.Caching.Abstractions (>= 3.1.4)
- Microsoft.Extensions.Hosting (>= 3.0.0)
- Microsoft.Extensions.Logging.Abstractions (>= 3.0.0)
- Microsoft.IO.RecyclableMemoryStream (>= 1.3.3)
- Scrutor (>= 3.2.0)
- Stubble.Core (>= 1.8.4)
- System.Reflection.DispatchProxy (>= 4.7.1)
- System.Runtime.Serialization.Formatters (>= 4.3.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.