using EnvelopeGenerator.Application.Common.Extensions;
using Microsoft.Extensions.Caching.Memory;
namespace EnvelopeGenerator.Application.Common.Extensions;
///
///
///
public static class MemoryCacheExtensions
{
private static readonly Guid BaseId = Guid.NewGuid();
///
///
///
///
///
///
///
///
///
public static IDictionary GetEnumAsDictionary(this IMemoryCache memoryCache, string key = "", params object[] ignores)
where TEnum : Enum
=> memoryCache.GetOrCreate(BaseId + typeof(TEnum).FullName + key, _ =>
{
var mergedIgnores = new List();
foreach (var ignore in ignores)
{
if (ignore is IEnumerable ignoreList)
mergedIgnores.AddRange(ignoreList);
else if (ignore is TEnum ignoreVal)
mergedIgnores.Add(ignoreVal);
}
return Enum.GetValues(typeof(TEnum))
.Cast()
.Where(e => !mergedIgnores.Contains(e))
.ToDictionary(e => e.ToString(), e => Convert.ToInt32(e));
})
?? throw new InvalidOperationException($"Failed to cache or retrieve enum dictionary for type '{typeof(TEnum).FullName}'.");
}