Skip to content

Commit

Permalink
Removes IDisplayedContentItemHandler in favour of ICacheTagProvider (O…
Browse files Browse the repository at this point in the history
  • Loading branch information
Chris Payne authored and sebastienros committed Aug 3, 2017
1 parent 5ce43b6 commit 7373c11
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
using Orchard.OutputCache.Models;
using Orchard.OutputCache.Services;
using Orchard.Services;
using Orchard.Themes;
using Orchard.UI.Admin;
using Orchard.Utility.Extensions;

Expand All @@ -33,14 +32,12 @@ public class OutputCacheFilter : FilterProvider, IActionFilter, IResultFilter, I
// 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;

Expand All @@ -49,26 +46,22 @@ public class OutputCacheFilter : FilterProvider, IActionFilter, IResultFilter, I
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;
Expand Down Expand Up @@ -206,8 +199,17 @@ public void OnResultExecuted(ResultExecutedContext filterContext) {
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;
Expand Down Expand Up @@ -248,7 +250,7 @@ public void OnResultExecuted(ResultExecutedContext filterContext) {
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
};

Expand Down
Original file line number Diff line number Diff line change
@@ -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));
}
}
}
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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" />
Expand All @@ -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" />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
using System.Collections.Generic;

namespace Orchard.OutputCache.Services {
public interface ICacheTagProvider : IDependency {
IEnumerable<string> GetTags();
}
}

This file was deleted.

0 comments on commit 7373c11

Please sign in to comment.