aegis-api/Aegis.Infrastructure/Caching/MemoryUnlockedDatastoreCache.cs

39 lines
1.1 KiB
C#

using System.Collections.Concurrent;
using Aegis.Application.Abstractions;
using Aegis.Domain;
namespace Aegis.Repository.Caching;
public class MemoryUnlockedDatastoreCache : IUnlockedDataStoreCache
{
// key: "userId|datastoreId"
private readonly ConcurrentDictionary<string, DateTimeOffset> _expirations = new();
public bool IsUnlocked(UserId userId, DataStoreId datastoreId)
{
var key = $"{userId.Value}|{datastoreId.Value}";
if (!_expirations.TryGetValue(key, out var exp))
return false;
if (DateTimeOffset.UtcNow >= exp)
{
_expirations.TryRemove(key, out _);
return false;
}
return true;
}
// Helper para quando você implementar unlock:
public void MarkUnlocked(UserId userId, DataStoreId datastoreId, TimeSpan ttl)
{
var key = $"{userId.Value}|{datastoreId.Value}";
_expirations[key] = DateTimeOffset.UtcNow.Add(ttl);
}
public void Lock(UserId userId, DataStoreId datastoreId)
{
var key = $"{userId.Value}|{datastoreId.Value}";
_expirations.TryRemove(key, out _);
}
}