Skip to content
This repository was archived by the owner on Jun 18, 2018. It is now read-only.

Commit fc5086b

Browse files
committed
Replaced the DataType's cache expiry with the distributed cache refresher
This is the approach to use to support load-balancing
1 parent 78b071f commit fc5086b

File tree

3 files changed

+33
-11
lines changed

3 files changed

+33
-11
lines changed
Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,39 @@
1-
using Umbraco.Core;
2-
using Umbraco.Core.Events;
3-
using Umbraco.Core.Models;
4-
using Umbraco.Core.Services;
1+
using System;
2+
using Newtonsoft.Json;
3+
using Our.Umbraco.NestedContent.Helpers;
4+
using Umbraco.Core;
5+
using Umbraco.Core.Cache;
6+
using Umbraco.Core.Sync;
7+
using Umbraco.Web.Cache;
58

69
namespace Our.Umbraco.NestedContent
710
{
811
public class Bootstrap : ApplicationEventHandler
912
{
1013
protected override void ApplicationStarted(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
1114
{
12-
DataTypeService.Saved += ExpireCache;
15+
CacheRefresherBase<DataTypeCacheRefresher>.CacheUpdated += DataTypeCacheRefresher_Updated;
1316
}
1417

15-
private void ExpireCache(IDataTypeService sender, SaveEventArgs<IDataTypeDefinition> e)
18+
private void DataTypeCacheRefresher_Updated(DataTypeCacheRefresher sender, CacheRefresherEventArgs e)
1619
{
17-
foreach (var dataType in e.SavedEntities)
20+
if (e.MessageType == MessageType.RefreshByJson)
1821
{
19-
ApplicationContext.Current.ApplicationCache.RuntimeCache.ClearCacheItem(
20-
string.Concat("Our.Umbraco.NestedContent.GetPreValuesCollectionByDataTypeId_", dataType.Id));
22+
var payload = JsonConvert.DeserializeObject<JsonPayload[]>((string)e.MessageObject);
23+
if (payload != null)
24+
{
25+
foreach (var item in payload)
26+
{
27+
NestedContentHelper.ClearCache(item.Id);
28+
}
29+
}
2130
}
2231
}
32+
33+
private class JsonPayload
34+
{
35+
public Guid UniqueId { get; set; }
36+
public int Id { get; set; }
37+
}
2338
}
2439
}

src/Our.Umbraco.NestedContent/Helpers/NestedContentHelper.cs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,23 @@ namespace Our.Umbraco.NestedContent.Helpers
1313
{
1414
internal static class NestedContentHelper
1515
{
16+
private const string CacheKeyPrefix = "Our.Umbraco.NestedContent.GetPreValuesCollectionByDataTypeId_";
17+
1618
public static PreValueCollection GetPreValuesCollectionByDataTypeId(int dtdId)
1719
{
1820
var preValueCollection = (PreValueCollection)ApplicationContext.Current.ApplicationCache.RuntimeCache.GetCacheItem(
19-
string.Concat("Our.Umbraco.NestedContent.GetPreValuesCollectionByDataTypeId_", dtdId),
21+
string.Concat(CacheKeyPrefix, dtdId),
2022
() => ApplicationContext.Current.Services.DataTypeService.GetPreValuesCollectionByDataTypeId(dtdId));
2123

2224
return preValueCollection;
2325
}
2426

27+
public static void ClearCache(int dtdId)
28+
{
29+
ApplicationContext.Current.ApplicationCache.RuntimeCache.ClearCacheItem(
30+
string.Concat(CacheKeyPrefix, dtdId));
31+
}
32+
2533
public static string GetContentTypeAliasFromItem(JObject item)
2634
{
2735
var contentTypeAliasProperty = item[NestedContentPropertyEditor.ContentTypeAliasPropertyKey];

src/Our.Umbraco.NestedContent/Our.Umbraco.NestedContent.csproj

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,6 @@
8686
<Compile Include="Extensions\PublishedPropertyTypeExtensions.cs" />
8787
<Compile Include="Converters\SingleNestedContentValueConverter.cs" />
8888
<Compile Include="Converters\NestedContentValueConverter.cs" />
89-
<Compile Include="Extensions\ContentTypeServiceExtensions.cs" />
9089
<Compile Include="Extensions\PreValueCollectionExtensions.cs" />
9190
<Compile Include="Helpers\NestedContentHelper.cs" />
9291
<Compile Include="Models\DetachedPublishedContent.cs" />

0 commit comments

Comments
 (0)