Fix composition. Store compression options in map

This commit is contained in:
nzdev
2020-08-04 18:01:06 +12:00
parent d1449a0f5c
commit d4276dff58
8 changed files with 51 additions and 23 deletions

View File

@@ -7,9 +7,9 @@ using System.Threading.Tasks;
namespace Umbraco.Web.PublishedCache.NuCache.DataSource
{
public class AppSettingsNucachePropertyMapFactory : INucachePropertyOptionsFactory
public class AppSettingsNuCachePropertyMapFactory : INuCachePropertyOptionsFactory
{
public NucachePropertyOptions GetNucachePropertyOptions()
public NucachePropertyOptions GetNuCachePropertyOptions()
{
NucachePropertyOptions options = new NucachePropertyOptions
{

View File

@@ -6,8 +6,8 @@ using System.Threading.Tasks;
namespace Umbraco.Web.PublishedCache.NuCache.DataSource
{
public interface INucachePropertyOptionsFactory
public interface INuCachePropertyOptionsFactory
{
NucachePropertyOptions GetNucachePropertyOptions();
NucachePropertyOptions GetNuCachePropertyOptions();
}
}

View File

@@ -8,15 +8,23 @@ using System.Threading.Tasks;
namespace Umbraco.Web.PublishedCache.NuCache.DataSource
{
public class LazyCompressedString
/// <summary>
/// Lazily decompresses a LZ4 Pickler compressed UTF8 string
/// </summary>
internal class LazyCompressedString
{
private byte[] _bytes;
private string _str;
/// <summary>
/// Constructor
/// </summary>
/// <param name="bytes">LZ4 Pickle compressed UTF8 String</param>
public LazyCompressedString(byte[] bytes)
{
_bytes = bytes;
}
public override string ToString()
{
return LazyInitializer.EnsureInitialized(ref _str, () =>

View File

@@ -32,18 +32,16 @@ namespace Umbraco.Web.PublishedCache.NuCache.DataSource
internal class Lz4DictionaryOfPropertyDataSerializer : SerializerBase, ISerializer<IDictionary<string, PropertyData[]>>, IDictionaryOfPropertyDataSerializer
{
private readonly IReadOnlyDictionary<string, (NucachePropertyCompressionLevel compress, NucachePropertyDecompressionLevel decompressionLevel, string mappedAlias)> _compressProperties;
private readonly LZ4Level _compressionLevel;
private readonly long? _minimumStringLengthForCompression;
private readonly IReadOnlyDictionary<string, (NucachePropertyCompressionLevel compress, NucachePropertyDecompressionLevel decompressionLevel, string mappedAlias)> _uncompressProperties;
public Lz4DictionaryOfPropertyDataSerializer(NucachePropertyOptions nucachePropertyOptions)
public Lz4DictionaryOfPropertyDataSerializer(INuCachePropertyOptionsFactory nucachePropertyOptionsFactory)
{
var nucachePropertyOptions = nucachePropertyOptionsFactory.GetNuCachePropertyOptions();
_compressProperties = nucachePropertyOptions.PropertyMap.ToList().ToDictionary(x => string.Intern(x.Key), x => (x.Value.compress,x.Value.decompressionLevel, string.Intern(x.Value.mappedAlias)));
_minimumStringLengthForCompression = nucachePropertyOptions.MinimumCompressibleStringLength;
_uncompressProperties = _compressProperties.ToList().ToDictionary(x => x.Value.mappedAlias, x => (x.Value.compress, x.Value.decompressionLevel, x.Key));
_compressionLevel = nucachePropertyOptions.LZ4CompressionLevel;
_nucachePropertyOptions = nucachePropertyOptions;
}
@@ -84,12 +82,11 @@ namespace Umbraco.Web.PublishedCache.NuCache.DataSource
pdata.Segment = ReadStringObject(stream, true) ?? string.Empty;
pdata.Value = ReadObject(stream);
var decompressionLevel = NucachePropertyDecompressionLevel.Immediate;
if ((map.Compress.Equals(NucachePropertyCompressionLevel.NucacheDatabase) || map.Compress.Equals(NucachePropertyCompressionLevel.SQLDatabase))
&& pdata.Value != null && pdata.Value is byte[] byteArrayValue)
{
//Compressed string
switch (decompressionLevel)
switch (map.decompressionLevel)
{
case NucachePropertyDecompressionLevel.Lazy:
pdata.Value = new LazyCompressedString(byteArrayValue);
@@ -136,12 +133,12 @@ namespace Umbraco.Web.PublishedCache.NuCache.DataSource
//Only compress strings
if (pdata.Value is string stringValue && pdata.Value != null && map.Compress.Equals(NucachePropertyCompressionLevel.NucacheDatabase)
&& (_minimumStringLengthForCompression == null
|| !_minimumStringLengthForCompression.HasValue
|| stringValue.Length > _minimumStringLengthForCompression.Value))
&& (_nucachePropertyOptions.MinimumCompressibleStringLength == null
|| !_nucachePropertyOptions.MinimumCompressibleStringLength.HasValue
|| stringValue.Length > _nucachePropertyOptions.MinimumCompressibleStringLength.Value))
{
var stringBytes = Encoding.UTF8.GetBytes(stringValue);
var compressedBytes = LZ4Pickler.Pickle(stringBytes, _compressionLevel);
var compressedBytes = LZ4Pickler.Pickle(stringBytes, _nucachePropertyOptions.LZ4CompressionLevel);
WriteObject(compressedBytes, stream);
}
WriteObject(pdata.Value, stream);
@@ -149,6 +146,8 @@ namespace Umbraco.Web.PublishedCache.NuCache.DataSource
}
}
private readonly (NucachePropertyCompressionLevel Compress, NucachePropertyDecompressionLevel decompressionLevel, string MappedAlias) DEFAULT_MAP =(NucachePropertyCompressionLevel.None, NucachePropertyDecompressionLevel.NotCompressed, null);
private readonly NucachePropertyOptions _nucachePropertyOptions;
public (NucachePropertyCompressionLevel Compress, NucachePropertyDecompressionLevel decompressionLevel, string MappedAlias) GetSerializationMap(string propertyAlias)
{
if (_compressProperties == null)

View File

@@ -14,7 +14,7 @@ namespace Umbraco.Web.PublishedCache.NuCache.DataSource
private MessagePackSerializerOptions _options;
private readonly NucachePropertyOptions _propertyOptions;
public MsgPackContentNestedDataSerializer(NucachePropertyOptions propertyOptions = null)
public MsgPackContentNestedDataSerializer(INuCachePropertyOptionsFactory propertyOptionsFactory = null)
{
var defaultOptions = ContractlessStandardResolver.Options;
@@ -34,7 +34,7 @@ namespace Umbraco.Web.PublishedCache.NuCache.DataSource
_options = defaultOptions
.WithResolver(resolver)
.WithCompression(MessagePackCompression.Lz4BlockArray);
_propertyOptions = propertyOptions ?? new NucachePropertyOptions();
_propertyOptions = propertyOptionsFactory?.GetNuCachePropertyOptions() ?? new NucachePropertyOptions();
}
public string ToJson(string serialized)

View File

@@ -14,13 +14,14 @@ namespace Umbraco.Web.PublishedCache.NuCache
base.Compose(composition);
var serializer = ConfigurationManager.AppSettings["Umbraco.Web.PublishedCache.NuCache.Serializer"];
composition.Register<INuCachePropertyOptionsFactory, AppSettingsNuCachePropertyMapFactory>();
composition.Register<Lz4DictionaryOfPropertyDataSerializer, Lz4DictionaryOfPropertyDataSerializer>();
if (serializer == "MsgPack")
{
var propertyDictionarySerializer = ConfigurationManager.AppSettings["Umbraco.Web.PublishedCache.NuCache.DictionaryOfPropertiesSerializer"];
if (propertyDictionarySerializer == "LZ4Map")
{
composition.Register<INucachePropertyOptionsFactory, AppSettingsNucachePropertyMapFactory>();
composition.Register(factory =>
{
var lz4Serializer = factory.GetInstance<Lz4DictionaryOfPropertyDataSerializer>();