using System.Collections.Concurrent; using TakeoutSaaS.Application.Dictionary.Abstractions; using TakeoutSaaS.Domain.Dictionary.Enums; namespace TakeoutSaaS.Integration.Tests.Fixtures; public sealed class TestDictionaryHybridCache : IDictionaryHybridCache { private readonly ConcurrentDictionary _cache = new(StringComparer.OrdinalIgnoreCase); public async Task GetOrCreateAsync( string key, TimeSpan ttl, Func> factory, CancellationToken cancellationToken = default) { if (_cache.TryGetValue(key, out var cached) && cached is T typed) { return typed; } var value = await factory(cancellationToken); if (value is not null) { _cache[key] = value; } return value; } public Task InvalidateAsync( string prefix, CacheInvalidationOperation operation = CacheInvalidationOperation.Update, CancellationToken cancellationToken = default) { foreach (var key in _cache.Keys) { if (key.StartsWith(prefix, StringComparison.OrdinalIgnoreCase)) { _cache.TryRemove(key, out _); } } return Task.CompletedTask; } }