Skip to content
This repository was archived by the owner on Feb 10, 2024. It is now read-only.

Commit 60bc7b8

Browse files
Added caching to expensive helper methods
1 parent 9526dbc commit 60bc7b8

File tree

2 files changed

+57
-15
lines changed

2 files changed

+57
-15
lines changed

src/Our.Umbraco.DocTypeGridEditor/Bootstrap.cs

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,31 @@ protected override void ApplicationStarting(UmbracoApplicationBase umbracoApplic
1919

2020
protected override void ApplicationStarted(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
2121
{
22-
DataTypeService.Saved += ExpireCache;
22+
DataTypeService.Saved += ExpireDataTypeCache;
23+
ContentTypeService.SavedContentType += ExpireContentTypeCache;
2324
}
2425

25-
private void ExpireCache(IDataTypeService sender, SaveEventArgs<IDataTypeDefinition> e)
26+
private void ExpireDataTypeCache(IDataTypeService sender, SaveEventArgs<IDataTypeDefinition> e)
2627
{
2728
foreach (var dataType in e.SavedEntities)
2829
{
2930
ApplicationContext.Current.ApplicationCache.RuntimeCache.ClearCacheItem(
3031
string.Concat("Our.Umbraco.DocTypeGridEditor.Web.Extensions.ContentTypeServiceExtensions.GetAliasById_", dataType.Key));
32+
33+
ApplicationContext.Current.ApplicationCache.RuntimeCache.ClearCacheItem(
34+
string.Concat("Our.Umbraco.DocTypeGridEditor.Helpers.DocTypeGridEditorHelper.GetPreValuesCollectionByDataTypeId_", dataType.Id));
35+
}
36+
}
37+
38+
private void ExpireContentTypeCache(IContentTypeService sender, SaveEventArgs<IContentType> e)
39+
{
40+
foreach (var contentType in e.SavedEntities)
41+
{
42+
ApplicationContext.Current.ApplicationCache.RuntimeCache.ClearCacheItem(
43+
string.Concat("Our.Umbraco.DocTypeGridEditor.Helpers.DocTypeGridEditorHelper.GetContentTypesByAlias_", contentType.Alias));
44+
45+
ApplicationContext.Current.ApplicationCache.RuntimeCache.ClearCacheItem(
46+
string.Concat("Our.Umbraco.DocTypeGridEditor.Helpers.DocTypeGridEditorHelper.GetContentTypeAliasByGuid_", contentType.Key));
3147
}
3248
}
3349
}

src/Our.Umbraco.DocTypeGridEditor/Helpers/DocTypeGridEditorHelper.cs

Lines changed: 39 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public static IPublishedContent ConvertValueToContent(string id, string contentT
3232
return ConvertValue(id, contentTypeAlias, dataJson);
3333

3434
return (IPublishedContent)ApplicationContext.Current.ApplicationCache.RequestCache.GetCacheItem(
35-
string.Concat("DocTypeGridEditorHelper.ConvertValueToContent_", id, "_", contentTypeAlias),
35+
string.Concat("Our.Umbraco.DocTypeGridEditor.Helpers.DocTypeGridEditorHelper.ConvertValueToContent_", id, "_", contentTypeAlias),
3636
() =>
3737
{
3838
return ConvertValue(id, contentTypeAlias, dataJson);
@@ -41,22 +41,17 @@ public static IPublishedContent ConvertValueToContent(string id, string contentT
4141

4242
private static IPublishedContent ConvertValue(string id, string contentTypeAlias, string dataJson)
4343
{
44-
using (var timer = DisposableTimer.DebugDuration<DocTypeGridEditorHelper>(string.Format("ConvertValueToContent ({0}, {1})", id, contentTypeAlias)))
44+
using (var timer = DisposableTimer.DebugDuration<DocTypeGridEditorHelper>(string.Format("ConvertValue ({0}, {1})", id, contentTypeAlias)))
4545
{
46-
Guid contentTypeGuid;
47-
if (Guid.TryParse(contentTypeAlias, out contentTypeGuid))
48-
contentTypeAlias = Services.ContentTypeService.GetAliasByGuid(contentTypeGuid);
49-
50-
var publishedContentType = PublishedContentType.Get(PublishedItemType.Content, contentTypeAlias);
51-
var contentType = Services.ContentTypeService.GetContentType(contentTypeAlias);
46+
var contentTypes = GetContentTypesByAlias(contentTypeAlias);
5247
var properties = new List<IPublishedProperty>();
5348

5449
// Convert all the properties
5550
var data = JsonConvert.DeserializeObject(dataJson);
5651
var propValues = ((JObject)data).ToObject<Dictionary<string, object>>();
5752
foreach (var jProp in propValues)
5853
{
59-
var propType = publishedContentType.GetPropertyType(jProp.Key);
54+
var propType = contentTypes.PublishedContentType.GetPropertyType(jProp.Key);
6055
if (propType != null)
6156
{
6257
/* Because we never store the value in the database, we never run the property editors
@@ -65,8 +60,7 @@ private static IPublishedContent ConvertValue(string id, string contentTypeAlias
6560
* we go on to convert the value for the view.
6661
*/
6762
var propEditor = PropertyEditorResolver.Current.GetByAlias(propType.PropertyEditorAlias);
68-
var propPreValues = Services.DataTypeService.GetPreValuesCollectionByDataTypeId(
69-
propType.DataTypeId);
63+
var propPreValues = GetPreValuesCollectionByDataTypeId(propType.DataTypeId);
7064

7165
var contentPropData = new ContentPropertyData(
7266
jProp.Value,
@@ -78,7 +72,7 @@ private static IPublishedContent ConvertValue(string id, string contentTypeAlias
7872
/* Now that we have the DB stored value, we actually need to then convert it into it's
7973
* XML serialized state as expected by the published property by calling ConvertDbToString
8074
*/
81-
var propType2 = contentType.CompositionPropertyTypes.Single(x => x.Alias.InvariantEquals(propType.PropertyTypeAlias));
75+
var propType2 = contentTypes.ContentType.CompositionPropertyTypes.Single(x => x.Alias.InvariantEquals(propType.PropertyTypeAlias));
8276

8377
Property prop2 = null;
8478
try
@@ -117,11 +111,43 @@ private static IPublishedContent ConvertValue(string id, string contentTypeAlias
117111
var containerNode = pcr != null && pcr.HasPublishedContent ? pcr.PublishedContent : null;
118112

119113
return new DetachedPublishedContent(nameObj != null ? nameObj.ToString() : null,
120-
publishedContentType,
114+
contentTypes.PublishedContentType,
121115
properties.ToArray(),
122116
containerNode);
123117
}
124118

125119
}
120+
121+
private static PreValueCollection GetPreValuesCollectionByDataTypeId(int dataTypeId)
122+
{
123+
return (PreValueCollection)ApplicationContext.Current.ApplicationCache.RuntimeCache.GetCacheItem("Our.Umbraco.DocTypeGridEditor.Helpers.DocTypeGridEditorHelper.GetPreValuesCollectionByDataTypeId_" + dataTypeId,
124+
() => Services.DataTypeService.GetPreValuesCollectionByDataTypeId(dataTypeId));
125+
}
126+
127+
private static ContentTypeContainer GetContentTypesByAlias(string contentTypeAlias)
128+
{
129+
Guid contentTypeGuid;
130+
if (Guid.TryParse(contentTypeAlias, out contentTypeGuid))
131+
contentTypeAlias = GetContentTypeAliasByGuid(contentTypeGuid);
132+
133+
return (ContentTypeContainer)ApplicationContext.Current.ApplicationCache.RuntimeCache.GetCacheItem("Our.Umbraco.DocTypeGridEditor.Helpers.DocTypeGridEditorHelper.GetContentTypesByAlias_" + contentTypeAlias,
134+
() => new ContentTypeContainer
135+
{
136+
PublishedContentType = PublishedContentType.Get(PublishedItemType.Content, contentTypeAlias),
137+
ContentType = Services.ContentTypeService.GetContentType(contentTypeAlias)
138+
});
139+
}
140+
141+
private static string GetContentTypeAliasByGuid(Guid contentTypeGuid)
142+
{
143+
return (string)ApplicationContext.Current.ApplicationCache.RuntimeCache.GetCacheItem("Our.Umbraco.DocTypeGridEditor.Helpers.DocTypeGridEditorHelper.GetContentTypeAliasByGuid_" + contentTypeGuid,
144+
() => Services.ContentTypeService.GetAliasByGuid(contentTypeGuid));
145+
}
146+
}
147+
148+
public class ContentTypeContainer
149+
{
150+
public PublishedContentType PublishedContentType { get; set; }
151+
public IContentType ContentType { get; set; }
126152
}
127153
}

0 commit comments

Comments
 (0)