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>();

View File

@@ -251,7 +251,7 @@
<Compile Include="Compose\NestedContentPropertyComposer.cs" />
<Compile Include="PropertyEditors\ParameterEditors\MultipleMediaPickerParameterEditor.cs" />
<Compile Include="PropertyEditors\RichTextEditorPastedImages.cs" />
<Compile Include="PublishedCache\NuCache\DataSource\AppSettingsNucachePropertyMapFactory.cs" />
<Compile Include="PublishedCache\NuCache\DataSource\AppSettingsNuCachePropertyMapFactory.cs" />
<Compile Include="PublishedCache\NuCache\DataSource\IContentNestedDataSerializer.cs" />
<Compile Include="PublishedCache\NuCache\DataSource\IDictionaryOfPropertyDataSerializer.cs" />
<Compile Include="PublishedCache\NuCache\DataSource\INucachePropertyOptionsFactory.cs" />

View File

@@ -58,8 +58,24 @@ Project("{E24C65DC-7377-472B-9ABA-BC803B73C61A}") = "Umbraco.Web.UI.Client", "ht
StartServerOnDebug = "false"
EndProjectSection
EndProject
Project("{E24C65DC-7377-472B-9ABA-BC803B73C61A}") = "Umbraco.Tests.AcceptanceTest\", "Umbraco.Tests.AcceptanceTest\", "{9E4C8A12-FBE0-4673-8CE2-DF99D5D57817}"
Project("{E24C65DC-7377-472B-9ABA-BC803B73C61A}") = "Umbraco.Tests.AcceptanceTest", "Umbraco.Tests.AcceptanceTest\", "{9E4C8A12-FBE0-4673-8CE2-DF99D5D57817}"
ProjectSection(WebsiteProperties) = preProject
TargetFrameworkMoniker = ".NETFramework,Version%3Dv4.0"
Debug.AspNetCompiler.VirtualPath = "/localhost_49800"
Debug.AspNetCompiler.PhysicalPath = "Umbraco.Tests.AcceptanceTest\"
Debug.AspNetCompiler.TargetPath = "PrecompiledWeb\localhost_49800\"
Debug.AspNetCompiler.Updateable = "true"
Debug.AspNetCompiler.ForceOverwrite = "true"
Debug.AspNetCompiler.FixedNames = "false"
Debug.AspNetCompiler.Debug = "True"
Release.AspNetCompiler.VirtualPath = "/localhost_49800"
Release.AspNetCompiler.PhysicalPath = "Umbraco.Tests.AcceptanceTest\"
Release.AspNetCompiler.TargetPath = "PrecompiledWeb\localhost_49800\"
Release.AspNetCompiler.Updateable = "true"
Release.AspNetCompiler.ForceOverwrite = "true"
Release.AspNetCompiler.FixedNames = "false"
Release.AspNetCompiler.Debug = "False"
VWDPort = "49800"
SlnRelativePath = "Umbraco.Tests.AcceptanceTest\"
EndProjectSection
EndProject
@@ -123,6 +139,10 @@ Global
{4C4C194C-B5E4-4991-8F87-4373E24CC19F}.Release|Any CPU.Build.0 = Release|Any CPU
{3819A550-DCEC-4153-91B4-8BA9F7F0B9B4}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{3819A550-DCEC-4153-91B4-8BA9F7F0B9B4}.Release|Any CPU.ActiveCfg = Debug|Any CPU
{9E4C8A12-FBE0-4673-8CE2-DF99D5D57817}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{9E4C8A12-FBE0-4673-8CE2-DF99D5D57817}.Debug|Any CPU.Build.0 = Debug|Any CPU
{9E4C8A12-FBE0-4673-8CE2-DF99D5D57817}.Release|Any CPU.ActiveCfg = Debug|Any CPU
{9E4C8A12-FBE0-4673-8CE2-DF99D5D57817}.Release|Any CPU.Build.0 = Debug|Any CPU
{651E1350-91B6-44B7-BD60-7207006D7003}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{651E1350-91B6-44B7-BD60-7207006D7003}.Debug|Any CPU.Build.0 = Debug|Any CPU
{651E1350-91B6-44B7-BD60-7207006D7003}.Release|Any CPU.ActiveCfg = Release|Any CPU
@@ -157,6 +177,7 @@ Global
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{227C3B55-80E5-4E7E-A802-BE16C5128B9D} = {2849E9D4-3B4E-40A3-A309-F3CB4F0E125F}
{9E4C8A12-FBE0-4673-8CE2-DF99D5D57817} = {B5BD12C1-A454-435E-8A46-FF4A364C0382}
{5D3B8245-ADA6-453F-A008-50ED04BFE770} = {B5BD12C1-A454-435E-8A46-FF4A364C0382}
{E3F9F378-AFE1-40A5-90BD-82833375DBFE} = {227C3B55-80E5-4E7E-A802-BE16C5128B9D}
{5B03EF4E-E0AC-4905-861B-8C3EC1A0D458} = {227C3B55-80E5-4E7E-A802-BE16C5128B9D}
@@ -164,7 +185,6 @@ Global
{3A33ADC9-C6C0-4DB1-A613-A9AF0210DF3D} = {B5BD12C1-A454-435E-8A46-FF4A364C0382}
{C7311C00-2184-409B-B506-52A5FAEA8736} = {FD962632-184C-4005-A5F3-E705D92FC645}
{FB5676ED-7A69-492C-B802-E7B24144C0FC} = {B5BD12C1-A454-435E-8A46-FF4A364C0382}
{9E4C8A12-FBE0-4673-8CE2-DF99D5D57817} = {B5BD12C1-A454-435E-8A46-FF4A364C0382}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {7A0F2E34-D2AF-4DAB-86A0-7D7764B3D0EC}