Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OutputCache and a custom gzip compression filter

I have this custom filter for 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's Voice in .NET).

Now I have an action method like this:

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

How can I ensure that the OutputCache filter is caching the compressed content? Using the "Order" parameter like in this example will be enough?

How can I see what is going on in the cache?

Cheers.

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

Probably this is because OutputCache is the last filter by default, and there is no way to change that. May anybody confirm this?

like image 272
vtortola Avatar asked Jul 29 '11 09:07

vtortola


People also ask

What is gzip filter?

This filter will compress the content of replies using GZIP. Compression is done on the fly. This assumes that you're really on a slow link, where you have lots of CPU, but not much bandwidth.

What is gzip and deflate encoding?

The browser sends a header telling the server it accepts compressed content (gzip and deflate are two compression schemes): Accept-Encoding: gzip, deflate. The server sends a response if the content is actually compressed: Content-Encoding: gzip.

Does ASP net Core support response compression?

When you're unable to use the compression features of web servers (IIS, Apache, Nginx), ASP.NET Core provides an alternate option, Response Compression middleware.

What is response compression?

Response compression can optimize network bandwidth usage and increase application responsiveness. The Commerce Engine supports JSON compression using GZIP, based the on Microsoft ASP.net Core 2.1., and using the Microsoft. AspNetCore.


1 Answers

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

From the page..

Order is important

The ActionFilter above must absolutely be run as last: as I discovered lately, as soon as an action filter changes the action result, the current action invocation is aborted. This also means that all other action filters which did not have a chance to run, will not run, ever. If you plan on adding this caching method to your project, make sure that all filters have the right priority (using the Order priority, that takes a positive integer and orders from lowest to highest).

like image 141
StefanE Avatar answered Nov 13 '22 07:11

StefanE