Skip to content
Permalink
Browse files

Disabling caching for unpublished pages; code review fixes

  • Loading branch information
napernik committed Jan 13, 2020
1 parent 78b472a commit db7a19aca3c5c425dff7a68e277dcf2d51e122c9
@@ -1,4 +1,4 @@
using System;
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Linq;
@@ -24,17 +24,13 @@ static OutputCacheHelper()
{
CacheabilityFieldInfo = typeof(HttpCachePolicy).GetField("_cacheability", BindingFlags.Instance | BindingFlags.NonPublic);

_outputCacheProfiles = new Dictionary<string, OutputCacheProfile>();
var section = WebConfigurationManager.OpenWebConfiguration(HostingEnvironment.ApplicationVirtualPath)
.GetSection("system.web/caching/outputCacheSettings");

var settings = WebConfigurationManager.OpenWebConfiguration(HostingEnvironment.ApplicationVirtualPath)
.GetSection("system.web/caching/outputCacheSettings") as OutputCacheSettingsSection;

if (settings != null)
if (section is OutputCacheSettingsSection settings)
{
foreach (OutputCacheProfile profile in settings.OutputCacheProfiles)
{
_outputCacheProfiles[profile.Name] = profile;
}
_outputCacheProfiles = settings.OutputCacheProfiles.OfType<OutputCacheProfile>()
.ToDictionary(_ => _.Name);
}
}

@@ -153,37 +149,34 @@ static OutputCacheProvider GetCacheProvider(HttpContext context)
}


public static bool ResponseCachebale(HttpContext context)
public static bool ResponseCacheable(HttpContext context)
{
if (context.Response.StatusCode != 200)
{
return false;
}

#if !DEBUG
var cacheability = GetPageCacheablity(context);
var cacheability = GetPageCacheability(context);

return cacheability > HttpCacheability.NoCache;
#endif
return true;
}


private static HttpCacheability GetPageCacheablity(HttpContext context)
private static HttpCacheability GetPageCacheability(HttpContext context)
=> (HttpCacheability)CacheabilityFieldInfo.GetValue(context.Response.Cache);



public static void InitializeFullPageCaching(HttpContext context)
{
using (var page = new CachableEmptyPage())
using (var page = new CacheableEmptyPage())
{
page.ProcessRequest(context);
}
}


private class CachableEmptyPage : Page
private class CacheableEmptyPage : Page
{
protected override void FrameworkInitialize()
{
@@ -20,22 +20,28 @@ public void ProcessRequest(HttpContext context)
{
OutputCacheHelper.InitializeFullPageCaching(context);

bool cachingEnabled = OutputCacheHelper.TryGetCacheKey(context, out string cacheKey);

using (var renderingContext = RenderingContext.InitializeFromHttpContext())
{
var functionContext = PageRenderer.GetPageRenderFunctionContextContainer();

XDocument document;
bool cachingEnabled = false;
string cacheKey = null;
DonutCacheEntry cacheEntry = null;
if (cachingEnabled)

bool consoleUserLoggedIn = Composite.C1Console.Security.UserValidationFacade.IsLoggedIn();

// "Donut caching" is enabled for logged in users, only if profiling is enabled as well.
if (!renderingContext.CachingDisabled
&& (!consoleUserLoggedIn || renderingContext.ProfilingEnabled))
{
cachingEnabled = OutputCacheHelper.TryGetCacheKey(context, out cacheKey);
using (Profiler.Measure("Cache lookup"))
{
cacheEntry = OutputCacheHelper.GetFromCache(context, cacheKey);
}
}

XDocument document;
var functionContext = PageRenderer.GetPageRenderFunctionContextContainer();

bool allFunctionsExecuted = false;
bool preventResponseCaching = false;

@@ -65,10 +71,10 @@ public void ProcessRequest(HttpContext context)
{
document = slimRenderer.Render(renderingContext.PageContentToRender, functionContext);
}

allFunctionsExecuted = PageRenderer.ExecuteCacheableFunctions(document.Root, functionContext);

if (cachingEnabled && !allFunctionsExecuted && OutputCacheHelper.ResponseCachebale(context))
if (cachingEnabled && !allFunctionsExecuted && OutputCacheHelper.ResponseCacheable(context))
{
preventResponseCaching = true;

@@ -129,6 +135,12 @@ public void ProcessRequest(HttpContext context)
context.Response.Cache.SetNoServerCaching();
}

// Disabling ASP.NET cache if there's a logged-in user
if (consoleUserLoggedIn)
{
context.Response.Cache.SetCacheability(HttpCacheability.NoCache);
}

// Inserting performance profiling information
if (renderingContext.ProfilingEnabled)
{
@@ -91,6 +91,17 @@ public IHttpHandler GetHttpHandler(RequestContext requestContext)
context.RewritePath(filePath, pathInfo, queryString);
}

if (_handlerType == null && GlobalSettingsFacade.OmitAspNetWebFormsSupport)
{
var page = _pageUrlData.GetPage()
?? throw new HttpException(404, "Page not found - either this page has not been published yet or it has been deleted.");

if (IsSlimPageRenderer(page.TemplateId))
{
return new CmsPageHttpHandler();
}
}

// Disabling ASP.NET cache if there's a logged-in user
if (Composite.C1Console.Security.UserValidationFacade.IsLoggedIn())
{
@@ -102,17 +113,6 @@ public IHttpHandler GetHttpHandler(RequestContext requestContext)
return (IHttpHandler)Activator.CreateInstance(_handlerType);
}

if (GlobalSettingsFacade.OmitAspNetWebFormsSupport)
{
var page = _pageUrlData.GetPage()
?? throw new HttpException(404, "Page not found - either this page has not been published yet or it has been deleted.");

if (IsSlimPageRenderer(page.TemplateId))
{
return new CmsPageHttpHandler();
}
}

return (IHttpHandler)BuildManager.CreateInstanceFromVirtualPath(PageHandlerVirtualPath, typeof(Page));
}

0 comments on commit db7a19a

Please sign in to comment.
You can’t perform that action at this time.