V15: Add custom serializer for hybrid cache (#17727)

* Add custom serializer

* Add migration to rebuild cache

* Rename migration namespace to 15.1

* Also clear media cache

* Remove failed cache items

* Refactor to only use keys for document cache repository

---------

Co-authored-by: nikolajlauridsen <nikolajlauridsen@protonmail.ch>
This commit is contained in:
Nikolaj Geisle
2024-12-06 13:20:57 +01:00
committed by GitHub
parent ce6d4c34e2
commit 1c859e75ca
11 changed files with 123 additions and 102 deletions

View File

@@ -0,0 +1,40 @@
using System.Buffers;
using MessagePack;
using MessagePack.Resolvers;
using Microsoft.Extensions.Caching.Hybrid;
using Microsoft.Extensions.Logging;
namespace Umbraco.Cms.Infrastructure.HybridCache.Serialization;
internal class HybridCacheSerializer : IHybridCacheSerializer<ContentCacheNode>
{
private readonly ILogger<HybridCacheSerializer> _logger;
private readonly MessagePackSerializerOptions _options;
public HybridCacheSerializer(ILogger<HybridCacheSerializer> logger)
{
_logger = logger;
MessagePackSerializerOptions defaultOptions = ContractlessStandardResolver.Options;
IFormatterResolver resolver = CompositeResolver.Create(defaultOptions.Resolver);
_options = defaultOptions
.WithResolver(resolver)
.WithCompression(MessagePackCompression.Lz4BlockArray)
.WithSecurity(MessagePackSecurity.UntrustedData);
}
public ContentCacheNode Deserialize(ReadOnlySequence<byte> source)
{
try
{
return MessagePackSerializer.Deserialize<ContentCacheNode>(source, _options);
}
catch (MessagePackSerializationException ex)
{
_logger.LogError(ex, "Error deserializing ContentCacheNode");
return null!;
}
}
public void Serialize(ContentCacheNode value, IBufferWriter<byte> target) => target.Write(MessagePackSerializer.Serialize(value, _options));
}