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

Commit

Permalink
Replaced the DataType's cache expiry with the distributed cache refre…
Browse files Browse the repository at this point in the history
…sher

This is the approach to use to support load-balancing
  • Loading branch information
leekelleher committed Jun 5, 2017
1 parent 78b071f commit fc5086b
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 11 deletions.
33 changes: 24 additions & 9 deletions src/Our.Umbraco.NestedContent/Bootstrap.cs
Original file line number Diff line number Diff line change
@@ -1,24 +1,39 @@
using Umbraco.Core;
using Umbraco.Core.Events;
using Umbraco.Core.Models;
using Umbraco.Core.Services;
using System;
using Newtonsoft.Json;
using Our.Umbraco.NestedContent.Helpers;
using Umbraco.Core;
using Umbraco.Core.Cache;
using Umbraco.Core.Sync;
using Umbraco.Web.Cache;

namespace Our.Umbraco.NestedContent
{
public class Bootstrap : ApplicationEventHandler
{
protected override void ApplicationStarted(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
{
DataTypeService.Saved += ExpireCache;
CacheRefresherBase<DataTypeCacheRefresher>.CacheUpdated += DataTypeCacheRefresher_Updated;
}

private void ExpireCache(IDataTypeService sender, SaveEventArgs<IDataTypeDefinition> e)
private void DataTypeCacheRefresher_Updated(DataTypeCacheRefresher sender, CacheRefresherEventArgs e)
{
foreach (var dataType in e.SavedEntities)
if (e.MessageType == MessageType.RefreshByJson)
{
ApplicationContext.Current.ApplicationCache.RuntimeCache.ClearCacheItem(
string.Concat("Our.Umbraco.NestedContent.GetPreValuesCollectionByDataTypeId_", dataType.Id));
var payload = JsonConvert.DeserializeObject<JsonPayload[]>((string)e.MessageObject);
if (payload != null)
{
foreach (var item in payload)
{
NestedContentHelper.ClearCache(item.Id);
}
}
}
}

private class JsonPayload
{
public Guid UniqueId { get; set; }
public int Id { get; set; }
}
}
}
10 changes: 9 additions & 1 deletion src/Our.Umbraco.NestedContent/Helpers/NestedContentHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,23 @@ namespace Our.Umbraco.NestedContent.Helpers
{
internal static class NestedContentHelper
{
private const string CacheKeyPrefix = "Our.Umbraco.NestedContent.GetPreValuesCollectionByDataTypeId_";

public static PreValueCollection GetPreValuesCollectionByDataTypeId(int dtdId)
{
var preValueCollection = (PreValueCollection)ApplicationContext.Current.ApplicationCache.RuntimeCache.GetCacheItem(
string.Concat("Our.Umbraco.NestedContent.GetPreValuesCollectionByDataTypeId_", dtdId),
string.Concat(CacheKeyPrefix, dtdId),
() => ApplicationContext.Current.Services.DataTypeService.GetPreValuesCollectionByDataTypeId(dtdId));

return preValueCollection;
}

public static void ClearCache(int dtdId)
{
ApplicationContext.Current.ApplicationCache.RuntimeCache.ClearCacheItem(
string.Concat(CacheKeyPrefix, dtdId));
}

public static string GetContentTypeAliasFromItem(JObject item)
{
var contentTypeAliasProperty = item[NestedContentPropertyEditor.ContentTypeAliasPropertyKey];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,6 @@
<Compile Include="Extensions\PublishedPropertyTypeExtensions.cs" />
<Compile Include="Converters\SingleNestedContentValueConverter.cs" />
<Compile Include="Converters\NestedContentValueConverter.cs" />
<Compile Include="Extensions\ContentTypeServiceExtensions.cs" />
<Compile Include="Extensions\PreValueCollectionExtensions.cs" />
<Compile Include="Helpers\NestedContentHelper.cs" />
<Compile Include="Models\DetachedPublishedContent.cs" />
Expand Down

0 comments on commit fc5086b

Please sign in to comment.