using System.Collections.Concurrent; using Microsoft.Extensions.Options; namespace Bit.Sso.Utilities { public class ExtendedOptionsMonitorCache : IExtendedOptionsMonitorCache where TOptions : class { private readonly ConcurrentDictionary> _cache = new ConcurrentDictionary>(StringComparer.Ordinal); public void AddOrUpdate(string name, TOptions options) { _cache.AddOrUpdate(name ?? Options.DefaultName, new Lazy(() => options), (string s, Lazy lazy) => new Lazy(() => options)); } public void Clear() { _cache.Clear(); } public TOptions GetOrAdd(string name, Func createOptions) { return _cache.GetOrAdd(name ?? Options.DefaultName, new Lazy(createOptions)).Value; } public bool TryAdd(string name, TOptions options) { return _cache.TryAdd(name ?? Options.DefaultName, new Lazy(() => options)); } public bool TryRemove(string name) { return _cache.TryRemove(name ?? Options.DefaultName, out _); } } }