forked from OrchardCMS/Orchard
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Decoupled cache eviction from cache settings handler OrchardCMS#7958
- Loading branch information
Carl Woodhouse
committed
Feb 1, 2018
1 parent
aa96953
commit b5c26b8
Showing
3 changed files
with
36 additions
and
22 deletions.
There are no files selected for viewing
33 changes: 33 additions & 0 deletions
33
src/Orchard.Web/Modules/Orchard.OutputCache/Handlers/CacheItemInvalidationHandler.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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)); | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters