2022-06-30 01:46:41 +02:00
|
|
|
|
using System.Collections.Concurrent;
|
2020-09-04 19:56:08 +02:00
|
|
|
|
using Microsoft.Extensions.Options;
|
|
|
|
|
|
|
|
|
|
namespace Bit.Sso.Utilities;
|
2022-08-29 22:06:55 +02:00
|
|
|
|
|
2020-09-04 19:56:08 +02:00
|
|
|
|
public class ExtendedOptionsMonitorCache<TOptions> : IExtendedOptionsMonitorCache<TOptions> where TOptions : class
|
|
|
|
|
{
|
|
|
|
|
private readonly ConcurrentDictionary<string, Lazy<TOptions>> _cache =
|
|
|
|
|
new ConcurrentDictionary<string, Lazy<TOptions>>(StringComparer.Ordinal);
|
|
|
|
|
|
|
|
|
|
public void AddOrUpdate(string name, TOptions options)
|
|
|
|
|
{
|
|
|
|
|
_cache.AddOrUpdate(name ?? Options.DefaultName, new Lazy<TOptions>(() => options),
|
|
|
|
|
(string s, Lazy<TOptions> lazy) => new Lazy<TOptions>(() => options));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Clear()
|
|
|
|
|
{
|
|
|
|
|
_cache.Clear();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public TOptions GetOrAdd(string name, Func<TOptions> createOptions)
|
|
|
|
|
{
|
|
|
|
|
return _cache.GetOrAdd(name ?? Options.DefaultName, new Lazy<TOptions>(createOptions)).Value;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public bool TryAdd(string name, TOptions options)
|
2022-08-29 21:53:48 +02:00
|
|
|
|
{
|
2020-09-04 19:56:08 +02:00
|
|
|
|
return _cache.TryAdd(name ?? Options.DefaultName, new Lazy<TOptions>(() => options));
|
2022-08-29 21:53:48 +02:00
|
|
|
|
}
|
|
|
|
|
|
2020-09-04 19:56:08 +02:00
|
|
|
|
public bool TryRemove(string name)
|
|
|
|
|
{
|
|
|
|
|
return _cache.TryRemove(name ?? Options.DefaultName, out _);
|
|
|
|
|
}
|
|
|
|
|
}
|