Skip to content

Instantly share code, notes, and snippets.

@ryanlewis
Created July 27, 2016 12:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ryanlewis/3b3266316765caa9c74be981cf9dcd2a to your computer and use it in GitHub Desktop.
Save ryanlewis/3b3266316765caa9c74be981cf9dcd2a to your computer and use it in GitHub Desktop.
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
Copy link
Author

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