Ex2 (integrado): decorator de cache para IWorkshopRepository + TTL por Options

This commit is contained in:
Arthur Faria
2025-10-14 19:44:18 -03:00
parent 047c1a7ddb
commit fc6f5fd1de
8 changed files with 222 additions and 2 deletions

View File

@@ -0,0 +1,59 @@
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using CampusWorkshops.Api.Services;
namespace CampusWorkshops.Api.Controllers;
[ApiController]
[AllowAnonymous] // Deixe aberto para facilitar o teste
[Route("api/[controller]")]
public class DiDemoController : ControllerBase
{
// Visualiza os IDs por lifetime
[HttpGet("lifetimes")]
public IActionResult Lifetimes(
[FromServices] IRequestIdTransient t1,
[FromServices] IRequestIdTransient t2,
[FromServices] IRequestIdScoped s1,
[FromServices] IRequestIdScoped s2,
[FromServices] IRequestIdSingleton g1,
[FromServices] IRequestIdSingleton g2)
{
return Ok(new {
transient1 = t1.Id, transient2 = t2.Id, // normalmente diferentes NA MESMA chamada
scoped1 = s1.Id, scoped2 = s2.Id, // iguais NA MESMA chamada; mudam entre chamadas
singleton1 = g1.Id, singleton2 = g2.Id // sempre iguais
});
}
// Demonstra o "cativeiro" do transient dentro de um singleton
[HttpGet("captive-transient")]
public IActionResult Captive(
[FromServices] ReportingSingleton singleton,
[FromServices] IPerRequestClock clockTransient)
{
// clockTransient é Transient: CreatedAt muda entre requisições
var transientCreatedAtNow = clockTransient.CreatedAt;
// GetClockCreatedAt (TODO) deveria retornar o CreatedAt visto pelo singleton
DateTimeOffset? singletonClockCreatedAt = null;
string? error = null;
try
{
singletonClockCreatedAt = singleton.GetClockCreatedAt();
}
catch (Exception ex)
{
error = $"{ex.GetType().Name}: {ex.Message}";
}
return Ok(new {
transientCreatedAtNow,
singletonClockCreatedAt,
esperado = "Valores DIFERENTES por requisição",
observacao = "Mas como o transient foi resolvido DENTRO do singleton, ele congelou e não muda",
todoImplementado = error is null,
errorAoChamarSingleton = error
});
}
}

View File

@@ -8,6 +8,9 @@ using System.Text;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.IdentityModel.Tokens;
using Microsoft.OpenApi.Models;
using CampusWorkshops.Api.Services;
using Microsoft.Extensions.Options;
using Microsoft.Extensions.Caching.Memory;
var builder = WebApplication.CreateBuilder(args);
@@ -44,7 +47,32 @@ builder.Services.AddAuthorization(options =>
builder.Services.AddDbContext<WorkshopsDbContext>(options =>
options.UseSqlite(builder.Configuration.GetConnectionString("WorkshopsDb")));
builder.Services.AddScoped<IWorkshopRepository, EfWorkshopRepository>();
builder.Services.AddMemoryCache();
builder.Services.Configure<CacheSettings>(builder.Configuration.GetSection("Cache"));
// Primeiro registramos a implementação EF concreta
builder.Services.AddScoped<EfWorkshopRepository>();
// Depois expomos IWorkshopRepository como o "EF envelopado por cache"
builder.Services.AddScoped<IWorkshopRepository>(sp =>
new CachedWorkshopRepository(
sp.GetRequiredService<EfWorkshopRepository>(),
sp.GetRequiredService<IMemoryCache>(),
sp.GetRequiredService<IOptionsSnapshot<CacheSettings>>()));
// Lifetimes de exemplo
builder.Services.AddTransient<IRequestIdTransient, RequestIdTransient>();
builder.Services.AddScoped<IRequestIdScoped, RequestIdScoped>();
builder.Services.AddSingleton<IRequestIdSingleton, RequestIdSingleton>();
// Exemplo de "captive dependency"
builder.Services.AddTransient<IPerRequestClock, PerRequestClock>(); // Transient
builder.Services.AddSingleton<ReportingSingleton>();
// Removendo para ativar o cached repository
// builder.Services.AddScoped<IWorkshopRepository, EfWorkshopRepository>();
builder.Services.AddControllers();
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen(o =>

View File

@@ -0,0 +1,82 @@
using CampusWorkshops.Api.Models;
using Microsoft.Extensions.Caching.Memory;
using Microsoft.Extensions.Options;
namespace CampusWorkshops.Api.Repositories;
public sealed class CachedWorkshopRepository : IWorkshopRepository
{
private readonly IWorkshopRepository _inner;
private readonly IMemoryCache _cache;
private readonly TimeSpan _ttl;
public CachedWorkshopRepository(
IWorkshopRepository inner,
IMemoryCache cache,
IOptionsSnapshot<CampusWorkshops.Api.Services.CacheSettings> opts)
{
_inner = inner;
_cache = cache;
_ttl = TimeSpan.FromSeconds(opts.Value.DefaultTtlSeconds);
}
private static string KeyById(Guid id) => $"workshops:byId:{id}";
private static string KeyList(DateTimeOffset? from, DateTimeOffset? to, string? q)
=> $"workshops:list:{from?.ToUnixTimeSeconds()}:{to?.ToUnixTimeSeconds()}:{q?.Trim().ToLowerInvariant()}";
public async Task<IReadOnlyList<Workshop>> GetAllAsync(
DateTimeOffset? from, DateTimeOffset? to, string? q, CancellationToken ct)
{
// Implementação final
var key = KeyList(from, to, q);
if (_cache.TryGetValue(key, out IReadOnlyList<Workshop>? cached) && cached is not null)
return cached;
var data = await _inner.GetAllAsync(from, to, q, ct);
_cache.Set(key, data, _ttl);
return data;
}
public async Task<Workshop?> GetByIdAsync(Guid id, CancellationToken ct)
{
var key = KeyById(id);
if (_cache.TryGetValue(key, out Workshop? cached) && cached is not null)
return cached;
var w = await _inner.GetByIdAsync(id, ct);
if (w is not null) _cache.Set(key, w, _ttl);
return w;
}
// Ações de escrita: apenas encaminham e limpam entradas simples
public async Task<Workshop> AddAsync(Workshop workshop, CancellationToken ct)
{
var w = await _inner.AddAsync(workshop, ct);
_cache.Remove(KeyById(w.Id));
return w;
}
public async Task<Workshop?> UpdateAsync(Workshop workshop, CancellationToken ct)
{
var w = await _inner.UpdateAsync(workshop, ct);
if (w is not null) _cache.Remove(KeyById(w.Id));
return w;
}
public async Task<bool> DeleteAsync(Guid id, CancellationToken ct)
{
var ok = await _inner.DeleteAsync(id, ct);
if (ok) _cache.Remove(KeyById(id));
return ok;
}
public Task<bool> ExistsAsync(Guid id, CancellationToken ct)
=> _inner.ExistsAsync(id, ct);
public async Task<Workshop?> UpdatePartialAsync(Guid id, Action<Workshop> updateAction, CancellationToken ct)
{
var w = await _inner.UpdatePartialAsync(id, updateAction, ct);
if (w is not null) _cache.Remove(KeyById(w.Id));
return w;
}
}

View File

@@ -0,0 +1,6 @@
namespace CampusWorkshops.Api.Services;
public sealed class CacheSettings
{
public int DefaultTtlSeconds { get; set; } = 15;
}

View File

@@ -0,0 +1,12 @@
namespace CampusWorkshops.Api.Services;
public interface IPerRequestClock
{
// Carimbo criado no CONSTRUTOR (não muda depois)
DateTimeOffset CreatedAt { get; }
}
public sealed class PerRequestClock : IPerRequestClock
{
public DateTimeOffset CreatedAt { get; } = DateTimeOffset.UtcNow;
}

View File

@@ -0,0 +1,23 @@
using System;
namespace CampusWorkshops.Api.Services;
/// <summary>
/// Singleton que recebe um Transient (IPerRequestClock).
/// Na prática, o Transient é resolvido uma única vez, na construção do Singleton,
/// e "vira" efetivamente singleton dentro dele.
/// </summary>
public sealed class ReportingSingleton
{
private readonly IPerRequestClock _clock;
public ReportingSingleton(IPerRequestClock clock)
{
_clock = clock;
}
public DateTimeOffset GetClockCreatedAt()
{
return _clock.CreatedAt;
}
}

9
Services/RequestId.cs Normal file
View File

@@ -0,0 +1,9 @@
namespace CampusWorkshops.Api.Services;
public interface IRequestIdTransient { Guid Id { get; } }
public interface IRequestIdScoped { Guid Id { get; } }
public interface IRequestIdSingleton { Guid Id { get; } }
internal sealed class RequestIdTransient : IRequestIdTransient { public Guid Id { get; } = Guid.NewGuid(); }
internal sealed class RequestIdScoped : IRequestIdScoped { public Guid Id { get; } = Guid.NewGuid(); }
internal sealed class RequestIdSingleton : IRequestIdSingleton { public Guid Id { get; } = Guid.NewGuid(); }

View File

@@ -8,7 +8,8 @@
"Jwt": {
"Issuer": "CampusWorkshops",
"Audience": "CampusWorkshops.Api",
"Key": "some-key"
"Key": "some-key-that-is-super-secret-always-secured"
},
"Cache": { "DefaultTtlSeconds": 15 },
"AllowedHosts": "*"
}