Removes IDisplayedContentItemHandler in favour of ICacheTagProvider (#7793)

Fixes #7792
This commit is contained in:
Chris Payne
2017-08-03 20:18:47 +01:00
committed by Sébastien Ros
parent 5ce43b6827
commit 7373c11a1e
6 changed files with 29 additions and 36 deletions

View File

@@ -20,7 +20,6 @@ using Orchard.OutputCache.Helpers;
using Orchard.OutputCache.Models;
using Orchard.OutputCache.Services;
using Orchard.Services;
using Orchard.Themes;
using Orchard.UI.Admin;
using Orchard.Utility.Extensions;
@@ -33,14 +32,12 @@ namespace Orchard.OutputCache.Filters {
// Dependencies.
private readonly ICacheManager _cacheManager;
private readonly IOutputCacheStorageProvider _cacheStorageProvider;
private readonly ITagCache _tagCache;
private readonly IDisplayedContentItemHandler _displayedContentItemHandler;
private readonly IWorkContextAccessor _workContextAccessor;
private readonly IThemeManager _themeManager;
private readonly IClock _clock;
private readonly ICacheService _cacheService;
private readonly ISignals _signals;
private readonly ShellSettings _shellSettings;
private readonly IEnumerable<ICacheTagProvider> _cacheTagProviders;
private readonly ICachingEventHandler _cachingEvents;
private bool _isDisposed = false;
@@ -49,26 +46,22 @@ namespace Orchard.OutputCache.Filters {
public OutputCacheFilter(
ICacheManager cacheManager,
IOutputCacheStorageProvider cacheStorageProvider,
ITagCache tagCache,
IDisplayedContentItemHandler displayedContentItemHandler,
IWorkContextAccessor workContextAccessor,
IThemeManager themeManager,
IClock clock,
ICacheService cacheService,
ISignals signals,
ShellSettings shellSettings,
IEnumerable<ICacheTagProvider> cacheTagProviders,
ICachingEventHandler cachingEvents) {
_cacheManager = cacheManager;
_cacheStorageProvider = cacheStorageProvider;
_tagCache = tagCache;
_displayedContentItemHandler = displayedContentItemHandler;
_workContextAccessor = workContextAccessor;
_themeManager = themeManager;
_clock = clock;
_cacheService = cacheService;
_signals = signals;
_shellSettings = shellSettings;
_cacheTagProviders = cacheTagProviders;
_cachingEvents = cachingEvents;
Logger = NullLogger.Instance;
@@ -206,8 +199,17 @@ namespace Orchard.OutputCache.Filters {
var cacheDuration = _cacheRouteConfig != null && _cacheRouteConfig.Duration.HasValue ? _cacheRouteConfig.Duration.Value : CacheSettings.DefaultCacheDuration;
var cacheGraceTime = _cacheRouteConfig != null && _cacheRouteConfig.GraceTime.HasValue ? _cacheRouteConfig.GraceTime.Value : CacheSettings.DefaultCacheGraceTime;
// Include each content item ID as tags for the cache entry.
var contentItemIds = _displayedContentItemHandler.GetDisplayed().Select(x => x.ToString(CultureInfo.InvariantCulture)).ToArray();
// Get the tags for this cache item from.
var cacheItemTags = new List<string>();
foreach (var cacheTagProvider in _cacheTagProviders) {
try {
cacheItemTags.AddRange(cacheTagProvider.GetTags());
}
catch (Exception ex) {
Logger.Warning(ex, "Cache tags from provider {0} will not be added to the cached item ({1}) because the provider threw an exception when asked to provide tags.", cacheTagProvider.GetType().FullName, _cacheKey);
}
}
// Capture the response output using a custom filter stream.
var response = filterContext.HttpContext.Response;
@@ -248,7 +250,7 @@ namespace Orchard.OutputCache.Filters {
Url = filterContext.HttpContext.Request.Url.AbsolutePath,
Tenant = scope.Resolve<ShellSettings>().Name,
StatusCode = response.StatusCode,
Tags = new[] { _invariantCacheKey }.Union(contentItemIds).ToArray(),
Tags = new[] { _invariantCacheKey }.Union(cacheItemTags.Distinct()).ToArray(),
ETag = etag
};

View File

@@ -1,26 +1,23 @@
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Globalization;
using System.Linq;
using Orchard.OutputCache.Services;
using Orchard.ContentManagement.Handlers;
namespace Orchard.OutputCache.Handlers {
/// <summary>
/// Saves references to content items which have been displayed during a request
/// Creates tags for content items which have been displayed during a request
/// </summary>
public class DisplayedContentItemHandler : ContentHandler, IDisplayedContentItemHandler {
public class DisplayedContentCacheTagProvider : ContentHandler, ICacheTagProvider {
private readonly Collection<int> _itemIds = new Collection<int>();
protected override void BuildDisplayShape(BuildDisplayContext context) {
_itemIds.Add(context.Content.Id);
}
public bool IsDisplayed(int id) {
return _itemIds.Contains(id);
}
public IEnumerable<int> GetDisplayed() {
return _itemIds.Distinct();
public IEnumerable<string> GetTags() {
return _itemIds.Distinct().Select(id => id.ToString(CultureInfo.InvariantCulture));
}
}
}

View File

@@ -1,9 +1,5 @@
using Orchard.Events;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
namespace Orchard.OutputCache {
public interface ICachingEventHandler : IEventHandler {

View File

@@ -128,7 +128,7 @@
<Compile Include="Filters\CaptureStream.cs" />
<Compile Include="Filters\OutputCacheFilter.cs" />
<Compile Include="Handlers\CacheSettingsPartHandler.cs" />
<Compile Include="Handlers\DisplayedContentItemHandler.cs" />
<Compile Include="Handlers\DisplayedContentCacheTagProvider.cs" />
<Compile Include="DatabaseOutputCacheMigrations.cs" />
<Compile Include="Helpers\OutputCacheAttributeExtensions.cs" />
<Compile Include="Migrations.cs" />
@@ -144,7 +144,7 @@
<Compile Include="Services\DefaultCacheControlStrategy.cs" />
<Compile Include="Services\DefaultTagCache.cs" />
<Compile Include="Services\ICacheControlStrategy.cs" />
<Compile Include="Services\IDisplayedContentItemHandler.cs" />
<Compile Include="Services\ICacheTagProvider.cs" />
<Compile Include="Services\ITagCache.cs" />
<Compile Include="Services\DefaultCacheStorageProvider.cs" />
<Compile Include="Services\ICacheService.cs" />

View File

@@ -0,0 +1,7 @@
using System.Collections.Generic;
namespace Orchard.OutputCache.Services {
public interface ICacheTagProvider : IDependency {
IEnumerable<string> GetTags();
}
}

View File

@@ -1,9 +0,0 @@
using System.Collections.Generic;
using Orchard;
namespace Orchard.OutputCache.Services {
public interface IDisplayedContentItemHandler : IDependency {
bool IsDisplayed(int id);
IEnumerable<int> GetDisplayed();
}
}