Permalink
Browse files

tune the caching from the handlers

  • Loading branch information...
ninianne98 committed Nov 2, 2018
1 parent 5478bbc commit fe4fc6098903b9fcc87afa8bc0c48b03fef81dce
Showing with 78 additions and 6 deletions.
  1. +1 −1 WebComponents/CarrotWeb.cs
  2. +69 −5 WebComponents/Controllers/HomeController.cs
  3. +8 −0 WebComponents/carrotHelp.js
@@ -47,7 +47,7 @@ public static class CarrotWeb {
public static string ShortDateTimeFormatPattern {
get {
return "{0:" + ShortDatePattern + "} {0:" + ShortTimePattern + "} ";
return "{0:" + ShortDatePattern + "} {0:" + ShortTimePattern + "}";
}
}
@@ -4,6 +4,7 @@
using System.Drawing.Imaging;
using System.IO;
using System.Text;
using System.Web;
using System.Web.Mvc;
/*
@@ -27,8 +28,14 @@ public class HomeController : Controller {
stream.Dispose();
}
[OutputCache(Duration = 120)]
public ActionResult GetImageThumb(string thumb, bool? scale, int? square) {
var context = System.Web.HttpContext.Current;
context.Response.Cache.VaryByParams["thumb"] = true;
context.Response.Cache.VaryByParams["scale"] = true;
context.Response.Cache.VaryByParams["square"] = true;
DoCacheMagic(context, 3);
string sImageUri = thumb;
string sImgPath = thumb;
@@ -126,6 +133,13 @@ public class HomeController : Controller {
}
public ActionResult GetCaptchaImage(string fgcolor, string bgcolor, string ncolor) {
var context = System.Web.HttpContext.Current;
context.Response.Cache.VaryByParams["fgcolor"] = true;
context.Response.Cache.VaryByParams["bgcolor"] = true;
context.Response.Cache.VaryByParams["ncolor"] = true;
DoCacheMagic(context, 3);
Color f = ColorTranslator.FromHtml(CaptchaImage.FGColorDef);
Color b = ColorTranslator.FromHtml(CaptchaImage.BGColorDef);
Color n = ColorTranslator.FromHtml(CaptchaImage.NColorDef);
@@ -143,14 +157,15 @@ public class HomeController : Controller {
bmpCaptcha.Save(stream, ImageFormat.Png);
bmpCaptcha.Dispose();
Response.StatusCode = 200;
Response.StatusDescription = "OK";
return File(stream.ToArray(), "image/png");
}
[OutputCache(Duration = 600)]
public ActionResult GetCarrotHelp(string id) {
var context = System.Web.HttpContext.Current;
context.Response.Cache.VaryByParams["ts"] = true;
DoCacheMagic(context, 10);
string sBody = CarrotWeb.GetManifestResourceStream("Carrotware.Web.UI.Components.carrotHelp.js");
DateTime timeAM = DateTime.Now.Date.AddHours(6); // 6AM
DateTime timePM = DateTime.Now.Date.AddHours(12); // 6PM
@@ -159,6 +174,8 @@ public class HomeController : Controller {
sBody = sBody.Replace("[[SHORTDATEPATTERN]]", CarrotWeb.ShortDatePattern);
sBody = sBody.Replace("[[SHORTTIMEPATTERN]]", CarrotWeb.ShortTimePattern);
sBody = sBody.Replace("[[SHORTDATEFORMATPATTERN]]", CarrotWeb.ShortDateFormatPattern);
sBody = sBody.Replace("[[SHORTDATETIMEFORMATPATTERN]]", CarrotWeb.ShortDateTimeFormatPattern);
sBody = sBody.Replace("[[AM_TIMEPATTERN]]", timeAM.ToString("tt"));
sBody = sBody.Replace("[[PM_TIMEPATTERN]]", timePM.ToString("tt"));
@@ -168,5 +185,52 @@ public class HomeController : Controller {
return File(stream, "text/javascript");
}
protected void DoCacheMagic(HttpContext context, int interval) {
DateTime now = DateTime.Now;
DateTime dtModified = GetFauxModDate(10);
DateTime? dtM = GetModDate(context);
string strModifed = dtModified.ToUniversalTime().ToString("r");
context.Response.AppendHeader("Last-Modified", strModifed);
context.Response.AppendHeader("Date", strModifed);
context.Response.Cache.SetLastModified(dtModified);
DateTime dtExpire = now.ToUniversalTime().AddMinutes(interval);
context.Response.Cache.SetExpires(dtExpire);
context.Response.Cache.SetValidUntilExpires(true);
context.Response.Cache.SetCacheability(HttpCacheability.Private);
if (dtM == null || dtM.Value != dtModified) {
context.Response.StatusCode = (int)System.Net.HttpStatusCode.OK;
context.Response.StatusDescription = "OK";
} else {
context.Response.StatusCode = (int)System.Net.HttpStatusCode.NotModified;
context.Response.SuppressContent = true;
}
}
protected DateTime GetFauxModDate(int interval) {
DateTime now = DateTime.Now;
DateTime dtMod = now.AddMinutes(-90);
TimeSpan ts = TimeSpan.FromMinutes(interval);
DateTime dtModified = new DateTime(((dtMod.Ticks + ts.Ticks - 1) / ts.Ticks) * ts.Ticks);
return dtModified;
}
protected DateTime? GetModDate(HttpContext context) {
DateTime? dtModSince = null;
string modSince = context.Request.Headers.Get("If-Modified-Since");
if (!String.IsNullOrEmpty(modSince)) {
dtModSince = DateTime.Parse(modSince);
dtModSince = dtModSince.Value.ToUniversalTime();
}
return dtModSince;
}
}
}
@@ -53,4 +53,12 @@ function __carrotGetPMDateFormat() {
return "[[PM_TIMEPATTERN]]";
}
function __carrotGetDateTemplate() {
return "[[SHORTDATEFORMATPATTERN]]";
}
function __carrotGetDateTimeTemplate() {
return "[[SHORTDATETIMEFORMATPATTERN]]";
}
//================================================================

0 comments on commit fe4fc60

Please sign in to comment.