Refactor MemoryCacheExtensions and HistoryController

Updated MemoryCacheExtensions to accept more flexible parameters in GetEnumAsDictionary and improved the merging logic for ignored values. Simplified HistoryController by removing the logger dependency and adjusted GetReferenceTypes to include a key parameter for cache retrieval.
This commit is contained in:
Developer 02 2025-05-11 15:19:37 +02:00
parent ce41090979
commit 6126fce24d
2 changed files with 14 additions and 8 deletions

View File

@ -6,11 +6,20 @@ public static class MemoryCacheExtensions
{
private static readonly Guid BaseId = Guid.NewGuid();
public static IDictionary<string, int> GetEnumAsDictionary<TEnum>(this IMemoryCache memoryCache, string key = "", params IEnumerable<TEnum>[] ignores)
public static IDictionary<string, int> GetEnumAsDictionary<TEnum>(this IMemoryCache memoryCache, string key = "", params object[] ignores)
where TEnum : Enum
=> memoryCache.GetOrCreate(BaseId + typeof(TEnum).FullName + key, _ =>
{
List<TEnum> mergedIgnores = ignores.SelectMany(x => x).ToList();
var mergedIgnores = new List<TEnum>();
foreach (var ignore in ignores)
{
if (ignore is IEnumerable<TEnum> ignoreList)
mergedIgnores.AddRange(ignoreList);
else if (ignore is TEnum ignoreVal)
mergedIgnores.Add(ignoreVal);
}
return Enum.GetValues(typeof(TEnum))
.Cast<TEnum>()
.Where(e => !mergedIgnores.Contains(e))

View File

@ -18,8 +18,6 @@ namespace EnvelopeGenerator.GeneratorAPI.Controllers;
[Authorize]
public class HistoryController : ControllerBase
{
private readonly ILogger<HistoryController> _logger;
private readonly IEnvelopeHistoryService _service;
private readonly IMemoryCache _memoryCache;
@ -27,11 +25,10 @@ public class HistoryController : ControllerBase
/// <summary>
/// Konstruktor für den HistoryController.
/// </summary>
/// <param name="logger">Der Logger, der für das Protokollieren von Informationen verwendet wird.</param>
/// <param name="service">Der Dienst, der für die Verarbeitung der Umschlaghistorie verantwortlich ist.</param>
public HistoryController(ILogger<HistoryController> logger, IEnvelopeHistoryService service, IMemoryCache memoryCache)
/// <param name="memoryCache"></param>
public HistoryController(IEnvelopeHistoryService service, IMemoryCache memoryCache)
{
_logger = logger;
_service = service;
_memoryCache = memoryCache;
}
@ -54,7 +51,7 @@ public class HistoryController : ControllerBase
[Authorize]
public IActionResult GetReferenceTypes(ReferenceType? referenceType = null)
{
return referenceType is null ? Ok(_memoryCache.GetEnumAsDictionary<ReferenceType>()) : Ok(referenceType.ToString());
return referenceType is null ? Ok(_memoryCache.GetEnumAsDictionary<ReferenceType>("gen.api", ReferenceType.Unknown)) : Ok(referenceType.ToString());
}
/// <summary>