forked from OrchardCMS/Orchard
/
CacheItemInvalidationHandler.cs
33 lines (28 loc) · 1.3 KB
/
CacheItemInvalidationHandler.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
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));
}
}
}
}
}