File Renaming; Fix #28 (Output Caching)

This commit is contained in:
Thomas Hounsell 2016-08-21 15:17:56 +01:00
parent 1531151259
commit bd4b3baff5
10 changed files with 54 additions and 32 deletions

View File

@ -90,11 +90,11 @@
<ItemGroup>
<Compile Include="Api\NewBuild.cs" />
<Compile Include="Api\SearchResult.cs" />
<Compile Include="Build-Group.cs" />
<Compile Include="Build-Lab.cs" />
<Compile Include="Build-Source.cs" />
<Compile Include="Build-Version.cs" />
<Compile Include="Build-Year.cs" />
<Compile Include="BuildRepository-Group.cs" />
<Compile Include="BuildRepository-Lab.cs" />
<Compile Include="BuildRepository-Source.cs" />
<Compile Include="BuildRepository-Version.cs" />
<Compile Include="BuildRepository-Year.cs" />
<Compile Include="BuildRepository.cs" />
<Compile Include="BuildGroup.cs" />
<Compile Include="Build.cs" />

View File

@ -1,12 +1,15 @@
using System;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;
using BuildFeed.Local;
namespace BuildFeed.Code.Options
{
public class Theme
{
private const string THEME_COOKIE_NAME = "bf_theme";
public static Theme[] AvailableThemes = (from st in Enum.GetValues(typeof(SiteTheme)).Cast<SiteTheme>()
select new Theme(st)).ToArray();
@ -17,6 +20,18 @@ namespace BuildFeed.Code.Options
public string DisplayName => MvcExtensions.GetDisplayTextForEnum(_siteTheme);
public Theme(SiteTheme st) { _siteTheme = st; }
public static SiteTheme DetectTheme(HttpContextBase context)
{
string themeCookie = context.Request.Cookies[THEME_COOKIE_NAME]?.Value;
SiteTheme theme = SiteTheme.Dark;
if (!string.IsNullOrEmpty(themeCookie))
{
Enum.TryParse(themeCookie, out theme);
}
return theme;
}
}
public enum SiteTheme

View File

@ -10,7 +10,6 @@ namespace BuildFeed.Controllers
public class BaseController : Controller
{
private const string LANG_COOKIE_NAME = "bf_lang";
private const string THEME_COOKIE_NAME = "bf_theme";
protected override void Initialize(RequestContext requestContext)
{
@ -36,13 +35,7 @@ namespace BuildFeed.Controllers
catch (CultureNotFoundException) { }
}
string themeCookie = requestContext.HttpContext.Request.Cookies[THEME_COOKIE_NAME]?.Value;
SiteTheme theme = SiteTheme.Dark;
if (!string.IsNullOrEmpty(themeCookie))
{
Enum.TryParse(themeCookie, out theme);
}
ViewBag.Theme = new Theme(theme);
ViewBag.Theme = new Theme(Theme.DetectTheme(requestContext.HttpContext));
base.Initialize(requestContext);
}

View File

@ -29,7 +29,7 @@ namespace BuildFeed.Controllers
[Route("", Order = 1)]
#if !DEBUG
// [OutputCache(Duration = 600, VaryByParam = "none", VaryByCustom = "userName")]
[OutputCache(Duration = 600, VaryByParam = "none", VaryByCustom = "userName;lang;theme")]
#endif
public async Task<ActionResult> Index()
{
@ -39,7 +39,7 @@ namespace BuildFeed.Controllers
[Route("page-{page:int:min(1)}/", Order = 0)]
#if !DEBUG
// [OutputCache(Duration = 600, VaryByParam = "page", VaryByCustom = "userName")]
[OutputCache(Duration = 600, VaryByParam = "page", VaryByCustom = "userName;lang;theme")]
#endif
public async Task<ActionResult> IndexPage(int page)
{
@ -59,7 +59,7 @@ namespace BuildFeed.Controllers
[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")]
[OutputCache(Duration = 600, VaryByParam = "none", VaryByCustom = "userName;lang;theme")]
#endif
public async Task<ActionResult> ViewGroup(uint major, uint minor, uint number, uint? revision = null)
{
@ -73,7 +73,7 @@ namespace BuildFeed.Controllers
List<Build> builds = await _bModel.SelectGroup(bg);
return builds.Count() == 1
return builds.Count == 1
? RedirectToAction(nameof(ViewBuild),
new
{
@ -84,7 +84,7 @@ namespace BuildFeed.Controllers
[Route("build/{id:guid}/", Name = "Build")]
#if !DEBUG
// [OutputCache(Duration = 600, VaryByParam = "none", VaryByCustom = "userName")]
[OutputCache(Duration = 600, VaryByParam = "none", VaryByCustom = "userName;lang;theme")]
#endif
public async Task<ActionResult> ViewBuild(Guid id)
{
@ -184,7 +184,7 @@ namespace BuildFeed.Controllers
[Route("lab/{lab}/", Order = 1, Name = "Lab Root")]
#if !DEBUG
// [OutputCache(Duration = 600, VaryByParam = "none", VaryByCustom = "userName")]
[OutputCache(Duration = 600, VaryByParam = "none", VaryByCustom = "userName;lang;theme")]
#endif
public async Task<ActionResult> ViewLab(string lab)
{
@ -193,7 +193,7 @@ namespace BuildFeed.Controllers
[Route("lab/{lab}/page-{page:int:min(2)}/", Order = 0)]
#if !DEBUG
// [OutputCache(Duration = 600, VaryByParam = "page", VaryByCustom = "userName")]
[OutputCache(Duration = 600, VaryByParam = "page", VaryByCustom = "userName;lang;theme")]
#endif
public async Task<ActionResult> ViewLabPage(string lab, int page)
{
@ -219,7 +219,7 @@ namespace BuildFeed.Controllers
[Route("source/{source}/", Order = 1, Name = "Source Root")]
#if !DEBUG
// [OutputCache(Duration = 600, VaryByParam = "none", VaryByCustom = "userName")]
[OutputCache(Duration = 600, VaryByParam = "none", VaryByCustom = "userName;lang;theme")]
#endif
public async Task<ActionResult> ViewSource(TypeOfSource source)
{
@ -228,7 +228,7 @@ namespace BuildFeed.Controllers
[Route("source/{source}/page-{page:int:min(2)}/", Order = 0)]
#if !DEBUG
// [OutputCache(Duration = 600, VaryByParam = "page", VaryByCustom = "userName")]
[OutputCache(Duration = 600, VaryByParam = "page", VaryByCustom = "userName;lang;theme")]
#endif
public async Task<ActionResult> ViewSourcePage(TypeOfSource source, int page)
{
@ -254,7 +254,7 @@ namespace BuildFeed.Controllers
[Route("year/{year}/", Order = 1, Name = "Year Root")]
#if !DEBUG
// [OutputCache(Duration = 600, VaryByParam = "none", VaryByCustom = "userName")]
[OutputCache(Duration = 600, VaryByParam = "none", VaryByCustom = "userName;lang;theme")]
#endif
public async Task<ActionResult> ViewYear(int year)
{
@ -263,7 +263,7 @@ namespace BuildFeed.Controllers
[Route("year/{year}/page-{page:int:min(2)}/", Order = 0)]
#if !DEBUG
// [OutputCache(Duration = 600, VaryByParam = "page", VaryByCustom = "userName")]
[OutputCache(Duration = 600, VaryByParam = "page", VaryByCustom = "userName;lang;theme")]
#endif
public async Task<ActionResult> ViewYearPage(int year, int page)
{
@ -289,7 +289,7 @@ namespace BuildFeed.Controllers
[Route("version/{major}.{minor}/", Order = 1, Name = "Version Root")]
#if !DEBUG
// [OutputCache(Duration = 600, VaryByParam = "none", VaryByCustom = "userName")]
[OutputCache(Duration = 600, VaryByParam = "none", VaryByCustom = "userName;lang;theme")]
#endif
public async Task<ActionResult> ViewVersion(uint major, uint minor)
{
@ -298,7 +298,7 @@ namespace BuildFeed.Controllers
[Route("version/{major}.{minor}/page-{page:int:min(2)}/", Order = 0)]
#if !DEBUG
// [OutputCache(Duration = 600, VaryByParam = "none", VaryByCustom = "userName")]
[OutputCache(Duration = 600, VaryByParam = "none", VaryByCustom = "userName;lang;theme")]
#endif
public async Task<ActionResult> ViewVersionPage(uint major, uint minor, int page)
{

View File

@ -1,8 +1,11 @@
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using BuildFeed.Code.Options;
using BuildFeed.Model;
namespace BuildFeed
@ -32,15 +35,26 @@ namespace BuildFeed
public override string GetVaryByCustomString(HttpContext context, string custom)
{
switch (custom)
string[] parts = custom.Split(';');
List<string> varyParts = new List<string>();
foreach (string part in parts)
{
case "userName":
return context.User.Identity.Name.ToLower();
case "lang":
return context.Request.Cookies["lang"]?.Value ?? CultureInfo.CurrentUICulture.IetfLanguageTag;
switch (part)
{
case "userName":
varyParts.Add($"user:{context.User.Identity.Name}");
break;
case "lang":
varyParts.Add($"lang:{CultureInfo.CurrentUICulture.LCID}");
break;
case "theme":
varyParts.Add($"theme:{Theme.DetectTheme(new HttpContextWrapper(context))}");
break;
}
}
return "";
return string.Join(";", varyParts.OrderBy(s => s));
}
}
}