Skip to content

Instantly share code, notes, and snippets.

@carlwoodhouse
Created September 29, 2015 08:57
Show Gist options
  • Save carlwoodhouse/c1af1f1105b2f5ced40c to your computer and use it in GitHub Desktop.
Save carlwoodhouse/c1af1f1105b2f5ced40c to your computer and use it in GitHub Desktop.
using Orchard;
using Orchard.Caching;
using Orchard.Environment.Configuration;
using Orchard.Environment.Extensions;
using Orchard.Mvc.Filters;
using Orchard.OutputCache.Filters;
using Orchard.OutputCache.Services;
using Orchard.Services;
using Orchard.Themes;
using Patient.Core.Extensions;
using Patient.Core.Layout;
using Patient.Core.Localisation.Services;
using Patient.Core.Membership;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace Patient.OutputCache
{
[OrchardSuppressDependency("Orchard.OutputCache.Filters.OutputCacheFilter")]
public class OutputCacheKeyModifier : OutputCacheFilter
{
private readonly IUserService userService;
private readonly ILocalisationService localisationService;
public OutputCacheKeyModifier(
ILocalisationService localisationService,
IUserService userService,
ICacheManager cacheManager,
IOutputCacheStorageProvider cacheStorageProvider,
ITagCache tagCache,
IDisplayedContentItemHandler displayedContentItemHandler,
IWorkContextAccessor workContextAccessor,
IThemeManager themeManager,
IClock clock,
ICacheService cacheService,
ISignals signals,
ShellSettings shellSettings) :
base(
cacheManager, cacheStorageProvider, tagCache, displayedContentItemHandler, workContextAccessor,
themeManager, clock, cacheService, signals, shellSettings)
{
this.userService = userService;
this.localisationService = localisationService;
}
protected override IDictionary<string, object> GetCacheKeyParameters(System.Web.Mvc.ActionExecutingContext filterContext)
{
var result = base.GetCacheKeyParameters(filterContext);
// cache by compression supprt
if (filterContext.RequestContext.HttpContext.IsGZipCapable()
&& !result.ContainsKey("compression"))
{
result.Add("compression", "gzip");
}
// Cache by device profile (Desktop, Mobile)
if (!result.ContainsKey("p_dp"))
{
DeviceProfileType currentDeviceProfile = DeviceProfiler.EvaluateDeviceProfile(filterContext.HttpContext);
result.Add("p_dp", (int)currentDeviceProfile);
}
// Cache by IE Version
if (!result.ContainsKey("p_ie"))
{
var ieVersion = DeviceProfiler.EvaluateIEVersion(filterContext.HttpContext);
result.Add("p_ie", ieVersion);
}
// n3
if (!result.ContainsKey("p_loc"))
{
if (localisationService.IsN3Request(filterContext.HttpContext))
{
result.Add("p_loc", "n3");
}
}
return result;
}
protected override bool RequestIsCacheable(System.Web.Mvc.ActionExecutingContext filterContext)
{
var isCacheable = base.RequestIsCacheable(filterContext);
if (isCacheable)
{
isCacheable = !userService.IsUserLoggedIn();
}
return isCacheable;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment