diff --git a/src/Orchard.Web/Modules/Orchard.OutputCache/Filters/OutputCacheFilter.cs b/src/Orchard.Web/Modules/Orchard.OutputCache/Filters/OutputCacheFilter.cs index fab027621..080cee81e 100644 --- a/src/Orchard.Web/Modules/Orchard.OutputCache/Filters/OutputCacheFilter.cs +++ b/src/Orchard.Web/Modules/Orchard.OutputCache/Filters/OutputCacheFilter.cs @@ -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 _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 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(); + + 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().Name, StatusCode = response.StatusCode, - Tags = new[] { _invariantCacheKey }.Union(contentItemIds).ToArray(), + Tags = new[] { _invariantCacheKey }.Union(cacheItemTags.Distinct()).ToArray(), ETag = etag }; diff --git a/src/Orchard.Web/Modules/Orchard.OutputCache/Handlers/DisplayedContentItemHandler.cs b/src/Orchard.Web/Modules/Orchard.OutputCache/Handlers/DisplayedContentCacheTagProvider.cs similarity index 57% rename from src/Orchard.Web/Modules/Orchard.OutputCache/Handlers/DisplayedContentItemHandler.cs rename to src/Orchard.Web/Modules/Orchard.OutputCache/Handlers/DisplayedContentCacheTagProvider.cs index bc913f0cb..64bdc6b39 100644 --- a/src/Orchard.Web/Modules/Orchard.OutputCache/Handlers/DisplayedContentItemHandler.cs +++ b/src/Orchard.Web/Modules/Orchard.OutputCache/Handlers/DisplayedContentCacheTagProvider.cs @@ -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 { /// - /// Saves references to content items which have been displayed during a request + /// Creates tags for content items which have been displayed during a request /// - public class DisplayedContentItemHandler : ContentHandler, IDisplayedContentItemHandler { + public class DisplayedContentCacheTagProvider : ContentHandler, ICacheTagProvider { private readonly Collection _itemIds = new Collection(); protected override void BuildDisplayShape(BuildDisplayContext context) { _itemIds.Add(context.Content.Id); } - public bool IsDisplayed(int id) { - return _itemIds.Contains(id); - } - - public IEnumerable GetDisplayed() { - return _itemIds.Distinct(); + public IEnumerable GetTags() { + return _itemIds.Distinct().Select(id => id.ToString(CultureInfo.InvariantCulture)); } } } \ No newline at end of file diff --git a/src/Orchard.Web/Modules/Orchard.OutputCache/ICachingEventHandler.cs b/src/Orchard.Web/Modules/Orchard.OutputCache/ICachingEventHandler.cs index a2dbc640e..8246222cd 100644 --- a/src/Orchard.Web/Modules/Orchard.OutputCache/ICachingEventHandler.cs +++ b/src/Orchard.Web/Modules/Orchard.OutputCache/ICachingEventHandler.cs @@ -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 { diff --git a/src/Orchard.Web/Modules/Orchard.OutputCache/Orchard.OutputCache.csproj b/src/Orchard.Web/Modules/Orchard.OutputCache/Orchard.OutputCache.csproj index d96d2a44a..a37b05b31 100644 --- a/src/Orchard.Web/Modules/Orchard.OutputCache/Orchard.OutputCache.csproj +++ b/src/Orchard.Web/Modules/Orchard.OutputCache/Orchard.OutputCache.csproj @@ -128,7 +128,7 @@ - + @@ -144,7 +144,7 @@ - + diff --git a/src/Orchard.Web/Modules/Orchard.OutputCache/Services/ICacheTagProvider.cs b/src/Orchard.Web/Modules/Orchard.OutputCache/Services/ICacheTagProvider.cs new file mode 100644 index 000000000..bd869acd6 --- /dev/null +++ b/src/Orchard.Web/Modules/Orchard.OutputCache/Services/ICacheTagProvider.cs @@ -0,0 +1,7 @@ +using System.Collections.Generic; + +namespace Orchard.OutputCache.Services { + public interface ICacheTagProvider : IDependency { + IEnumerable GetTags(); + } +} \ No newline at end of file diff --git a/src/Orchard.Web/Modules/Orchard.OutputCache/Services/IDisplayedContentItemHandler.cs b/src/Orchard.Web/Modules/Orchard.OutputCache/Services/IDisplayedContentItemHandler.cs deleted file mode 100644 index 7766e269b..000000000 --- a/src/Orchard.Web/Modules/Orchard.OutputCache/Services/IDisplayedContentItemHandler.cs +++ /dev/null @@ -1,9 +0,0 @@ -using System.Collections.Generic; -using Orchard; - -namespace Orchard.OutputCache.Services { - public interface IDisplayedContentItemHandler : IDependency { - bool IsDisplayed(int id); - IEnumerable GetDisplayed(); - } -} \ No newline at end of file