OutputCache and customizable gzip compression filter

I have this custom filter to compress the output of my pages:

public class EnableCompressionAttribute : ActionFilterAttribute { const CompressionMode compress = CompressionMode.Compress; public override void OnActionExecuting(ActionExecutingContext filterContext) { HttpRequestBase request = filterContext.HttpContext.Request; HttpResponseBase response = filterContext.HttpContext.Response; string acceptEncoding = request.Headers["Accept-Encoding"]; if (acceptEncoding == null) return; if (acceptEncoding.ToLower().Contains("gzip")) { response.Filter = new GZipStream(response.Filter, compress); response.AppendHeader("Content-Encoding", "gzip"); } else if (acceptEncoding.ToLower().Contains("deflate")) { response.Filter = new DeflateStream(response.Filter, compress); response.AppendHeader("Content-Encoding", "deflate"); } } } 

I got the code from the book: Pro ASP.NET MVC V2 Framework (Expert Voice in .NET).

Now I have a mode of action similar to this:

 [OutputCache(Order=1, Duration=300,VaryByParam="*", VaryByContentEncoding="gzip; deflate")] [EnableCompression(Order=0)] public ActionResult About() { return View(); } 

How can I guarantee that the OutputCache filter caches compressed content? Will using the "Order" parameter, as in this example, be enough?

How can I find out what is happening in the cache?

Greetings.

UPDATE I tested Fiddler, apparently, it works no matter what order you use in the filters ... I get the first response with gzip and http.302 encoding in the following requests if the client is allowed to cache it or more http.200 with gzip encoding if only server allowed

Perhaps this is because OutputCache is the last filter by default, and there is no way to change it. Can anyone confirm this?

+6
source share
1 answer

Take a look at this page, http://www.klopfenstein.net/lorenz.aspx/my-take-on-asp-net-output-caching There is good information there, especially Jeff Atwood's advice on compressing cache items

From the page ..

Order is important

The ActionFilter above should be absolutely launched as the last one: as I discovered recently, as soon as the action filter changes the result of the action, the current action call is interrupted. It also means that all other action filters that cannot be triggered will never work. If you plan to add this caching method to your project, make sure that all filters have the correct priority (using the Order priority, which takes a positive integer and orders from the lowest to the highest).

+4
source

Source: https://habr.com/ru/post/893849/


All Articles