| | 1 | | using System.Text; |
| | 2 | | using System.Text.Json; |
| | 3 | | using System.Text.Json.Serialization; |
| | 4 | | using Microsoft.Extensions.Caching.Distributed; |
| | 5 | |
|
| | 6 | | namespace LGDXRobotCloud.API.Extensions; |
| | 7 | |
|
| | 8 | | public static class DistributedCacheExtensions |
| | 9 | | { |
| 0 | 10 | | private static readonly JsonSerializerOptions serializerOptions = new() |
| 0 | 11 | | { |
| 0 | 12 | | PropertyNamingPolicy = null, |
| 0 | 13 | | WriteIndented = true, |
| 0 | 14 | | AllowTrailingCommas = true, |
| 0 | 15 | | DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull |
| 0 | 16 | | }; |
| | 17 | |
|
| | 18 | | public static async Task SetAsync<T>(this IDistributedCache cache, string key, T value, DistributedCacheEntryOptions o |
| 0 | 19 | | { |
| 0 | 20 | | var bytes = Encoding.UTF8.GetBytes(JsonSerializer.Serialize(value, serializerOptions)); |
| 0 | 21 | | await cache.SetAsync(key, bytes, options); |
| 0 | 22 | | } |
| | 23 | |
|
| | 24 | | public static async Task<T?> GetAsync<T>(this IDistributedCache cache, string key) |
| 0 | 25 | | { |
| 0 | 26 | | var bytes = await cache.GetAsync(key); |
| 0 | 27 | | return bytes != null ? JsonSerializer.Deserialize<T>(bytes, serializerOptions) : default; |
| 0 | 28 | | } |
| | 29 | | } |