From b5c26b81da68b8e16ee12a313c6f8de57a744a5d Mon Sep 17 00:00:00 2001 From: Carl Woodhouse Date: Thu, 1 Feb 2018 15:33:23 +0000 Subject: [PATCH] Decoupled cache eviction from cache settings handler #7958 --- .../Handlers/CacheItemInvalidationHandler.cs | 33 +++++++++++++++++++ .../Handlers/CacheSettingsPartHandler.cs | 23 +------------ .../Orchard.OutputCache.csproj | 2 ++ 3 files changed, 36 insertions(+), 22 deletions(-) create mode 100644 src/Orchard.Web/Modules/Orchard.OutputCache/Handlers/CacheItemInvalidationHandler.cs diff --git a/src/Orchard.Web/Modules/Orchard.OutputCache/Handlers/CacheItemInvalidationHandler.cs b/src/Orchard.Web/Modules/Orchard.OutputCache/Handlers/CacheItemInvalidationHandler.cs new file mode 100644 index 00000000000..0850dd716b5 --- /dev/null +++ b/src/Orchard.Web/Modules/Orchard.OutputCache/Handlers/CacheItemInvalidationHandler.cs @@ -0,0 +1,33 @@ +using System.Globalization; +using Orchard.ContentManagement; +using Orchard.ContentManagement.Handlers; +using Orchard.Core.Common.Models; +using Orchard.OutputCache.Services; + +namespace Orchard.OutputCache.Handlers { + public class CacheItemInvalidationHandler : ContentHandler { + private readonly ICacheService _cacheService; + + public CacheItemInvalidationHandler(ICacheService cacheService) { + _cacheService = cacheService; + + // Evict cached content when updated, removed or destroyed. + OnPublished((context, part) => Invalidate(part)); + OnRemoved((context, part) => Invalidate(part)); + OnDestroyed((context, part) => Invalidate(part)); + } + + private void Invalidate(IContent content) { + // Remove any item tagged with this content item ID. + _cacheService.RemoveByTag(content.ContentItem.Id.ToString(CultureInfo.InvariantCulture)); + + // Search the cache for containers too. + var commonPart = content.As(); + if (commonPart != null) { + if (commonPart.Container != null) { + _cacheService.RemoveByTag(commonPart.Container.Id.ToString(CultureInfo.InvariantCulture)); + } + } + } + } +} \ No newline at end of file diff --git a/src/Orchard.Web/Modules/Orchard.OutputCache/Handlers/CacheSettingsPartHandler.cs b/src/Orchard.Web/Modules/Orchard.OutputCache/Handlers/CacheSettingsPartHandler.cs index 6a0eff1f38e..423d387adc1 100644 --- a/src/Orchard.Web/Modules/Orchard.OutputCache/Handlers/CacheSettingsPartHandler.cs +++ b/src/Orchard.Web/Modules/Orchard.OutputCache/Handlers/CacheSettingsPartHandler.cs @@ -1,10 +1,7 @@ -using System; -using System.Globalization; -using System.Linq; +using System.Linq; using System.Xml.Linq; using Orchard.ContentManagement; using Orchard.ContentManagement.Handlers; -using Orchard.Core.Common.Models; using Orchard.OutputCache.Models; using Orchard.OutputCache.Services; @@ -23,28 +20,10 @@ public class CacheSettingsPartHandler : ContentHandler { part.DefaultCacheGraceTime = 60; }); - // Evict cached content when updated, removed or destroyed. - OnPublished((context, part) => Invalidate(part)); - OnRemoved((context, part) => Invalidate(part)); - OnDestroyed((context, part) => Invalidate(part)); - OnExporting(ExportRouteSettings); OnImporting(ImportRouteSettings); } - private void Invalidate(IContent content) { - // Remove any item tagged with this content item ID. - _cacheService.RemoveByTag(content.ContentItem.Id.ToString(CultureInfo.InvariantCulture)); - - // Search the cache for containers too. - var commonPart = content.As(); - if (commonPart != null) { - if (commonPart.Container != null) { - _cacheService.RemoveByTag(commonPart.Container.Id.ToString(CultureInfo.InvariantCulture)); - } - } - } - private void ExportRouteSettings(ExportContentContext context, CacheSettingsPart part) { var routes = _cacheService.GetRouteConfigs(); var routesElement = new XElement("Routes", diff --git a/src/Orchard.Web/Modules/Orchard.OutputCache/Orchard.OutputCache.csproj b/src/Orchard.Web/Modules/Orchard.OutputCache/Orchard.OutputCache.csproj index 3d4a6cf67c2..171f0342d90 100644 --- a/src/Orchard.Web/Modules/Orchard.OutputCache/Orchard.OutputCache.csproj +++ b/src/Orchard.Web/Modules/Orchard.OutputCache/Orchard.OutputCache.csproj @@ -26,6 +26,7 @@ + true @@ -107,6 +108,7 @@ +