From 6ce31efc278d8fa72a603d1821dc56dc19d5de7b Mon Sep 17 00:00:00 2001 From: mattbrailsford Date: Wed, 26 Mar 2014 13:57:41 +0000 Subject: [PATCH] Added caching to the datatype lookup to save database hits --- .../ArchetypeValueConverter.cs | 40 +++++++++---------- 1 file changed, 20 insertions(+), 20 deletions(-) diff --git a/app/Umbraco/Umbraco.Archetype/PropertyConverters/ArchetypeValueConverter.cs b/app/Umbraco/Umbraco.Archetype/PropertyConverters/ArchetypeValueConverter.cs index a8bfc8b..18a3bff 100644 --- a/app/Umbraco/Umbraco.Archetype/PropertyConverters/ArchetypeValueConverter.cs +++ b/app/Umbraco/Umbraco.Archetype/PropertyConverters/ArchetypeValueConverter.cs @@ -90,30 +90,30 @@ namespace Archetype.Umbraco.PropertyConverters internal ArchetypePreValue GetArchetypePreValueFromDataTypeId(int dataTypeId) { - var preValues = Services.DataTypeService.GetPreValuesCollectionByDataTypeId(dataTypeId); + return ApplicationContext.Current.ApplicationCache.RuntimeCache.GetCacheItem( + "Archetype_GetArchetypePreValueFromDataTypeId_" + dataTypeId, + () => + { + var preValues = Services.DataTypeService.GetPreValuesCollectionByDataTypeId(dataTypeId); - var configJson = preValues.IsDictionaryBased - ? preValues.PreValuesAsDictionary[Constants.PreValueAlias].Value - : preValues.PreValuesAsArray.First().Value; + var configJson = preValues.IsDictionaryBased + ? preValues.PreValuesAsDictionary[Constants.PreValueAlias].Value + : preValues.PreValuesAsArray.First().Value; - var config = JsonConvert.DeserializeObject(configJson, _jsonSettings); + var config = JsonConvert.DeserializeObject(configJson, _jsonSettings); - foreach (var fieldset in config.Fieldsets) - { - foreach (var property in fieldset.Properties) - { - // Lookup the properties property editor alias - // (See if we've already looked it up first though to save a database hit) - var propertyWithSameDataType = config.Fieldsets.SelectMany(x => x.Properties) - .FirstOrDefault(x => x.DataTypeId == property.DataTypeId && !string.IsNullOrWhiteSpace(x.PropertyEditorAlias)); + foreach (var fieldset in config.Fieldsets) + { + foreach (var property in fieldset.Properties) + { + property.PropertyEditorAlias = ApplicationContext.Current.ApplicationCache.RuntimeCache.GetCacheItem( + "Archetype_GetArchetypePreValueFromDataTypeId_GetPropertyEditorAlias_" + property.DataTypeId, + () => Services.DataTypeService.GetDataTypeDefinitionById(property.DataTypeId).PropertyEditorAlias).ToString(); + } + } - property.PropertyEditorAlias = propertyWithSameDataType != null - ? propertyWithSameDataType.PropertyEditorAlias - : Services.DataTypeService.GetDataTypeDefinitionById(property.DataTypeId).PropertyEditorAlias; - } - } - - return config; + return config; + }) as ArchetypePreValue; } } }