Output cache route config fixes #6523

This commit is contained in:
Carl Woodhouse
2016-03-10 20:31:55 +00:00
parent 26e2fe9977
commit 31ed21b061

View File

@@ -71,6 +71,7 @@ namespace Orchard.OutputCache.Filters {
// State. // State.
private CacheSettings _cacheSettings; private CacheSettings _cacheSettings;
private CacheRouteConfig _cacheRouteConfig;
private DateTime _now; private DateTime _now;
private WorkContext _workContext; private WorkContext _workContext;
private string _cacheKey; private string _cacheKey;
@@ -92,6 +93,13 @@ namespace Orchard.OutputCache.Filters {
_now = _clock.UtcNow; _now = _clock.UtcNow;
_workContext = _workContextAccessor.GetContext(); _workContext = _workContextAccessor.GetContext();
var configurations = _cacheService.GetRouteConfigs();
if (configurations.Any()) {
var route = filterContext.Controller.ControllerContext.RouteData.Route;
var key = _cacheService.GetRouteDescriptorKey(filterContext.HttpContext, route);
_cacheRouteConfig = configurations.FirstOrDefault(c => c.RouteKey == key);
}
if (!RequestIsCacheable(filterContext)) if (!RequestIsCacheable(filterContext))
return; return;
@@ -180,16 +188,8 @@ namespace Orchard.OutputCache.Filters {
Logger.Debug("Item '{0}' was rendered.", _cacheKey); Logger.Debug("Item '{0}' was rendered.", _cacheKey);
// Obtain individual route configuration, if any.
CacheRouteConfig configuration = null; if (!ResponseIsCacheable(filterContext)) {
var configurations = _cacheService.GetRouteConfigs();
if (configurations.Any()) {
var route = filterContext.Controller.ControllerContext.RouteData.Route;
var key = _cacheService.GetRouteDescriptorKey(filterContext.HttpContext, route);
configuration = configurations.FirstOrDefault(c => c.RouteKey == key);
}
if (!ResponseIsCacheable(filterContext, configuration)) {
filterContext.HttpContext.Response.Cache.SetCacheability(HttpCacheability.NoCache); filterContext.HttpContext.Response.Cache.SetCacheability(HttpCacheability.NoCache);
filterContext.HttpContext.Response.Cache.SetNoStore(); filterContext.HttpContext.Response.Cache.SetNoStore();
filterContext.HttpContext.Response.Cache.SetMaxAge(new TimeSpan(0)); filterContext.HttpContext.Response.Cache.SetMaxAge(new TimeSpan(0));
@@ -197,8 +197,8 @@ namespace Orchard.OutputCache.Filters {
} }
// Determine duration and grace time. // Determine duration and grace time.
var cacheDuration = configuration != null && configuration.Duration.HasValue ? configuration.Duration.Value : CacheSettings.DefaultCacheDuration; var cacheDuration = _cacheRouteConfig != null && _cacheRouteConfig.Duration.HasValue ? _cacheRouteConfig.Duration.Value : CacheSettings.DefaultCacheDuration;
var cacheGraceTime = configuration != null && configuration.GraceTime.HasValue ? configuration.GraceTime.Value : CacheSettings.DefaultCacheGraceTime; var cacheGraceTime = _cacheRouteConfig != null && _cacheRouteConfig.GraceTime.HasValue ? _cacheRouteConfig.GraceTime.Value : CacheSettings.DefaultCacheGraceTime;
// Include each content item ID as tags for the cache entry. // Include each content item ID as tags for the cache entry.
var contentItemIds = _displayedContentItemHandler.GetDisplayed().Select(x => x.ToString(CultureInfo.InvariantCulture)).ToArray(); var contentItemIds = _displayedContentItemHandler.GetDisplayed().Select(x => x.ToString(CultureInfo.InvariantCulture)).ToArray();
@@ -312,6 +312,12 @@ namespace Orchard.OutputCache.Filters {
return false; return false;
} }
// Don't cache if individual route configuration says no.
if (_cacheRouteConfig != null && _cacheRouteConfig.Duration == 0) {
Logger.Debug("Request for item '{0}' ignored because route is configured to not be cached.", itemDescriptor);
return false;
}
// Ignore requests with the refresh key on the query string. // Ignore requests with the refresh key on the query string.
foreach (var key in filterContext.RequestContext.HttpContext.Request.QueryString.AllKeys) { foreach (var key in filterContext.RequestContext.HttpContext.Request.QueryString.AllKeys) {
if (String.Equals(_refreshKey, key, StringComparison.OrdinalIgnoreCase)) { if (String.Equals(_refreshKey, key, StringComparison.OrdinalIgnoreCase)) {
@@ -323,7 +329,7 @@ namespace Orchard.OutputCache.Filters {
return true; return true;
} }
protected virtual bool ResponseIsCacheable(ResultExecutedContext filterContext, CacheRouteConfig configuration) { protected virtual bool ResponseIsCacheable(ResultExecutedContext filterContext) {
if (filterContext.HttpContext.Request.Url == null) { if (filterContext.HttpContext.Request.Url == null) {
return false; return false;
@@ -335,12 +341,6 @@ namespace Orchard.OutputCache.Filters {
return false; return false;
} }
// Don't cache in individual route configuration says no.
if (configuration != null && configuration.Duration == 0) {
Logger.Debug("Response for item '{0}' will not be cached because route is configured to not be cached.", _cacheKey);
return false;
}
// Don't cache if request created notifications. // Don't cache if request created notifications.
var hasNotifications = !String.IsNullOrEmpty(Convert.ToString(filterContext.Controller.TempData["messages"])); var hasNotifications = !String.IsNullOrEmpty(Convert.ToString(filterContext.Controller.TempData["messages"]));
if (hasNotifications) { if (hasNotifications) {
@@ -626,4 +626,4 @@ namespace Orchard.OutputCache.Filters {
public class ViewDataContainer : IViewDataContainer { public class ViewDataContainer : IViewDataContainer {
public ViewDataDictionary ViewData { get; set; } public ViewDataDictionary ViewData { get; set; }
} }
} }