Ex2 (integrado): decorator de cache para IWorkshopRepository + TTL por Options
This commit is contained in:
6
Services/CacheSettings.cs
Normal file
6
Services/CacheSettings.cs
Normal file
@@ -0,0 +1,6 @@
|
||||
namespace CampusWorkshops.Api.Services;
|
||||
|
||||
public sealed class CacheSettings
|
||||
{
|
||||
public int DefaultTtlSeconds { get; set; } = 15;
|
||||
}
|
||||
12
Services/PerRequestClock.cs
Normal file
12
Services/PerRequestClock.cs
Normal 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;
|
||||
}
|
||||
23
Services/ReportingSingleton.cs
Normal file
23
Services/ReportingSingleton.cs
Normal 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
9
Services/RequestId.cs
Normal 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(); }
|
||||
Reference in New Issue
Block a user