Adding Flags API
This commit is contained in:
22
src/api/MicCheck.Api/Features/FlagCache.cs
Normal file
22
src/api/MicCheck.Api/Features/FlagCache.cs
Normal file
@@ -0,0 +1,22 @@
|
||||
using Microsoft.Extensions.Caching.Memory;
|
||||
|
||||
namespace MicCheck.Api.Features;
|
||||
|
||||
public class FlagCache(IMemoryCache cache)
|
||||
{
|
||||
private static readonly TimeSpan CacheDuration = TimeSpan.FromSeconds(60);
|
||||
|
||||
public IReadOnlyList<FeatureStateResult>? Get(int environmentId)
|
||||
{
|
||||
cache.TryGetValue(CacheKey(environmentId), out IReadOnlyList<FeatureStateResult>? flags);
|
||||
return flags;
|
||||
}
|
||||
|
||||
public void Set(int environmentId, IReadOnlyList<FeatureStateResult> flags)
|
||||
=> cache.Set(CacheKey(environmentId), flags, CacheDuration);
|
||||
|
||||
public void Invalidate(int environmentId)
|
||||
=> cache.Remove(CacheKey(environmentId));
|
||||
|
||||
private static string CacheKey(int environmentId) => $"flags:{environmentId}";
|
||||
}
|
||||
Reference in New Issue
Block a user