using System; using System.Collections.Concurrent; using Microsoft.Extensions.Options; namespace Oqtane.Infrastructure { public class SiteOptionsCache : IOptionsMonitorCache where TOptions : class, new() { private readonly IAliasAccessor _aliasAccessor; private readonly ConcurrentDictionary> map = new ConcurrentDictionary>(); public SiteOptionsCache(IAliasAccessor aliasAccessor) { _aliasAccessor = aliasAccessor; } public void Clear() { var cache = map.GetOrAdd(GetKey(), new OptionsCache()); cache.Clear(); } public TOptions GetOrAdd(string name, Func createOptions) { name = name ?? Options.DefaultName; var cache = map.GetOrAdd(GetKey(), new OptionsCache()); return cache.GetOrAdd(name, createOptions); } public bool TryAdd(string name, TOptions options) { name = name ?? Options.DefaultName; var cache = map.GetOrAdd(GetKey(), new OptionsCache()); return cache.TryAdd(name, options); } public bool TryRemove(string name) { name = name ?? Options.DefaultName; var cache = map.GetOrAdd(GetKey(), new OptionsCache()); return cache.TryRemove(name); } private string GetKey() { return _aliasAccessor?.Alias?.SiteKey ?? ""; } } }