fix: if cache get bytes then skip serealize
This commit is contained in:
parent
79151e7da8
commit
1a0d539e76
@ -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);
|
||||
}
|
||||
|
||||
|
@ -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<T?> GetAsync<T>(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<T>(value));
|
||||
}
|
||||
|
||||
public Task RemoveAsync(string key, CancellationToken cancellationToken = default)
|
||||
|
Loading…
Reference in New Issue
Block a user