Add memory caching support in HistoryController
- Updated `EnvelopeGenerator.Extensions.csproj` to include `Microsoft.Extensions.Caching.Memory` package. - Refactored `HistoryController` to use `IMemoryCache` for caching functionality. - Replaced manual dictionary creation in `GetReferenceTypes` with a call to `GetEnumAsDictionary<ReferenceType>()`. - Changed enum type in `GetEnvelopeStatus` to `ReferenceType` for consistency. - Introduced `MemoryCacheExtensions` class with `GetEnumAsDictionary<TEnum>` method for efficient enum to dictionary conversion.
This commit is contained in:
parent
3a1fe45524
commit
3035ec7e9c
@ -13,6 +13,7 @@
|
|||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<PackageReference Include="HtmlSanitizer" Version="8.0.865" />
|
<PackageReference Include="HtmlSanitizer" Version="8.0.865" />
|
||||||
|
<PackageReference Include="Microsoft.Extensions.Caching.Memory" Version="9.0.4" />
|
||||||
<PackageReference Include="Microsoft.Extensions.Localization.Abstractions" Version="7.0.19" />
|
<PackageReference Include="Microsoft.Extensions.Localization.Abstractions" Version="7.0.19" />
|
||||||
<PackageReference Include="Microsoft.Extensions.Logging" Version="8.0.0" />
|
<PackageReference Include="Microsoft.Extensions.Logging" Version="8.0.0" />
|
||||||
<PackageReference Include="Otp.NET" Version="1.4.0" />
|
<PackageReference Include="Otp.NET" Version="1.4.0" />
|
||||||
|
|||||||
21
EnvelopeGenerator.Extensions/MemoryCacheExtensions.cs
Normal file
21
EnvelopeGenerator.Extensions/MemoryCacheExtensions.cs
Normal file
@ -0,0 +1,21 @@
|
|||||||
|
using Microsoft.Extensions.Caching.Memory;
|
||||||
|
|
||||||
|
namespace EnvelopeGenerator.Extensions;
|
||||||
|
|
||||||
|
public static class MemoryCacheExtensions
|
||||||
|
{
|
||||||
|
public static IDictionary<string, int> GetEnumAsDictionary<TEnum>(this IMemoryCache memoryCache)
|
||||||
|
where TEnum : Enum
|
||||||
|
{
|
||||||
|
var referenceTypes = Enum.GetValues(typeof(TEnum))
|
||||||
|
.Cast<TEnum>()
|
||||||
|
.ToDictionary(rt =>
|
||||||
|
{
|
||||||
|
var key = rt.ToString();
|
||||||
|
var keyAsCamelCase = char.ToLowerInvariant(key[0]) + key[1..];
|
||||||
|
return keyAsCamelCase;
|
||||||
|
}, rt => Convert.ToInt32(rt));
|
||||||
|
|
||||||
|
return referenceTypes;
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -1,8 +1,9 @@
|
|||||||
using DigitalData.EmailProfilerDispatcher.Abstraction.Entities;
|
using EnvelopeGenerator.Application.Contracts.Services;
|
||||||
using EnvelopeGenerator.Application.Contracts.Services;
|
|
||||||
using EnvelopeGenerator.Application.Histories.Queries.Read;
|
using EnvelopeGenerator.Application.Histories.Queries.Read;
|
||||||
|
using EnvelopeGenerator.Extensions;
|
||||||
using Microsoft.AspNetCore.Authorization;
|
using Microsoft.AspNetCore.Authorization;
|
||||||
using Microsoft.AspNetCore.Mvc;
|
using Microsoft.AspNetCore.Mvc;
|
||||||
|
using Microsoft.Extensions.Caching.Memory;
|
||||||
using static EnvelopeGenerator.Common.Constants;
|
using static EnvelopeGenerator.Common.Constants;
|
||||||
|
|
||||||
|
|
||||||
@ -20,15 +21,18 @@ public class HistoryController : ControllerBase
|
|||||||
|
|
||||||
private readonly IEnvelopeHistoryService _service;
|
private readonly IEnvelopeHistoryService _service;
|
||||||
|
|
||||||
|
private readonly IMemoryCache _memoryCache;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Konstruktor für den HistoryController.
|
/// Konstruktor für den HistoryController.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <param name="logger">Der Logger, der für das Protokollieren von Informationen verwendet wird.</param>
|
/// <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>
|
/// <param name="service">Der Dienst, der für die Verarbeitung der Umschlaghistorie verantwortlich ist.</param>
|
||||||
public HistoryController(ILogger<HistoryController> logger, IEnvelopeHistoryService service)
|
public HistoryController(ILogger<HistoryController> logger, IEnvelopeHistoryService service, IMemoryCache memoryCache)
|
||||||
{
|
{
|
||||||
_logger = logger;
|
_logger = logger;
|
||||||
_service = service;
|
_service = service;
|
||||||
|
_memoryCache = memoryCache;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@ -49,17 +53,7 @@ public class HistoryController : ControllerBase
|
|||||||
[Authorize]
|
[Authorize]
|
||||||
public IActionResult GetReferenceTypes()
|
public IActionResult GetReferenceTypes()
|
||||||
{
|
{
|
||||||
// Enum zu Schlüssel-Wert-Paar
|
return Ok(_memoryCache.GetEnumAsDictionary<ReferenceType>());
|
||||||
var referenceTypes = Enum.GetValues(typeof(ReferenceType))
|
|
||||||
.Cast<ReferenceType>()
|
|
||||||
.ToDictionary(rt =>
|
|
||||||
{
|
|
||||||
var key = rt.ToString();
|
|
||||||
var keyAsCamelCase = char.ToLower(key[0]) + key[1..];
|
|
||||||
return keyAsCamelCase;
|
|
||||||
}, rt => (int)rt);
|
|
||||||
|
|
||||||
return Ok(referenceTypes);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
@ -105,8 +99,8 @@ public class HistoryController : ControllerBase
|
|||||||
public IActionResult GetEnvelopeStatus([FromQuery] ReferenceType? related = null)
|
public IActionResult GetEnvelopeStatus([FromQuery] ReferenceType? related = null)
|
||||||
{
|
{
|
||||||
// Enum zu Schlüssel-Wert-Paar
|
// Enum zu Schlüssel-Wert-Paar
|
||||||
var referenceTypes = Enum.GetValues(typeof(EnvelopeStatus))
|
var referenceTypes = Enum.GetValues(typeof(ReferenceType))
|
||||||
.Cast<EnvelopeStatus>()
|
.Cast<ReferenceType>()
|
||||||
.ToDictionary(rt =>
|
.ToDictionary(rt =>
|
||||||
{
|
{
|
||||||
var key = rt.ToString();
|
var key = rt.ToString();
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user