From f09fcfbdd4ed4ef9584ab7f4982a09c3fa15da74 Mon Sep 17 00:00:00 2001 From: nzdev <834725+nzdev@users.noreply.github.com> Date: Fri, 27 Nov 2020 14:45:44 +1300 Subject: [PATCH] Fix serialization of decompressed lazy strings --- .../NuCache/DataSource/LazyCompressedString.cs | 9 ++++++++- .../NuCache/DataSource/SerializerBase.cs | 12 ++++++++++-- 2 files changed, 18 insertions(+), 3 deletions(-) diff --git a/src/Umbraco.Web/PublishedCache/NuCache/DataSource/LazyCompressedString.cs b/src/Umbraco.Web/PublishedCache/NuCache/DataSource/LazyCompressedString.cs index 3e0e796d36..99e6f44af7 100644 --- a/src/Umbraco.Web/PublishedCache/NuCache/DataSource/LazyCompressedString.cs +++ b/src/Umbraco.Web/PublishedCache/NuCache/DataSource/LazyCompressedString.cs @@ -13,6 +13,7 @@ namespace Umbraco.Web.PublishedCache.NuCache.DataSource private byte[] _bytes; private string _str; private readonly object _locker; + private bool _isDecompressed; /// /// Constructor @@ -22,7 +23,8 @@ namespace Umbraco.Web.PublishedCache.NuCache.DataSource { _locker = new object(); _bytes = bytes; - _str = null; + _str = null; + _isDecompressed = false; } public byte[] GetBytes() @@ -31,6 +33,10 @@ namespace Umbraco.Web.PublishedCache.NuCache.DataSource throw new InvalidOperationException("The bytes have already been expanded"); return _bytes; } + public bool IsDecompressed() + { + return _isDecompressed; + } public override string ToString() { @@ -41,6 +47,7 @@ namespace Umbraco.Web.PublishedCache.NuCache.DataSource if (_bytes == null) throw new PanicException("Bytes have already been cleared"); _str = Encoding.UTF8.GetString(LZ4Pickler.Unpickle(_bytes)); _bytes = null; + _isDecompressed = true; } return _str; } diff --git a/src/Umbraco.Web/PublishedCache/NuCache/DataSource/SerializerBase.cs b/src/Umbraco.Web/PublishedCache/NuCache/DataSource/SerializerBase.cs index e6ae789702..f7e2f32f5d 100644 --- a/src/Umbraco.Web/PublishedCache/NuCache/DataSource/SerializerBase.cs +++ b/src/Umbraco.Web/PublishedCache/NuCache/DataSource/SerializerBase.cs @@ -172,8 +172,16 @@ namespace Umbraco.Web.PublishedCache.NuCache.DataSource } else if (value is LazyCompressedString lazyCompressedString) { - PrimitiveSerializer.Char.WriteTo(PrefixCompressedStringByteArray, stream); - PrimitiveSerializer.Bytes.WriteTo(lazyCompressedString.GetBytes(), stream); + if (lazyCompressedString.IsDecompressed()) + { + PrimitiveSerializer.Char.WriteTo(PrefixString, stream); + PrimitiveSerializer.String.WriteTo(lazyCompressedString, stream); + } + else + { + PrimitiveSerializer.Char.WriteTo(PrefixCompressedStringByteArray, stream); + PrimitiveSerializer.Bytes.WriteTo(lazyCompressedString.GetBytes(), stream); + } } else if (value is sbyte signedByteValue) {