Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Decoupled cache eviction from cache settings handler fixes #7958 #7959

Merged
merged 2 commits into from Feb 1, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
@@ -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>