⏪ Use Json Serializer in cache again
This commit is contained in:
@@ -1,16 +1,31 @@
|
|||||||
using System.Text.Json;
|
using System.Text.Json;
|
||||||
|
using System.Text.Json.Serialization;
|
||||||
|
using System.Text.Json.Serialization.Metadata;
|
||||||
|
using DysonNetwork.Shared.Data;
|
||||||
|
using NodaTime;
|
||||||
|
using NodaTime.Serialization.SystemTextJson;
|
||||||
|
|
||||||
namespace DysonNetwork.Shared.Cache;
|
namespace DysonNetwork.Shared.Cache;
|
||||||
|
|
||||||
public class JsonCacheSerializer(JsonSerializerOptions? options = null) : ICacheSerializer
|
public class JsonCacheSerializer : ICacheSerializer
|
||||||
{
|
{
|
||||||
private readonly JsonSerializerOptions _options = options ?? new JsonSerializerOptions(JsonSerializerDefaults.Web)
|
private readonly JsonSerializerOptions _options;
|
||||||
{
|
|
||||||
// Customize as needed (NodaTime, camelCase, converters, etc.)
|
|
||||||
WriteIndented = false
|
|
||||||
};
|
|
||||||
|
|
||||||
// Customize as needed (NodaTime, camelCase, converters, etc.)
|
public JsonCacheSerializer()
|
||||||
|
{
|
||||||
|
_options = new JsonSerializerOptions
|
||||||
|
{
|
||||||
|
TypeInfoResolver = new DefaultJsonTypeInfoResolver
|
||||||
|
{
|
||||||
|
Modifiers = { JsonExtensions.UnignoreAllProperties() },
|
||||||
|
},
|
||||||
|
ReferenceHandler = ReferenceHandler.Preserve,
|
||||||
|
NumberHandling = JsonNumberHandling.AllowNamedFloatingPointLiterals,
|
||||||
|
Converters = { new ByteStringConverter() }
|
||||||
|
};
|
||||||
|
_options.ConfigureForNodaTime(DateTimeZoneProviders.Tzdb);
|
||||||
|
_options.PropertyNameCaseInsensitive = true;
|
||||||
|
}
|
||||||
|
|
||||||
public string Serialize<T>(T value)
|
public string Serialize<T>(T value)
|
||||||
=> JsonSerializer.Serialize(value, _options);
|
=> JsonSerializer.Serialize(value, _options);
|
||||||
|
|||||||
@@ -1,12 +1,15 @@
|
|||||||
using MessagePack;
|
using MessagePack;
|
||||||
|
using MessagePack.Resolvers;
|
||||||
|
|
||||||
namespace DysonNetwork.Shared.Cache;
|
namespace DysonNetwork.Shared.Cache;
|
||||||
|
|
||||||
public class MessagePackCacheSerializer(MessagePackSerializerOptions? options = null) : ICacheSerializer
|
public class MessagePackCacheSerializer(MessagePackSerializerOptions? options = null) : ICacheSerializer
|
||||||
{
|
{
|
||||||
private readonly MessagePackSerializerOptions _options = options ?? MessagePackSerializerOptions.Standard
|
private readonly MessagePackSerializerOptions _options = options ?? MessagePackSerializerOptions.Standard
|
||||||
.WithResolver(MessagePack.Resolvers.ContractlessStandardResolver.Instance)
|
.WithResolver(ContractlessStandardResolver.Instance)
|
||||||
.WithCompression(MessagePackCompression.Lz4BlockArray);
|
.WithCompression(MessagePackCompression.Lz4BlockArray)
|
||||||
|
.WithSecurity(MessagePackSecurity.UntrustedData)
|
||||||
|
.WithOmitAssemblyVersion(true);
|
||||||
|
|
||||||
public string Serialize<T>(T value)
|
public string Serialize<T>(T value)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -70,8 +70,7 @@ public static class Extensions
|
|||||||
return RedLockFactory.Create(new List<RedLockMultiplexer> { new(mux) });
|
return RedLockFactory.Create(new List<RedLockMultiplexer> { new(mux) });
|
||||||
});
|
});
|
||||||
builder.Services.AddSingleton<ICacheService, CacheServiceRedis>();
|
builder.Services.AddSingleton<ICacheService, CacheServiceRedis>();
|
||||||
// Using message pack for now
|
builder.Services.AddSingleton<ICacheSerializer, JsonCacheSerializer>();
|
||||||
builder.Services.AddSingleton<ICacheSerializer, MessagePackCacheSerializer>();
|
|
||||||
|
|
||||||
return builder;
|
return builder;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user