diff --git a/src/Umbraco.Web/Cache/DataTypeCacheRefresher.cs b/src/Umbraco.Web/Cache/DataTypeCacheRefresher.cs index fe629a6ccf..2f9a914a82 100644 --- a/src/Umbraco.Web/Cache/DataTypeCacheRefresher.cs +++ b/src/Umbraco.Web/Cache/DataTypeCacheRefresher.cs @@ -116,6 +116,8 @@ namespace Umbraco.Web.Cache TagsValueConverter.ClearCaches(); MultipleMediaPickerPropertyConverter.ClearCaches(); SliderValueConverter.ClearCaches(); + MediaPickerPropertyConverter.ClearCaches(); + base.Refresh(jsonPayload); } diff --git a/src/Umbraco.Web/PropertyEditors/ValueConverters/MediaPickerPropertyConverter.cs b/src/Umbraco.Web/PropertyEditors/ValueConverters/MediaPickerPropertyConverter.cs index 69de94952b..97c3ee0295 100644 --- a/src/Umbraco.Web/PropertyEditors/ValueConverters/MediaPickerPropertyConverter.cs +++ b/src/Umbraco.Web/PropertyEditors/ValueConverters/MediaPickerPropertyConverter.cs @@ -3,18 +3,19 @@ // Umbraco // // -// The legacy media picker value converter +// The media picker 2 value converter // // -------------------------------------------------------------------------------------------------------------------- using System; +using System.Collections.Concurrent; using System.Collections.Generic; using System.Linq; using Umbraco.Core; -using Umbraco.Core.Logging; using Umbraco.Core.Models; using Umbraco.Core.Models.PublishedContent; using Umbraco.Core.PropertyEditors; +using Umbraco.Core.Services; using Umbraco.Web.Extensions; namespace Umbraco.Web.PropertyEditors.ValueConverters @@ -25,6 +26,20 @@ namespace Umbraco.Web.PropertyEditors.ValueConverters [DefaultPropertyValueConverter] public class MediaPickerPropertyConverter : PropertyValueConverterBase, IPropertyValueConverterMeta { + private readonly IDataTypeService _dataTypeService; + + //TODO: Remove this ctor in v8 since the other one will use IoC + public MediaPickerPropertyConverter() + : this(ApplicationContext.Current.Services.DataTypeService) + { + } + + public MediaPickerPropertyConverter(IDataTypeService dataTypeService) + { + if (dataTypeService == null) throw new ArgumentNullException("dataTypeService"); + _dataTypeService = dataTypeService; + } + public Type GetPropertyValueType(PublishedPropertyType propertyType) { return IsMultipleDataType(propertyType.DataTypeId) ? typeof(IEnumerable) : typeof(IPublishedContent); @@ -61,32 +76,21 @@ namespace Umbraco.Web.PropertyEditors.ValueConverters /// /// The . /// - private bool IsMultipleDataType(int dataTypeId) + public bool IsMultipleDataType(int dataTypeId) { - var multipleItems = false; + // GetPreValuesCollectionByDataTypeId is cached at repository level; + // still, the collection is deep-cloned so this is kinda expensive, + // better to cache here + trigger refresh in DataTypeCacheRefresher - try + return Storages.GetOrAdd(dataTypeId, id => { - var dts = ApplicationContext.Current.Services.DataTypeService; + var preValue = _dataTypeService.GetPreValuesCollectionByDataTypeId(id) + .PreValuesAsDictionary + .FirstOrDefault(x => string.Equals(x.Key, "multiPicker", StringComparison.InvariantCultureIgnoreCase)) + .Value; - var multiPickerPreValues = - dts.GetPreValuesCollectionByDataTypeId(dataTypeId).PreValuesAsDictionary; - - var multiPickerPreValue = multiPickerPreValues.FirstOrDefault(x => string.Equals(x.Key, "multiPicker", StringComparison.InvariantCultureIgnoreCase)).Value; - - var attemptConvert = multiPickerPreValue.Value.TryConvertTo(); - - if (attemptConvert.Success) - { - multipleItems = attemptConvert.Result; - } - } - catch (Exception ex) - { - LogHelper.Error(string.Format("Error finding multipleItems data type prevalue on data type Id {0}", dataTypeId), ex); - } - - return multipleItems; + return preValue != null && preValue.Value.TryConvertTo().Result; + }); } /// @@ -175,5 +179,12 @@ namespace Umbraco.Web.PropertyEditors.ValueConverters return source; } + + private static readonly ConcurrentDictionary Storages = new ConcurrentDictionary(); + + internal static void ClearCaches() + { + Storages.Clear(); + } } } \ No newline at end of file