Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Using Umbraco Contour in an anonymous load balanced scenario (v7.3+)
public class ContourFormRefreshPayload
{
public Guid Id { get; set; }
}
public class ContourFormCacheRefresher : JsonCacheRefresherBase<ContourFormCacheRefresher>
{
public static string Id = "AA2970FD-8785-42C2-A289-A7A6614CAE45";
protected override ContourFormCacheRefresher Instance => this;
public override Guid UniqueIdentifier => new Guid(Id);
public override string Name => "Clears Contour form cache";
public override void Refresh(string json)
{
// ignore the payload for now - we're just wiping the form cache completely
LogHelper.Info<ContourFormCacheRefresher>(() => "Resaving Contour form because of distributed cache instruction");
Umbraco.Forms.Core.Services.CacheService.ClearAllCache();
}
internal static ContourFormRefreshPayload[] DeserializeFromJsonPayload(string json)
{
var serializer = new JavaScriptSerializer();
return serializer.Deserialize<ContourFormRefreshPayload[]>(json);
}
internal static string SerializeToJsonPayload(params ContourFormRefreshPayload[] payloads)
{
var serializer = new JavaScriptSerializer();
var json = serializer.Serialize(payloads);
return json;
}
}
public class ContourFormSync : ApplicationEventHandler
{
protected override void ApplicationStarted(UmbracoApplicationBase umbracoApplication,
ApplicationContext applicationContext)
{
// listen to contour forms being saved
Umbraco.Forms.Data.Storage.FormStorage.FormCreated += DistributeCacheInstruction;
Umbraco.Forms.Data.Storage.FormStorage.FormUpdated += DistributeCacheInstruction;
}
internal static void DistributeCacheInstruction(object sender, FormEventArgs e)
{
var refresherGuid = Guid.Parse(ContourFormCacheRefresher.Id);
var payload = ContourFormCacheRefresher.SerializeToJsonPayload(new ContourFormRefreshPayload {Id = e.Form.Id});
DistributedCache.Instance.RefreshByJson(refresherGuid, payload);
}
}
@ryanlewis

This comment has been minimized.

Copy link
Owner Author

ryanlewis commented Jul 27, 2016

Yes, we're using Contour - this is an old project and bumping the version is a bit of a leap.

We updated to Umbraco 7.4, and each of the servers were not being made aware of changes to forms within the Umbraco back office. This POC will distribute cache instructions to other servers which will clear the internal Umbraco Contour cache when a form is updated.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.