2013-03-16 08:47:55 +06:00
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
|
using Umbraco.Core;
|
|
|
|
|
|
using Umbraco.Core.Cache;
|
|
|
|
|
|
using Umbraco.Core.Models;
|
2013-03-19 23:49:20 +06:00
|
|
|
|
using Umbraco.Core.Models.EntityBase;
|
2013-02-06 09:53:13 +06:00
|
|
|
|
using Umbraco.Core.Services;
|
|
|
|
|
|
using umbraco;
|
2013-02-12 04:13:29 +06:00
|
|
|
|
using umbraco.BusinessLogic;
|
2013-02-06 09:53:13 +06:00
|
|
|
|
using umbraco.cms.businesslogic;
|
|
|
|
|
|
using umbraco.cms.businesslogic.member;
|
|
|
|
|
|
using System.Linq;
|
2013-03-16 08:47:55 +06:00
|
|
|
|
using Macro = umbraco.cms.businesslogic.macro.Macro;
|
|
|
|
|
|
using Template = umbraco.cms.businesslogic.template.Template;
|
2013-02-06 09:53:13 +06:00
|
|
|
|
|
|
|
|
|
|
namespace Umbraco.Web.Cache
|
|
|
|
|
|
{
|
|
|
|
|
|
/// <summary>
|
2013-02-07 04:45:05 +06:00
|
|
|
|
/// Class which listens to events on business level objects in order to invalidate the cache amongst servers when data changes
|
2013-02-06 09:53:13 +06:00
|
|
|
|
/// </summary>
|
2013-02-07 04:45:05 +06:00
|
|
|
|
public class CacheRefresherEventHandler : ApplicationEventHandler
|
2013-02-06 09:53:13 +06:00
|
|
|
|
{
|
|
|
|
|
|
protected override void ApplicationStarted(UmbracoApplicationBase umbracoApplication, ApplicationContext applicationContext)
|
|
|
|
|
|
{
|
|
|
|
|
|
if (UmbracoSettings.UmbracoLibraryCacheDuration <= 0) return;
|
|
|
|
|
|
|
2013-03-16 01:37:05 +06:00
|
|
|
|
//Bind to content events - currently used for:
|
|
|
|
|
|
// - macro clearing
|
|
|
|
|
|
// - clearing the xslt cache (MS.Internal.Xml.XPath.XPathSelectionIterator)
|
|
|
|
|
|
//NOTE: These are 'special' event handlers that will only clear cache for items on the current server
|
|
|
|
|
|
// that is because this event will fire based on a distributed cache call, meaning this event fires on
|
|
|
|
|
|
// all servers based on the distributed cache call for updating content.
|
2013-03-16 08:47:55 +06:00
|
|
|
|
content.AfterUpdateDocumentCache += ContentAfterUpdateDocumentCache;
|
|
|
|
|
|
content.AfterClearDocumentCache += ContentAfterClearDocumentCache;
|
|
|
|
|
|
|
|
|
|
|
|
//Bind to content type events
|
|
|
|
|
|
|
|
|
|
|
|
ContentTypeService.SavedContentType += ContentTypeServiceSavedContentType;
|
|
|
|
|
|
ContentTypeService.SavedMediaType += ContentTypeServiceSavedMediaType;
|
2013-03-12 22:58:21 +04:00
|
|
|
|
|
2013-02-12 04:13:29 +06:00
|
|
|
|
//Bind to user events
|
|
|
|
|
|
|
|
|
|
|
|
User.Saving += UserSaving;
|
|
|
|
|
|
User.Deleting += UserDeleting;
|
|
|
|
|
|
|
2013-02-07 05:26:53 +06:00
|
|
|
|
//Bind to template events
|
|
|
|
|
|
|
|
|
|
|
|
Template.AfterSave += TemplateAfterSave;
|
|
|
|
|
|
Template.AfterDelete += TemplateAfterDelete;
|
|
|
|
|
|
|
2013-02-07 04:45:05 +06:00
|
|
|
|
//Bind to macro events
|
|
|
|
|
|
|
|
|
|
|
|
Macro.AfterSave += MacroAfterSave;
|
2013-02-07 05:26:53 +06:00
|
|
|
|
Macro.AfterDelete += MacroAfterDelete;
|
2013-02-07 04:45:05 +06:00
|
|
|
|
|
|
|
|
|
|
//Bind to member events
|
2013-02-06 09:53:13 +06:00
|
|
|
|
|
|
|
|
|
|
Member.AfterSave += MemberAfterSave;
|
|
|
|
|
|
Member.BeforeDelete += MemberBeforeDelete;
|
|
|
|
|
|
|
2013-02-07 04:45:05 +06:00
|
|
|
|
//Bind to media events
|
|
|
|
|
|
|
2013-02-06 09:53:13 +06:00
|
|
|
|
MediaService.Saved += MediaServiceSaved;
|
2013-02-07 02:19:51 +06:00
|
|
|
|
//We need to perform all of the 'before' events here because we need a reference to the
|
|
|
|
|
|
//media item's Path before it is moved/deleting/trashed
|
|
|
|
|
|
//see: http://issues.umbraco.org/issue/U4-1653
|
2013-02-06 09:53:13 +06:00
|
|
|
|
MediaService.Deleting += MediaServiceDeleting;
|
2013-02-07 02:19:51 +06:00
|
|
|
|
MediaService.Moving += MediaServiceMoving;
|
|
|
|
|
|
MediaService.Trashing += MediaServiceTrashing;
|
2013-02-06 09:53:13 +06:00
|
|
|
|
}
|
|
|
|
|
|
|
2013-03-16 08:47:55 +06:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Fires when a media type is saved
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="sender"></param>
|
|
|
|
|
|
/// <param name="e"></param>
|
|
|
|
|
|
static void ContentTypeServiceSavedMediaType(IContentTypeService sender, Core.Events.SaveEventArgs<IMediaType> e)
|
|
|
|
|
|
{
|
|
|
|
|
|
e.SavedEntities.ForEach(x => DistributedCache.Instance.RemoveMediaTypeCache(x));
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Fires when a content type is saved
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="sender"></param>
|
|
|
|
|
|
/// <param name="e"></param>
|
|
|
|
|
|
static void ContentTypeServiceSavedContentType(IContentTypeService sender, Core.Events.SaveEventArgs<IContentType> e)
|
2013-03-20 22:03:35 +06:00
|
|
|
|
{
|
2013-03-19 23:49:20 +06:00
|
|
|
|
e.SavedEntities.ForEach(contentType => DistributedCache.Instance.RemoveContentTypeCache(contentType));
|
2013-03-16 08:47:55 +06:00
|
|
|
|
}
|
|
|
|
|
|
|
2013-03-12 22:58:21 +04:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Fires after the document cache has been cleared for a particular document
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="sender"></param>
|
|
|
|
|
|
/// <param name="e"></param>
|
2013-03-16 08:47:55 +06:00
|
|
|
|
static void ContentAfterClearDocumentCache(global::umbraco.cms.businesslogic.web.Document sender, DocumentCacheEventArgs e)
|
2013-03-12 22:58:21 +04:00
|
|
|
|
{
|
2013-03-16 01:37:05 +06:00
|
|
|
|
DistributedCache.Instance.ClearAllMacroCacheOnCurrentServer();
|
|
|
|
|
|
DistributedCache.Instance.ClearXsltCacheOnCurrentServer();
|
2013-03-12 22:58:21 +04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Fires after the document cache has been updated for a particular document
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="sender"></param>
|
|
|
|
|
|
/// <param name="e"></param>
|
2013-03-16 08:47:55 +06:00
|
|
|
|
static void ContentAfterUpdateDocumentCache(global::umbraco.cms.businesslogic.web.Document sender, DocumentCacheEventArgs e)
|
2013-03-12 22:58:21 +04:00
|
|
|
|
{
|
2013-03-16 01:37:05 +06:00
|
|
|
|
DistributedCache.Instance.ClearAllMacroCacheOnCurrentServer();
|
|
|
|
|
|
DistributedCache.Instance.ClearXsltCacheOnCurrentServer();
|
2013-03-12 22:58:21 +04:00
|
|
|
|
}
|
|
|
|
|
|
|
2013-02-12 04:13:29 +06:00
|
|
|
|
static void UserDeleting(User sender, System.EventArgs e)
|
|
|
|
|
|
{
|
|
|
|
|
|
DistributedCache.Instance.RemoveUserCache(sender.Id);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void UserSaving(User sender, System.EventArgs e)
|
|
|
|
|
|
{
|
|
|
|
|
|
DistributedCache.Instance.RefreshUserCache(sender.Id);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2013-02-07 05:26:53 +06:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Removes cache for template
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="sender"></param>
|
|
|
|
|
|
/// <param name="e"></param>
|
|
|
|
|
|
static void TemplateAfterDelete(Template sender, DeleteEventArgs e)
|
|
|
|
|
|
{
|
|
|
|
|
|
DistributedCache.Instance.RemoveTemplateCache(sender.Id);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Refresh cache for template
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="sender"></param>
|
|
|
|
|
|
/// <param name="e"></param>
|
|
|
|
|
|
static void TemplateAfterSave(Template sender, SaveEventArgs e)
|
|
|
|
|
|
{
|
|
|
|
|
|
DistributedCache.Instance.RefreshTemplateCache(sender.Id);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Flush macro from cache
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="sender"></param>
|
|
|
|
|
|
/// <param name="e"></param>
|
|
|
|
|
|
static void MacroAfterDelete(Macro sender, DeleteEventArgs e)
|
|
|
|
|
|
{
|
2013-03-12 03:00:42 +04:00
|
|
|
|
DistributedCache.Instance.RemoveMacroCache(sender);
|
2013-02-07 05:26:53 +06:00
|
|
|
|
}
|
|
|
|
|
|
|
2013-02-07 04:45:05 +06:00
|
|
|
|
/// <summary>
|
|
|
|
|
|
/// Flush macro from cache
|
|
|
|
|
|
/// </summary>
|
|
|
|
|
|
/// <param name="sender"></param>
|
|
|
|
|
|
/// <param name="e"></param>
|
|
|
|
|
|
static void MacroAfterSave(Macro sender, SaveEventArgs e)
|
|
|
|
|
|
{
|
2013-03-12 03:00:42 +04:00
|
|
|
|
DistributedCache.Instance.RefreshMacroCache(sender);
|
2013-02-07 04:45:05 +06:00
|
|
|
|
}
|
|
|
|
|
|
|
2013-02-07 02:19:51 +06:00
|
|
|
|
static void MediaServiceTrashing(IMediaService sender, Core.Events.MoveEventArgs<Core.Models.IMedia> e)
|
2013-02-06 09:53:13 +06:00
|
|
|
|
{
|
2013-02-12 07:35:47 +06:00
|
|
|
|
DistributedCache.Instance.RemoveMediaCache(e.Entity);
|
2013-02-06 09:53:13 +06:00
|
|
|
|
}
|
|
|
|
|
|
|
2013-02-07 02:19:51 +06:00
|
|
|
|
static void MediaServiceMoving(IMediaService sender, Core.Events.MoveEventArgs<Core.Models.IMedia> e)
|
2013-02-06 09:53:13 +06:00
|
|
|
|
{
|
2013-02-12 07:35:47 +06:00
|
|
|
|
DistributedCache.Instance.RefreshMediaCache(e.Entity);
|
2013-02-06 09:53:13 +06:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void MediaServiceDeleting(IMediaService sender, Core.Events.DeleteEventArgs<Core.Models.IMedia> e)
|
|
|
|
|
|
{
|
2013-02-12 07:35:47 +06:00
|
|
|
|
DistributedCache.Instance.RemoveMediaCache(e.DeletedEntities.ToArray());
|
2013-02-06 09:53:13 +06:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void MediaServiceSaved(IMediaService sender, Core.Events.SaveEventArgs<Core.Models.IMedia> e)
|
|
|
|
|
|
{
|
2013-02-12 07:35:47 +06:00
|
|
|
|
DistributedCache.Instance.RefreshMediaCache(e.SavedEntities.ToArray());
|
2013-02-06 09:53:13 +06:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void MemberBeforeDelete(Member sender, DeleteEventArgs e)
|
|
|
|
|
|
{
|
2013-02-07 05:53:59 +06:00
|
|
|
|
DistributedCache.Instance.RemoveMemberCache(sender.Id);
|
2013-02-06 09:53:13 +06:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void MemberAfterSave(Member sender, SaveEventArgs e)
|
|
|
|
|
|
{
|
2013-02-07 04:57:44 +06:00
|
|
|
|
DistributedCache.Instance.RefreshMemberCache(sender.Id);
|
2013-02-06 09:53:13 +06:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|