Bundler: Better cache response

This commit is contained in:
2018-03-17 02:01:52 +01:00
parent 3b87116d44
commit 130c20a30d
3 changed files with 24 additions and 10 deletions

View File

@@ -1,7 +1,9 @@
using System.Collections.Generic;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Web;
namespace VAR.Focus.Web.Code
{
@@ -42,22 +44,32 @@ namespace VAR.Focus.Web.Code
#region Public methods
public void WriteResponse(Stream outStream)
public void WriteResponse(HttpResponse response, string contentType)
{
response.ContentType = contentType;
foreach (string fileName in Files)
{
string fileContent = File.ReadAllText(fileName);
byte[] byteArray = Encoding.UTF8.GetBytes(fileContent);
if (byteArray.Length > 0)
{
outStream.Write(byteArray, 0, byteArray.Length);
response.OutputStream.Write(byteArray, 0, byteArray.Length);
byteArray = Encoding.UTF8.GetBytes("\n\n");
outStream.Write(byteArray, 0, byteArray.Length);
response.OutputStream.Write(byteArray, 0, byteArray.Length);
}
}
}
public void PrepareCacheableResponse(HttpResponse response)
{
const int secondsInDay = 86400;
response.ExpiresAbsolute = DateTime.Now.AddSeconds(secondsInDay);
response.Expires = secondsInDay;
response.Cache.SetCacheability(HttpCacheability.Public);
response.Cache.SetMaxAge(new TimeSpan(0, 0, secondsInDay));
}
#endregion Public methods
}
}

View File

@@ -1,4 +1,5 @@
using System.Web;
using System;
using System.Web;
namespace VAR.Focus.Web.Code
{
@@ -11,8 +12,8 @@ namespace VAR.Focus.Web.Code
public void ProcessRequest(HttpContext context)
{
Bundler bundler = new Bundler(context.Server.MapPath("~/Scripts/"));
context.Response.ContentType = "text/javascript";
bundler.WriteResponse(context.Response.OutputStream);
bundler.PrepareCacheableResponse(context.Response);
bundler.WriteResponse(context.Response, "text/javascript");
}
#endregion IHttpHandler

View File

@@ -1,4 +1,5 @@
using System.Web;
using System;
using System.Web;
namespace VAR.Focus.Web.Code
{
@@ -11,8 +12,8 @@ namespace VAR.Focus.Web.Code
public void ProcessRequest(HttpContext context)
{
Bundler bundler = new Bundler(context.Server.MapPath("~/Styles/"));
context.Response.ContentType = "text/css";
bundler.WriteResponse(context.Response.OutputStream);
bundler.PrepareCacheableResponse(context.Response);
bundler.WriteResponse(context.Response, "text/css");
}
#endregion IHttpHandler