From 9100ba667372f3cafbd176cc096accd604b2b7f3 Mon Sep 17 00:00:00 2001 From: Thomas Hounsell Date: Sun, 21 Aug 2016 21:49:38 +0100 Subject: [PATCH] Add HTTP/2 Push to Output Cache --- BuildFeed/BuildFeed.csproj | 1 + BuildFeed/Code/OutputCachePushAttribute.cs | 27 ++++++++++++++++++++++ BuildFeed/Controllers/frontController.cs | 24 +++++++++---------- 3 files changed, 40 insertions(+), 12 deletions(-) create mode 100644 BuildFeed/Code/OutputCachePushAttribute.cs diff --git a/BuildFeed/BuildFeed.csproj b/BuildFeed/BuildFeed.csproj index 5ea9c39..72bf702 100644 --- a/BuildFeed/BuildFeed.csproj +++ b/BuildFeed/BuildFeed.csproj @@ -192,6 +192,7 @@ + diff --git a/BuildFeed/Code/OutputCachePushAttribute.cs b/BuildFeed/Code/OutputCachePushAttribute.cs new file mode 100644 index 0000000..1cd1597 --- /dev/null +++ b/BuildFeed/Code/OutputCachePushAttribute.cs @@ -0,0 +1,27 @@ +using System; +using System.Collections.Generic; +using System.Globalization; +using System.Linq; +using System.Web; +using System.Web.Mvc; +using BuildFeed.Code.Options; + +namespace BuildFeed.Code +{ + public class OutputCachePushAttribute: ActionFilterAttribute + { + public override void OnResultExecuted(ResultExecutedContext filterContext) + { + bool isRtl = CultureInfo.CurrentUICulture.TextInfo.IsRightToLeft; + Theme theme = new Theme(Theme.DetectTheme(filterContext.HttpContext)); + + filterContext.HttpContext.Response.PushPromise("/res/css/default.min.css"); + filterContext.HttpContext.Response.PushPromise(VirtualPathUtility.ToAbsolute(theme.CssPath)); + if (isRtl) + { + filterContext.HttpContext.Response.PushPromise("/res/css/rtl.min.css"); + } + filterContext.HttpContext.Response.PushPromise("/res/ts/bfs.min.js"); + } + } +} \ No newline at end of file diff --git a/BuildFeed/Controllers/frontController.cs b/BuildFeed/Controllers/frontController.cs index 1f4257c..3dd4282 100644 --- a/BuildFeed/Controllers/frontController.cs +++ b/BuildFeed/Controllers/frontController.cs @@ -29,7 +29,7 @@ public FrontController() [Route("", Order = 1)] #if !DEBUG - [OutputCache(Duration = 600, VaryByParam = "none", VaryByCustom = "userName;lang;theme")] + [OutputCache(Duration = 600, VaryByParam = "none", VaryByCustom = "userName;lang;theme"), OutputCachePush(Order = 2)] #endif public async Task Index() { @@ -39,7 +39,7 @@ public async Task Index() [Route("page-{page:int:min(1)}/", Order = 0)] #if !DEBUG - [OutputCache(Duration = 600, VaryByParam = "page", VaryByCustom = "userName;lang;theme")] + [OutputCache(Duration = 600, VaryByParam = "page", VaryByCustom = "userName;lang;theme"), OutputCachePush(Order = 2)] #endif public async Task IndexPage(int page) { @@ -59,7 +59,7 @@ public async Task IndexPage(int page) [Route("group/{major}.{minor}.{number}.{revision}/", Order = 1)] [Route("group/{major}.{minor}.{number}/", Order = 5)] // for when there is no revision #if !DEBUG - [OutputCache(Duration = 600, VaryByParam = "none", VaryByCustom = "userName;lang;theme")] + [OutputCache(Duration = 600, VaryByParam = "none", VaryByCustom = "userName;lang;theme"), OutputCachePush(Order = 2)] #endif public async Task ViewGroup(uint major, uint minor, uint number, uint? revision = null) { @@ -84,7 +84,7 @@ public async Task ViewGroup(uint major, uint minor, uint number, u [Route("build/{id:guid}/", Name = "Build")] #if !DEBUG - [OutputCache(Duration = 600, VaryByParam = "none", VaryByCustom = "userName;lang;theme")] + [OutputCache(Duration = 600, VaryByParam = "none", VaryByCustom = "userName;lang;theme"), OutputCachePush(Order = 2)] #endif public async Task ViewBuild(Guid id) { @@ -184,7 +184,7 @@ public async Task TwitterCard(long id) [Route("lab/{lab}/", Order = 1, Name = "Lab Root")] #if !DEBUG - [OutputCache(Duration = 600, VaryByParam = "none", VaryByCustom = "userName;lang;theme")] + [OutputCache(Duration = 600, VaryByParam = "none", VaryByCustom = "userName;lang;theme"), OutputCachePush(Order = 2)] #endif public async Task ViewLab(string lab) { @@ -193,7 +193,7 @@ public async Task ViewLab(string lab) [Route("lab/{lab}/page-{page:int:min(2)}/", Order = 0)] #if !DEBUG - [OutputCache(Duration = 600, VaryByParam = "page", VaryByCustom = "userName;lang;theme")] + [OutputCache(Duration = 600, VaryByParam = "page", VaryByCustom = "userName;lang;theme"), OutputCachePush(Order = 2)] #endif public async Task ViewLabPage(string lab, int page) { @@ -219,7 +219,7 @@ public async Task ViewLabPage(string lab, int page) [Route("source/{source}/", Order = 1, Name = "Source Root")] #if !DEBUG - [OutputCache(Duration = 600, VaryByParam = "none", VaryByCustom = "userName;lang;theme")] + [OutputCache(Duration = 600, VaryByParam = "none", VaryByCustom = "userName;lang;theme"), OutputCachePush(Order = 2)] #endif public async Task ViewSource(TypeOfSource source) { @@ -228,7 +228,7 @@ public async Task ViewSource(TypeOfSource source) [Route("source/{source}/page-{page:int:min(2)}/", Order = 0)] #if !DEBUG - [OutputCache(Duration = 600, VaryByParam = "page", VaryByCustom = "userName;lang;theme")] + [OutputCache(Duration = 600, VaryByParam = "page", VaryByCustom = "userName;lang;theme"), OutputCachePush(Order = 2)] #endif public async Task ViewSourcePage(TypeOfSource source, int page) { @@ -254,7 +254,7 @@ public async Task ViewSourcePage(TypeOfSource source, int page) [Route("year/{year}/", Order = 1, Name = "Year Root")] #if !DEBUG - [OutputCache(Duration = 600, VaryByParam = "none", VaryByCustom = "userName;lang;theme")] + [OutputCache(Duration = 600, VaryByParam = "none", VaryByCustom = "userName;lang;theme"), OutputCachePush(Order = 2)] #endif public async Task ViewYear(int year) { @@ -263,7 +263,7 @@ public async Task ViewYear(int year) [Route("year/{year}/page-{page:int:min(2)}/", Order = 0)] #if !DEBUG - [OutputCache(Duration = 600, VaryByParam = "page", VaryByCustom = "userName;lang;theme")] + [OutputCache(Duration = 600, VaryByParam = "page", VaryByCustom = "userName;lang;theme"), OutputCachePush(Order = 2)] #endif public async Task ViewYearPage(int year, int page) { @@ -289,7 +289,7 @@ public async Task ViewYearPage(int year, int page) [Route("version/{major}.{minor}/", Order = 1, Name = "Version Root")] #if !DEBUG - [OutputCache(Duration = 600, VaryByParam = "none", VaryByCustom = "userName;lang;theme")] + [OutputCache(Duration = 600, VaryByParam = "none", VaryByCustom = "userName;lang;theme"), OutputCachePush(Order = 2)] #endif public async Task ViewVersion(uint major, uint minor) { @@ -298,7 +298,7 @@ public async Task ViewVersion(uint major, uint minor) [Route("version/{major}.{minor}/page-{page:int:min(2)}/", Order = 0)] #if !DEBUG - [OutputCache(Duration = 600, VaryByParam = "none", VaryByCustom = "userName;lang;theme")] + [OutputCache(Duration = 600, VaryByParam = "none", VaryByCustom = "userName;lang;theme"), OutputCachePush(Order = 2)] #endif public async Task ViewVersionPage(uint major, uint minor, int page) {