using System;
using System.Collections.Generic;
using System.Threading.Tasks;
namespace Schaad.Accounting.Repositories
{
///
/// Per-scope cache of deserialized XML lists, keyed by absolute file path.
///
/// Registered as Scoped so it lives for the SignalR circuit; each XML file is
/// then loaded at most once per circuit. Writes go through ,
/// which invalidates the corresponding entry so the next read reloads from disk.
///
/// Not thread-safe — callers within a single Blazor Server circuit run serialized on the
/// circuit's synchronization context, so no locking is needed.
///
public class RepositoryCache
{
private readonly Dictionary entries =
new(StringComparer.OrdinalIgnoreCase);
public async Task> GetOrLoadAsync(string cacheKey, Func>> loader)
{
if (entries.TryGetValue(cacheKey, out var cached))
{
return (List)cached;
}
var list = await loader();
entries[cacheKey] = list;
return list;
}
public void Invalidate(string cacheKey)
{
entries.Remove(cacheKey);
}
}
}