In this series for Caching side we will talk about
·
i) Page caching (called
Output Caching)
·
ii) Data caching
·
iii) Control caching
· iv) Cache Dependencies(like how cache depends on a
file)
·
v) Setting Cache lifetimes
The idea of caching is that information is not constantly
changing, the information that is kind of static over a long period of time
asp.net should not come up every time someone ask for it. So
“Caching is a way to generate the result once and sending that result to multiple different user over coarse of time”.
Lots of different level of caching.
You can cache the output of application data, the entire
page, just a fragment of page like the output of particular control and you can
also do thing like set cache
dependencies , i.e you can say that I don’t want this information for more
than 1 hr as after this gonna change, or I want until
some and some file changes.
Lets do the couple of things.
Page Caching
- Page level, or output caching, caches the HTML output of dynamic requests to ASP.NET Web pages. The way ASP.NET implements this (roughly) is through an Output Cache engine. Each time an incoming ASP.NET page request comes in, this engine checks to see if the page being requested has a cached output entry. If it does, this cached HTML is sent as a response; otherwise, the page is dynamically rendered, it's output is stored in the Output Cache engine.
- Output Caching is particularly useful when you have very static pages.
-
Output caching is easy to
implement. By simply using the
@OuputCache
page directive, ASP.NET Web pages can take advantage of this powerful technique.
Below page directive in your page write
<%@ Page Language="C#"
AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<%@ OutputCache
Duration="5"
VaryByParam="none"
%>
Now the page will cache for 5 sec and its not
gonna change by anything so varybyparam=”none”.
Creating Demo Page :
<%@ OutputCache
Duration="5"
VaryByParam="none"
%>
<body>
<form id="form1" runat="server">
<div>
<h1> Output
Caching Demo</h1>
</div>
<div>
This page is cached for a
duration of 5 seconds. When this page was last cached the date/time was <%=DateTime.Now.ToLongTimeString()%>. Refresh repeatedly and you'll see that this time remains constant until
the cache is expired, then it is updated to the current time.
The reason this time stays
constant is because the HTML generated by this ASP.NET Web page is cached using
Output Caching. To enable Output Caching, simply add the following declaration
to the top of your ASP.NET Web page:
< %@OutputCache
Duration="durationInSeconds" VaryByParam="varyBy" % >
</div>
</form>
Understand VaryByParam parameter
The
VaryByParam
parameter is used to
indicate whether any GET (QueryString) or POST (via a form submit with method="POST"
) parameters
should be used in varying what gets cached. In other words, multiple versions
of a page can be cached if the output used to generate the page is different
for different values passed in via either a GET or POST.
The
VaryByParam
is a useful setting that can be used to cache different
"views" of a dynamic page whose content is generated by GET or POST
values. For example, you may have an ASP.NET Web page that reads in a Part
number from the QueryString and displays information about a particular widget whose
part number matches the QueryString Part number. Imagine for a moment that
Output Caching ignored the QueryString parameters altogether (which you can do
by setting VaryByParam="none"
). If the first user visited the page with QueryString /ProductInfo.aspx?PartNo=4
, she
would see information out widget #4. The HTML for this page would be cached.
The next user now visits and wished to see information on widget #8, a la /ProductInfo.aspx?PartNo=8
. If VaryByParam
is set to VaryByParam="none"
, the
Output Caching engine will assume that the requests to the two pages are
synonymous, and return the cached HTML for widget #4 to the person wishing to
see widget #8! To solve for this problem, you can specify that the Output
Caching engine should vary its caches based on the PartNo
parameter by either specifying it explicitly, like VaryByParam="PartNo"
, or by saying to vary on all
GET/POST parameters, like: VaryByParam="*"
.
In the next section we will see how to use web.config to create the cache profile(another way of doing page caching), how to discard a cache and how can we set the cache dependency.(i.e keep the cache until so and so file doesn't changes).
No comments:
Post a Comment