Developer 02 0698b44b68 Refactor MemoryCacheExtensions and clean up HistoryController
- Introduced a static readonly field `BaseId` in `MemoryCacheExtensions.cs`.
- Refactored `GetEnumAsDictionary<TEnum>` to use expression-bodied syntax and LINQ for improved readability and efficiency.
- Removed the import statement for `Microsoft.IdentityModel.Tokens` in `HistoryController.cs`, indicating a potential shift in authentication/authorization handling.
2025-05-08 13:52:57 +02:00

17 lines
687 B
C#

using Microsoft.Extensions.Caching.Memory;
namespace EnvelopeGenerator.Extensions;
public static class MemoryCacheExtensions
{
private static readonly Guid BaseId = Guid.NewGuid();
public static IDictionary<string, int> GetEnumAsDictionary<TEnum>(this IMemoryCache memoryCache)
where TEnum : Enum
=> memoryCache.GetOrCreate(BaseId + typeof(TEnum).FullName, _ =>
Enum.GetValues(typeof(TEnum))
.Cast<TEnum>()
.ToDictionary(e => e.ToString(), e => Convert.ToInt32(e)))
?? throw new InvalidOperationException($"Failed to cache or retrieve enum dictionary for type '{typeof(TEnum).FullName}'.");
}