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 VoidCategory (StringName,Int? Page)
The following shows the screen-shot in firebug when cache FilterIs not applied:
And this is the screen-shot when the cache FilterIs 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 VoidCategory (StringName,Int? Page)
The following shows when compressionIs not applied:
And this is the screen-shot when the compress FilterIs 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 VoidCategory (StringName,Int? Page)
And this is the screen-shot:
Enjoy !!! (From: http://weblogs.asp.net/rashid/archive/2008/03/28/asp-net-mvc-action-filter-caching-and-compression.aspx)