Skip to content

Instantly share code, notes, and snippets.

@kgiszewski
Created June 26, 2014 13:31
Show Gist options
  • Save kgiszewski/561e2ae89bb51c6ce723 to your computer and use it in GitHub Desktop.
Save kgiszewski/561e2ae89bb51c6ce723 to your computer and use it in GitHub Desktop.
Expire Cache
using System;
using System.Web;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Umbraco.Core;
using Umbraco.Core.Publishing;
using Umbraco.Core.Logging;
namespace PioneerPump.Umbraco.Extensions.Events
{
public class UpdateHelpers : ApplicationEventHandler
{
protected override void ApplicationStarting(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
{
base.ApplicationStarting(umbracoApplication, applicationContext);
PublishingStrategy.Published += PublishingStrategy_Published;
}
void PublishingStrategy_UnPublished(IPublishingStrategy sender, global::Umbraco.Core.Events.PublishEventArgs<IContent> e)
{
ExpireCache(e.PublishedEntities);
}
void ContentService_Deleted(IContentService sender, global::Umbraco.Core.Events.DeleteEventArgs<IContent> e)
{
ExpireCache(e.DeletedEntities);
}
void PublishingStrategy_Published(IPublishingStrategy sender, global::Umbraco.Core.Events.PublishEventArgs<global::Umbraco.Core.Models.IContent> e)
{
ExpireCache(e.PublishedEntities);
}
void ExpireCache(IEnumerable<IContent> nodes) {
foreach (var node in e.PublishedEntities)
{
//clear the helper if published or one of their children
if (Helpers.Settings.Instance != null && (node.ContentType.Alias == "Settings" || node.Path.Contains("-1," + Helpers.Settings.Instance.Id)))
{
Helpers.Settings.Instance = null;
LogHelper.Info<UpdateHelpers>("Clearing Settings Helper");
}
if (Helpers.Products.Instance != null && (node.ContentType.Alias == "RootProductFolder" || node.Path.Contains("-1,1072," + Helpers.Products.Instance.Id)))
{
Helpers.Products.Instance = null;
LogHelper.Info<UpdateHelpers>("Clearing Products Helper");
}
if (Helpers.News.Instance != null && (node.ContentType.Alias == "NewsFolder" || node.Path.Contains("-1,1072," + Helpers.News.Instance.Id)))
{
Helpers.News.Instance = null;
LogHelper.Info<UpdateHelpers>("Clearing News Helper");
}
if (Helpers.Events.Instance != null && (node.ContentType.Alias == "EventsFolder" || node.Path.Contains("-1,1072," + Helpers.Events.Instance.Id)))
{
Helpers.Events.Instance = null;
LogHelper.Info<UpdateHelpers>("Clearing Events Helper");
}
if (Helpers.ProductFeatures.Instance != null && (node.ContentType.Alias == "ProductFeaturesFolder" || node.Path.Contains("-1,1072," + Helpers.ProductFeatures.Instance.Id)))
{
Helpers.ProductFeatures.Instance = null;
LogHelper.Info<UpdateHelpers>("Clearing ProductFeatures Helper");
}
if (Helpers.Site.Instance != null && (node.ContentType.Alias == "SitePage" || node.Path.Contains("-1," + Helpers.Site.Instance.Id)))
{
Helpers.Site.Instance = null;
LogHelper.Info<UpdateHelpers>("Site Helper");
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment