Friday, March 28, 2008 3:12 AM Kazi Manzur Rashid

ASP.NET MVC Action Filter - Caching and Compression

Caching plays a major role in developing highly scalable web applications. We can cache any http get request in the user browser for a predefined time, if the user request the same URL in that predefined time the response will be loaded from the browser cache instead of the server. You can archive the same in ASP.NET MVC application with the following action filter:

using System;
using System.Web;
using System.Web.Mvc;

public class CacheFilterAttribute : ActionFilterAttribute
{
    /// <summary>
    /// Gets or sets the cache duration in seconds. The default is 10 seconds.
    /// </summary>
    /// <value>The cache duration in seconds.</value>
    public int Duration
    {
        get;
        set;
    }

    public CacheFilterAttribute()
    {
        Duration = 10;
    }

    public override void OnActionExecuted(FilterExecutedContext filterContext)
    {
        if (Duration <= 0) return;

        HttpCachePolicyBase cache = filterContext.HttpContext.Response.Cache;
        TimeSpan cacheDuration = TimeSpan.FromSeconds(Duration);

        cache.SetCacheability(HttpCacheability.Public);
        cache.SetExpires(DateTime.Now.Add(cacheDuration));
        cache.SetMaxAge(cacheDuration);
        cache.AppendCacheExtension("must-revalidate, proxy-revalidate");
    }
}

You can apply the filter in your Controller action method like the following.

[CacheFilter(Duration = 60)]
public void Category(string name, int? page)

The following shows the screen-shot in firebug  when cache filter is not applied:

and this is the screen-shot when the cache filter is applied:

Another important thing is compression. Now a days, all modern browsers accept compressed contents and it saves huge bandwidth. You can apply the following action filter to compress your response in your  ASP.NET MVC application:

using System.Web;
using System.Web.Mvc;

public class CompressFilter : ActionFilterAttribute
{
    public override void OnActionExecuting(FilterExecutingContext filterContext)
    {
        HttpRequestBase request = filterContext.HttpContext.Request;

        string acceptEncoding = request.Headers["Accept-Encoding"];

        if (string.IsNullOrEmpty(acceptEncoding)) return;

        acceptEncoding = acceptEncoding.ToUpperInvariant();

        HttpResponseBase response = filterContext.HttpContext.Response;

        if (acceptEncoding.Contains("GZIP"))
        {
            response.AppendHeader("Content-encoding", "gzip");
            response.Filter = new GZipStream(response.Filter, CompressionMode.Compress);
        }
        else if (acceptEncoding.Contains("DEFLATE"))
        {
            response.AppendHeader("Content-encoding", "deflate");
            response.Filter = new DeflateStream(response.Filter, CompressionMode.Compress);
        }
    }
}

Just decorate your controller action with this filter:

[CompressFilter]
public void Category(string name, int? page)

The following shows when compression is not applied:

and this is the screen-shot when the compress filter is applied:

You can also apply both these filter in the same action method, like the following:

[CompressFilter(Order = 1)]
[CacheFilter(Duration = 60, Order = 2)]
public void Category(string name, int? page)

And this is the screen-shot:


Enjoy!!!

Download: Source.zip

kick it on DotNetKicks.com

Filed under: ,

Comments

# ASP.NET MVC Action Filter - Caching and Compression

Thursday, March 27, 2008 4:14 PM by DotNetKicks.com

You've been kicked (a good thing) - Trackback from DotNetKicks.com

# ASP.NET MVC Action Filter - 缓存与压缩

Thursday, March 27, 2008 10:09 PM by Q.Lee.lulu

原文地址:ASP.NETMVCActionFilter-CachingandCompression下载源码:Source.zip关于ActionFilter你可以参考我的另外...

# re: ASP.NET MVC Action Filter - Caching and Compression

Thursday, March 27, 2008 11:11 PM by Stuart

Thanks a bunch for posting these.  Your whole MVC series has been great

# ASP.NET MVC Action Filter - 缓存与压缩

Friday, March 28, 2008 1:21 AM by cnblogs.com

原文地址: ASP.NET MVC Action Filter - Caching and Compression 下载源码 : Source.zip 关于Action Filter你可以参考我的另外一篇文章

# ASP.NET MVC Action Filter - 缓存与压缩。

Friday, March 28, 2008 3:10 AM by Zqin

ASP.NET MVC Action Filter - 缓存与压缩

# Reflective Perspective - Chris Alcock &raquo; The Morning Brew #61

Pingback from  Reflective Perspective - Chris Alcock  &raquo; The Morning Brew #61

# re: ASP.NET MVC Action Filter - Caching and Compression

Friday, March 28, 2008 10:37 AM by Muhammad Mosa

Simple and clear, and easy to follow!

Thank you Kazi

# re: ASP.NET MVC Action Filter - Caching and Compression

Friday, March 28, 2008 6:58 PM by tgmdbm

Nice, I like the compression filter, but the "Browser Cache" filter doesn't seem to do anything.

my Cache-Control header is "private, must-revalidate, proxy-revalidate, max-age=60" even tho we're obviously trying to set it to "public ...". I don't understand that.

Maybe that is affecting it but no matter how many times i request my page the Request Headers don't contain a Modified-Since entry.

With browser cache, the browser should send the request asking if the file has been modified since the date of the cached version. But in the case of dynamic pages the server should always say the file has just been modified so would send the page down every time anyway.

We'd have to send down a false modified date to get the browser to use its cached version.

# re: ASP.NET MVC Action Filter - Caching and Compression

Saturday, March 29, 2008 4:35 AM by Kazi Manzur Rashid

@Everyone: Thanks.

@tgmdbm: I think I need to clarify more on Browser caching. Content can be cached by both Cach-Control header(The CacheFilter in this case) and ETag(Which I guess you are mentioning). The advantage of Cache-Control over ETag is, the browser does not send a request to server to check if the content has been modified in that cache priod, but for ETag the browser sends a request to Server to check if the content is modified and if the content is not modified the server sends a 304(Not Modified). So using Cache-Control you are reducing the extra Roundtrip of the server. Also ETag is more appropriate for static files like image, css, js where the web server aumatically genarates the values of ETag.

The following is an excellent article on Http Cache:

www.mnot.net/cache_docs

# Enlaces de Marzo: ASP.NET, ASP.NET AJAX, ASP.NET MVC, Visual Studio, Silverlight, .NET &laquo; Thinking in .NET

Pingback from  Enlaces de Marzo: ASP.NET, ASP.NET AJAX, ASP.NET MVC, Visual Studio, Silverlight, .NET &laquo; Thinking in .NET

# ASP.NET MVC - Controller mit Attributen und Views absichern | Code-Inside Blog

Pingback from  ASP.NET MVC - Controller mit Attributen und Views absichern | Code-Inside Blog

# re: ASP.NET MVC Action Filter - Caching and Compression

Wednesday, April 02, 2008 2:41 PM by azamsharp

Very very nice!

Keep up the good work!

# re: ASP.NET MVC Action Filter - Caching and Compression

Wednesday, April 02, 2008 5:23 PM by maddog

Hey What utility are you using in the screenies???

# 本周ASP.NET英文技术文章推荐[03/23 - 04/05]:C#、Visual Studio、MVC、死锁、Web 2.0 API、jQuery、IIS7、FileUpload

Friday, April 04, 2008 11:28 PM by Dflying Chen

摘要本期共有9篇文章:提高C#和VisualStudio2008生产力的10个技巧ASP.NETMVCAction过滤器:缓存和压缩程序停止工作及其解决方法:第一部分:死锁调用D...

# re: ASP.NET MVC Action Filter - Caching and Compression

Saturday, April 05, 2008 11:18 PM by Omar AL Zabir

Very useful code. Thanks for introducing these.

One suggestion about compression, it's always better to use IIS 6.0's built in compression over any ASP.NET code, whether a filter or an HttpModule when it comes to scalability. IIS Native compressor always performs better than the .NET components. Moreover, you can increase/decrease compression ratio and balance speed over size.

See here how to turn on IIS compression on dynamic ASP.NET requests:

msmvps.com/.../iis-6-compression-quickest-and-effective-way-to-do-it-for-asp-net-compression.aspx

However, in IIS, you specify which extensions to compress. Now that MVC is almost extensionless, this introduces an interesting challenge - how will IIS know which extension to compress?

# re: ASP.NET MVC Action Filter - Caching and Compression

Sunday, April 06, 2008 8:30 PM by Kazi Manzur Rashid

@azamsharp: Thanks. Sure I will.

@Mad Dog: Print Screen + MS Paint.

@Omar: Thanks for comments, Yes, I agree IIS Compression is always better but most of the shared hosting does not allow to modify the IIS metabase. For those who are running dedicated IIS6 can surly turn on the compression like you mentoned in your blog.

# 【收藏】本周ASP.NET英文技术文章推荐[03/23 - 04/05]:C#、Visual Studio、MVC、死锁、Web 2.0 API、jQuery、IIS7、FileUpload

Thursday, April 10, 2008 3:16 AM by Jacky_Xu

摘要

本期共有9篇文章: 提高C#和VisualStudio2008生产力的10个技巧

ASP.NETMVCAction过滤器:缓存和压缩

程序停止工作及其解决方法:第一部分...

# re: ASP.NET MVC Action Filter - Caching and Compression

Saturday, April 26, 2008 6:31 AM by yang

Is it possible to do this on the controller level. Say I want to compress all the actions of the controller. Is there any other way to apply this attribute to every action of the controller?

# re: ASP.NET MVC Action Filter - Caching and Compression

Saturday, April 26, 2008 9:30 PM by Kazi Manzur Rashid

@yang :

Sure, just apply it in your controller instead of the action methods.

# re: ASP.NET MVC Action Filter - Caching and Compression

Wednesday, May 21, 2008 11:47 AM by jamil

I have xml file I loaded into cache

is there any way I can see the cache content and modify it

if you have some example please send

thanks

# re: ASP.NET MVC Action Filter - Caching and Compression

Monday, June 23, 2008 3:59 PM by shaun

You should update the sample to change the FilterExecutedContext (ActionExecutedContext) and FilterExecutingContext (ActionExecutingContext) to reflect the changes in preview 3.

I also made a minor change to the caching filter to set the cacheability to HttpCacheability.NoCache when the duration is <= 0. Not sure if that was necessary but I wanted to make sure that a few actions were not cached.

Other than that, thanks for the great post!

# ASP.NET MVC Archived Buzz, Page 1

Friday, July 04, 2008 8:53 AM by ASP.NET MVC Archived Buzz, Page 1

Pingback from  ASP.NET MVC Archived Buzz, Page 1

# re: ASP.NET MVC Action Filter - Caching and Compression

Thursday, July 24, 2008 8:05 AM by Steve Gentile

Excellent on the compression part!

I updated for Preview 4 - one minor change:

public override void OnActionExecuting(ActionExecutingContext filterContext)

Now accepts a 'ActionExecutingContext'

Thanks again!

# asp.net mvc 资源

Thursday, August 14, 2008 12:52 AM by 梦想永存

mvc 官方站点:http://asp.net/mvc mvc 精品文章:【翻译】ASP.NET MVC深度接触:ASP.NET MVC请求生命周期 ASP.NET MVC Action...

# asp.net mvc 资源

Wednesday, September 10, 2008 11:17 PM by 梦想永存

mvc 官方站点:http://asp.net/mvc mvc 精品文章:【翻译】ASP.NET MVC深度接触:ASP.NET MVC请求生命周期 ASP.NET MVC Action...

# ASP.NET MVC Action Filter - Caching and Compression - Kazi Manzur Rashid's Blog

Saturday, November 15, 2008 3:10 PM by DotNetShoutout

Your Story is Submitted - Trackback from DotNetShoutout

# ASP.NET MVC Action Filter - Caching and Compression

Wednesday, November 19, 2008 5:10 PM by DotNetShoutout

Your Story is Submitted - Trackback from DotNetShoutout

# ASP.NET MVC Caching and Compression &laquo; Redditech &#8216;Ground Zero&#8217; Blog

Pingback from  ASP.NET MVC Caching and Compression  &laquo; Redditech &#8216;Ground Zero&#8217; Blog

# re: ASP.NET MVC Action Filter - Caching and Compression

Wednesday, March 11, 2009 6:23 PM by dkarantonis

Great article!

Any idea of the reason that one should implement the action filter you suggest for caching output instead of using a cache profile on the web.config file and call the  [OutputCache(CacheProfile = "myCahceProfile")] on the action?

This way, any changes to the caching profile (duration for example) can be implemented in the web.config file without even recompiling the application.

thanks

# ASP.NET MVC : Un filtre pour contrôler la compression de la réponse

Wednesday, March 18, 2009 11:19 AM by Code is poetry

Une des grandes forces de ASP.NET MVC est son extensibilité. Les action filters sont un de ces points

# re: ASP.NET MVC Action Filter - Caching and Compression

Friday, April 17, 2009 4:53 AM by DevareattyVaH

nice, really nice!

# GZip and Deflate Compression Filter for ASP.Net MVC

Saturday, May 02, 2009 5:17 PM by 58bits - tech

# 58bits - tech - GZip and Deflate Compression Filter for ASP.Net MVC

Pingback from  58bits - tech - GZip and Deflate Compression Filter for ASP.Net MVC

# GZip and Deflate Compression Filter for ASP.Net MVC

Saturday, May 02, 2009 5:20 PM by 58bits - tech

# 58bits - tech - GZip and Deflate Compression Filter for ASP.Net MVC

Pingback from  58bits - tech - GZip and Deflate Compression Filter for ASP.Net MVC

# GZip and Deflate Compression Filter for ASP.Net MVC

Saturday, May 02, 2009 5:24 PM by 58bits - tech

# 58bits - tech - GZip and Deflate Compression Filter for ASP.Net MVC

Pingback from  58bits - tech - GZip and Deflate Compression Filter for ASP.Net MVC

# Social comments and analytics for this post

Tuesday, November 10, 2009 2:06 AM by uberVU - social comments

This post was mentioned on Twitter by kuckuvn: http://bit.ly/2BuVKz #MVC CACHING AND COMPRESSION

# :NET MVC cache http://weblogs.asp.net/rashid/archive/2008/03/28/asp-net-mvc-action-filter-caching-and-compression.aspx

Friday, January 29, 2010 12:28 PM by Twitter Mirror

:NET MVC cache http://weblogs. asp.net /rashid/archive/2008/03/28/asp-net-mvc-action-filter-caching-and

# asp.net mvc filtering: http://weblogs.asp.net/rashid/archive/2008/03/28/asp-net-mvc-action-filter-caching-and-compression.aspx

Monday, March 29, 2010 11:25 AM by Twitter Mirror

# Asp.Net MVC Cache and Compress Aspx Pages &#8211; weirdlover - I make love to asp.net mvc, c#, vb, legos, ladies, etc.

Pingback from  Asp.Net MVC Cache and Compress Aspx Pages  &#8211;   weirdlover  - I make love to asp.net mvc, c#, vb, legos, ladies, etc.

# ASP.NET MVC| [MVC]ASP.NET MVC Action Filter &#8211; Caching and Compr | Mikel

Pingback from  ASP.NET MVC|  [MVC]ASP.NET MVC Action Filter &#8211; Caching and Compr | Mikel

# IIS 6.0 dynamic compression + MVC compression filters = double-gzipped content? | DeveloperQuestion.com

Pingback from  IIS 6.0 dynamic compression + MVC compression filters = double-gzipped content? | DeveloperQuestion.com

# Caching in asp.net-mvc ??? HTMLCoderHelper.com

Sunday, November 14, 2010 5:46 AM by Caching in asp.net-mvc ??? HTMLCoderHelper.com

Pingback from  Caching in asp.net-mvc ??? HTMLCoderHelper.com

# IIS 6.0 dynamic compression + MVC compression filters = double-gzipped content?

Pingback from  IIS 6.0 dynamic compression + MVC compression filters = double-gzipped content?

# Caching in asp.net-mvc - Question Lounge

Friday, February 04, 2011 9:05 AM by Caching in asp.net-mvc - Question Lounge

Pingback from  Caching in asp.net-mvc - Question Lounge

# Compressão GZIP no ASP .NET MVC | Michel Banagouro

Thursday, April 07, 2011 8:19 AM by Compressão GZIP no ASP .NET MVC | Michel Banagouro

Pingback from  Compressão GZIP no ASP .NET MVC | Michel Banagouro

# GZip or Deflate compression for asp.net mvc 2 without access to server config - Programmers Goodies

Pingback from  GZip or Deflate compression for asp.net mvc 2 without access to server config - Programmers Goodies

# Kaxi proxie | Jlaura

Saturday, September 08, 2012 6:58 AM by Kaxi proxie | Jlaura

Pingback from  Kaxi proxie | Jlaura