#nullable enable using Baya.Application.Contracts.Common; using Microsoft.Extensions.Caching.Memory; namespace Baya.Infrastructure.CrossCutting.Seams; /// /// In-process implementation of — the mock seam. /// The real implementation swaps to Redis (StackExchange.Redis) while keeping the same key/TTL scheme. /// public sealed class MemoryCacheService(IMemoryCache cache) : ICacheService { public ValueTask GetAsync(string key, CancellationToken cancellationToken = default) { return ValueTask.FromResult(cache.TryGetValue(key, out T? value) ? value : default); } public ValueTask SetAsync(string key, T value, TimeSpan? ttl = null, CancellationToken cancellationToken = default) { var options = new MemoryCacheEntryOptions(); if (ttl is { } expiry) options.AbsoluteExpirationRelativeToNow = expiry; cache.Set(key, value, options); return ValueTask.CompletedTask; } public ValueTask RemoveAsync(string key, CancellationToken cancellationToken = default) { cache.Remove(key); return ValueTask.CompletedTask; } public async ValueTask GetOrCreateAsync( string key, Func> factory, TimeSpan? ttl = null, CancellationToken cancellationToken = default) { if (cache.TryGetValue(key, out T? cached) && cached is not null) return cached; var value = await factory(cancellationToken); await SetAsync(key, value, ttl, cancellationToken); return value; } }