Skip to content

Commit

Permalink
Decoupled cache eviction from cache settings handler OrchardCMS#7958
Browse files Browse the repository at this point in the history
  • Loading branch information
Carl Woodhouse committed Feb 1, 2018
1 parent aa96953 commit b5c26b8
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 22 deletions.
@@ -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<IContent>((context, part) => Invalidate(part));
OnRemoved<IContent>((context, part) => Invalidate(part));
OnDestroyed<IContent>((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<CommonPart>();
if (commonPart != null) {
if (commonPart.Container != null) {
_cacheService.RemoveByTag(commonPart.Container.Id.ToString(CultureInfo.InvariantCulture));
}
}
}
}
}
@@ -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;

Expand All @@ -23,28 +20,10 @@ public class CacheSettingsPartHandler : ContentHandler {
part.DefaultCacheGraceTime = 60;
});

// Evict cached content when updated, removed or destroyed.
OnPublished<IContent>((context, part) => Invalidate(part));
OnRemoved<IContent>((context, part) => Invalidate(part));
OnDestroyed<IContent>((context, part) => Invalidate(part));

OnExporting<CacheSettingsPart>(ExportRouteSettings);
OnImporting<CacheSettingsPart>(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<CommonPart>();
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",
Expand Down
Expand Up @@ -26,6 +26,7 @@
<IISExpressWindowsAuthentication />
<IISExpressUseClassicPipelineMode />
<UseGlobalApplicationHostFile />
<Use64BitIISExpress />
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
Expand Down Expand Up @@ -107,6 +108,7 @@
<Content Include="Web.config" />
<Content Include="Scripts\Web.config" />
<Content Include="Styles\Web.config" />
<Compile Include="Handlers\CacheItemInvalidationHandler.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Content Include="Module.txt" />
</ItemGroup>
Expand Down

0 comments on commit b5c26b8

Please sign in to comment.