Kavita/API/Services/Caching/AuthKeyCacheInvalidator.cs
Joe Milazzo 8043650aa5
No more JWTs for Scripts + Polish (#4274)
Co-authored-by: Amelia <77553571+Fesaa@users.noreply.github.com>
2025-12-13 05:55:02 -08:00

27 lines
939 B
C#

using System.Threading;
using System.Threading.Tasks;
using API.Middleware;
using Microsoft.Extensions.Caching.Hybrid;
namespace API.Services.Caching;
public interface IAuthKeyCacheInvalidator
{
/// <summary>
/// Invalidates the cached authentication data for a specific auth key.
/// Call this when a key is rotated or deleted.
/// </summary>
/// <param name="keyValue">The actual key value (not the ID)</param>
/// <param name="cancellationToken">Cancellation token</param>
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);
}
}