Skip to content

Commit

Permalink
Decoupled cache eviction from cache settings handler fixes #7958 (#7959)
Browse files Browse the repository at this point in the history
  • Loading branch information
carlwoodhouse authored and sebastienros committed Feb 1, 2018
1 parent aa96953 commit fcaaac3
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 23 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 @@ -107,6 +107,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 Expand Up @@ -208,4 +209,4 @@
</FlavorProperties>
</VisualStudio>
</ProjectExtensions>
</Project>
</Project>

0 comments on commit fcaaac3

Please sign in to comment.