using System.Threading;
using System.Threading.Tasks;
using API.Middleware;
using Microsoft.Extensions.Caching.Hybrid;
namespace API.Services.Caching;
public interface IAuthKeyCacheInvalidator
{
///
/// Invalidates the cached authentication data for a specific auth key.
/// Call this when a key is rotated or deleted.
///
/// The actual key value (not the ID)
/// Cancellation token
Task InvalidateAsync(string keyValue, CancellationToken cancellationToken = default);
}
public class AuthKeyCacheInvalidator(HybridCache cache) : IAuthKeyCacheInvalidator
{
public async Task InvalidateAsync(string keyValue, CancellationToken cancellationToken = default)
{
var cacheKey = AuthKeyAuthenticationHandler.CreateCacheKey(keyValue);
await cache.RemoveAsync(cacheKey, cancellationToken);
}
}