From 1a0d539e7632cf0f297bf0744a1670fc99046cca Mon Sep 17 00:00:00 2001 From: Polianin Nikita Date: Fri, 21 Jun 2024 21:44:34 +0300 Subject: [PATCH] fix: if cache get bytes then skip serealize --- .../Common/Services/Security/DistributedCacheService.cs | 2 +- Endpoint/Common/Services/Security/MemoryCacheService.cs | 7 ++++--- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/Endpoint/Common/Services/Security/DistributedCacheService.cs b/Endpoint/Common/Services/Security/DistributedCacheService.cs index bf3dc39..1d4a32b 100644 --- a/Endpoint/Common/Services/Security/DistributedCacheService.cs +++ b/Endpoint/Common/Services/Security/DistributedCacheService.cs @@ -17,7 +17,7 @@ public class DistributedCacheService(IDistributedCache cache) : ICacheService SlidingExpiration = slidingExpiration }; - var serializedValue = JsonSerializer.SerializeToUtf8Bytes(value); + var serializedValue = value as byte[] ?? JsonSerializer.SerializeToUtf8Bytes(value); await cache.SetAsync(key, serializedValue, options, cancellationToken); } diff --git a/Endpoint/Common/Services/Security/MemoryCacheService.cs b/Endpoint/Common/Services/Security/MemoryCacheService.cs index a428034..fcb1f09 100644 --- a/Endpoint/Common/Services/Security/MemoryCacheService.cs +++ b/Endpoint/Common/Services/Security/MemoryCacheService.cs @@ -1,6 +1,7 @@ using Microsoft.Extensions.Caching.Memory; using Mirea.Api.Security.Common.Interfaces; using System; +using System.Text.Json; using System.Threading; using System.Threading.Tasks; @@ -16,14 +17,14 @@ public class MemoryCacheService(IMemoryCache cache) : ICacheService SlidingExpiration = slidingExpiration }; - cache.Set(key, value, options); + cache.Set(key, value as byte[] ?? JsonSerializer.SerializeToUtf8Bytes(value), options); return Task.CompletedTask; } public Task GetAsync(string key, CancellationToken cancellationToken = default) { - cache.TryGetValue(key, out T? value); - return Task.FromResult(value); + cache.TryGetValue(key, out byte[]? value); + return Task.FromResult(JsonSerializer.Deserialize(value)); } public Task RemoveAsync(string key, CancellationToken cancellationToken = default)