1
0
mirror of https://github.com/bitwarden/server.git synced 2024-12-04 14:13:28 +01:00
bitwarden-server/bitwarden_license/src/Sso/Utilities/ExtendedOptionsMonitorCache.cs

37 lines
1.1 KiB
C#
Raw Normal View History

using System.Collections.Concurrent;
2021-12-16 15:35:09 +01:00
using Microsoft.Extensions.Options;
2020-09-04 19:56:08 +02:00
2022-08-29 22:06:55 +02:00
namespace Bit.Sso.Utilities;
public class ExtendedOptionsMonitorCache<TOptions> : IExtendedOptionsMonitorCache<TOptions> where TOptions : class
2020-09-04 19:56:08 +02:00
{
2022-08-29 22:06:55 +02:00
private readonly ConcurrentDictionary<string, Lazy<TOptions>> _cache =
new ConcurrentDictionary<string, Lazy<TOptions>>(StringComparer.Ordinal);
2020-09-04 19:56:08 +02:00
2022-08-29 22:06:55 +02:00
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));
}
2020-09-04 19:56:08 +02:00
2022-08-29 22:06:55 +02:00
public void Clear()
{
_cache.Clear();
}
2020-09-04 19:56:08 +02:00
2022-08-29 22:06:55 +02:00
public TOptions GetOrAdd(string name, Func<TOptions> createOptions)
{
return _cache.GetOrAdd(name ?? Options.DefaultName, new Lazy<TOptions>(createOptions)).Value;
}
2020-09-04 19:56:08 +02:00
2022-08-29 22:06:55 +02:00
public bool TryAdd(string name, TOptions options)
{
return _cache.TryAdd(name ?? Options.DefaultName, new Lazy<TOptions>(() => options));
}
2022-08-29 22:06:55 +02:00
public bool TryRemove(string name)
{
return _cache.TryRemove(name ?? Options.DefaultName, out _);
2020-09-04 19:56:08 +02:00
}
2021-12-16 15:35:09 +01:00
}