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

Commit 1140e4b

Browse files
committed
Replaced the DataType and ContentType's cache expiry with the distributed cache refresher
This is the approach to use to support load-balancing
1 parent d96137c commit 1140e4b

File tree

1 file changed

+39
-25
lines changed

1 file changed

+39
-25
lines changed
Lines changed: 39 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1-
using System.Web.Mvc;
1+
using System;
2+
using System.Web.Mvc;
3+
using Newtonsoft.Json;
24
using Our.Umbraco.DocTypeGridEditor.Web.Attributes;
35
using Our.Umbraco.DocTypeGridEditor.Web.Mvc;
46
using Umbraco.Core;
5-
using Umbraco.Core.Events;
6-
using Umbraco.Core.Models;
7-
using Umbraco.Core.Services;
7+
using Umbraco.Core.Sync;
8+
using Umbraco.Web.Cache;
89

910
namespace Our.Umbraco.DocTypeGridEditor
1011
{
@@ -22,32 +23,45 @@ protected override void ApplicationStarting(UmbracoApplicationBase umbracoApplic
2223

2324
protected override void ApplicationStarted(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
2425
{
25-
DataTypeService.Saved += ExpireDataTypeCache;
26-
ContentTypeService.SavedContentType += ExpireContentTypeCache;
27-
}
28-
29-
private void ExpireDataTypeCache(IDataTypeService sender, SaveEventArgs<IDataTypeDefinition> e)
30-
{
31-
foreach (var dataType in e.SavedEntities)
26+
DataTypeCacheRefresher.CacheUpdated += (sender, e) =>
3227
{
33-
ApplicationContext.Current.ApplicationCache.RuntimeCache.ClearCacheItem(
34-
string.Concat("Our.Umbraco.DocTypeGridEditor.Web.Extensions.ContentTypeServiceExtensions.GetAliasById_", dataType.Key));
28+
if (e.MessageType == MessageType.RefreshByJson)
29+
{
30+
var payload = JsonConvert.DeserializeAnonymousType((string)e.MessageObject, new[] { new { Id = default(int), UniqueId = default(Guid) } });
31+
if (payload != null)
32+
{
33+
foreach (var item in payload)
34+
{
35+
applicationContext.ApplicationCache.RuntimeCache.ClearCacheItem(
36+
string.Concat("Our.Umbraco.DocTypeGridEditor.Web.Extensions.ContentTypeServiceExtensions.GetAliasById_", item.UniqueId));
3537

36-
ApplicationContext.Current.ApplicationCache.RuntimeCache.ClearCacheItem(
37-
string.Concat("Our.Umbraco.DocTypeGridEditor.Helpers.DocTypeGridEditorHelper.GetPreValuesCollectionByDataTypeId_", dataType.Id));
38-
}
39-
}
38+
applicationContext.ApplicationCache.RuntimeCache.ClearCacheItem(
39+
string.Concat("Our.Umbraco.DocTypeGridEditor.Helpers.DocTypeGridEditorHelper.GetPreValuesCollectionByDataTypeId_", item.Id));
40+
}
41+
}
42+
}
43+
};
4044

41-
private void ExpireContentTypeCache(IContentTypeService sender, SaveEventArgs<IContentType> e)
42-
{
43-
foreach (var contentType in e.SavedEntities)
45+
ContentTypeCacheRefresher.CacheUpdated += (sender, e) =>
4446
{
45-
ApplicationContext.Current.ApplicationCache.RuntimeCache.ClearCacheItem(
46-
string.Concat("Our.Umbraco.DocTypeGridEditor.Helpers.DocTypeGridEditorHelper.GetContentTypesByAlias_", contentType.Alias));
47+
if (e.MessageType == MessageType.RefreshByJson)
48+
{
49+
var payload = JsonConvert.DeserializeAnonymousType((string)e.MessageObject, new[] { new { Alias = default(string) } });
50+
if (payload != null)
51+
{
52+
foreach (var item in payload)
53+
{
54+
applicationContext.ApplicationCache.RuntimeCache.ClearCacheItem(
55+
string.Concat("Our.Umbraco.DocTypeGridEditor.Helpers.DocTypeGridEditorHelper.GetContentTypesByAlias_", item.Alias));
4756

48-
ApplicationContext.Current.ApplicationCache.RuntimeCache.ClearCacheItem(
49-
string.Concat("Our.Umbraco.DocTypeGridEditor.Helpers.DocTypeGridEditorHelper.GetContentTypeAliasByGuid_", contentType.Key));
50-
}
57+
// NOTE: Unsure how to get the doctype GUID, without hitting the database?
58+
// So we end up clearing the entire cache for this key. [LK:2018-01-30]
59+
applicationContext.ApplicationCache.RuntimeCache.ClearCacheByKeySearch(
60+
"Our.Umbraco.DocTypeGridEditor.Helpers.DocTypeGridEditorHelper.GetContentTypeAliasByGuid_");
61+
}
62+
}
63+
}
64+
};
5165
}
5266
}
5367
}

0 commit comments

Comments
 (0)