ASP.Net supports the below caching techniques,
1. Page Level Caching(Output
Caching)
2. Fragment caching or Control
(Output Caching)
3. Application or Data
Caching(also programmatic caching)
Page Level Caching
Using this technique we can cache the whole aspx page
output. Since we are caching page output, it is called as page output caching.
In this technique, the page will be executed for the first time and will be
cached for the duration specified in the expiry setting. The subsequent requests
will be served with the cached page output instead of processing the page again.
This is achieved by using @OutputCache directive in
the aspx page. The below setting will cache the Default.aspx page for 120
seconds.
<%@ Page Language="C#" AutoEventWireup="true"
CodeFile="Default.aspx.cs" Inherits="_Default" %>
<%@ OutputCache Duration="120" VaryByParam="None"%>
It is compulsory to have Duration and VaryByParam
parameter with some settings in @OutputCache directive for the caching to work.
There will be situations where we need cache the page
for different input parameters on the page. The input will be querystring in GET
and form fields in POST requests. It is obvious that the page output will be
different for different input parameters. In this case, to cache the output of
different versions of page we need to set the parameter name(s) in VaryByParam
parameter. For example, if you have querystring called catid and if you like to
cache the page for different category values then the below will work,
<%@ Page Language="C#" AutoEventWireup="true"
CodeFile="Default.aspx.cs" Inherits="_Default" %>
<%@ OutputCache Duration="120"
VaryByParam="catid"%>
The same setting will apply to a POST request with catid
as form field. The above setting will cache the page out with different catid
values.
The None value in VaryByParam will make ASP.Net to cache
the page without any input parameter. You can give multiple parameters by
separating the parameter with semicolon(;). If you want to cache the page for
all the parameter values, then specify VaryByParam="*".
The other parameters which helps in caching different
version of page is,
Ø
VaryByControl
Ø
VaryByHeader
Ø
VaryByCustom
Ø
VaryByContentEncodings
VaryByControl
This parameter accepts semicolon separated list of
control ids to cache the different versions of page. See below,
<%@ Page Language="C#" AutoEventWireup="true"
CodeFile="Default.aspx.cs" Inherits="_Default" %>
<%@ OutputCache Duration="120"
VaryByControl="txtEmpID"%>
The above setting will cache the page for different
values in txtEmpID.
Note
If you specify the VaryByControl then VaryByParam is
optional i.e. Duration and either VaryByControl or VaryByParam is mandatory.
VaryByHeader
This setting will cache different versions of the page
depending upon the http header set in the request. Refer below,
<%@ Page Language="C#" AutoEventWireup="true"
CodeFile="Default.aspx.cs" Inherits="_Default" %>
<%@ OutputCache Duration="120" VaryByParam="None"
VaryByHeader="User-Agent" %>
The above setting will cache different version of page
when requested from different user agents i.e. IE, firefox, etc. You can specify
multiple header values separated by semicolon.
VaryByCustom
This can be used to cache with a custom parameter. Refer
this post which speaks about the
parameter usage.
VaryByContentEncodings
This parameter can be used to cache different version of
the page depending on different encoding.
Where the Cached elements are stored ?
The other useful parameter of @OutputCache directive is
Location which dictates where the cached items should be stored. It is an
enumeration of type OutputCacheLocation. The possible values are,
Any
The output cache can be located on the browser client,
on a proxy server, or on the server where the request was processed.
Client
The output cache is located on the browser client where
the request is originated.
Downstream
This make the cache to be stored in proxy servers and
the client that made the request other than the processing server.
Server
The output cache is located on the Web server where the
request was processed
None
This dictates that the cache is stored nowhere. In other
words, the output cache is disabled for the requested page.
ServerAndClient
The output cache can be stored only at the origin server
or at the requesting client.
Fragment caching or Control Caching
Using this technique we can cache a part or fragment of
a page in ASP.Net. The fragment that needs to be cached should be a usercontrol
so that it can be cached. Again, this is done by using @OutputCache directive.
Refer below,
<%@ Control Language="C#" AutoEventWireup="true"
CodeFile="WebUserControl.ascx.cs" Inherits="WebUserControl" %>
<%@ OutputCache Duration="120" VaryByParam="None"
%>
The fragment caching supports VaryByParam, VaryByControl
and VaryByCustom parameters.
|