.NET provides three primary forms of
caching:
page level output caching easy to implement
user control level output caching (or fragment caching) easy to implement
Using Cache Object More flexible throughout every layer of an
application
1. Page Level Output Caching
The simplest form of caching, output caching simply keeps a copy
of the HTML that was sent in response to
a request in memory. Subsequent requests are then sent the cached output until the cache expires, resulting in
potentially very large performance gains (depending on how much effort was required to create the original page output—sending cached output is always very fast and fairly constant).
a request in memory. Subsequent requests are then sent the cached output until the cache expires, resulting in
potentially very large performance gains (depending on how much effort was required to create the original page output—sending cached output is always very fast and fairly constant).
You have to give the duration in seconds for the page to be cached.
Implementation:
<%@ OutputCache Duration="60" VaryByParam="*" %>
This directive, as
with other page directives should appear at the top of the ASPX page, before
any output.
2. Fragment Caching, User
Control Output Caching
Often,
caching an entire page is not feasible, because certain parts of the page are
customized for the user.
However, there may be other parts of the page that are common to the entire application. These are perfect candidates for caching, using fragment caching and user controls. Menus and other layout elements, especially ones that are dynamically generated from a data source, should be cached with this technique. If need be, the cached controls can be configured to vary based on the changes to its controls (or other properties) or any of the other variations supported by page level output caching. Hundreds of pages using the same controls can also share the cached entries for those controls, rather than keeping separate cached versions for each page.
However, there may be other parts of the page that are common to the entire application. These are perfect candidates for caching, using fragment caching and user controls. Menus and other layout elements, especially ones that are dynamically generated from a data source, should be cached with this technique. If need be, the cached controls can be configured to vary based on the changes to its controls (or other properties) or any of the other variations supported by page level output caching. Hundreds of pages using the same controls can also share the cached entries for those controls, rather than keeping separate cached versions for each page.
Note:
Fragment
caching uses the same syntax as page level output caching, but applied to a
user control (.ascx file) instead of to a web form (.aspx file).
3. Caching API, Using the
Cache Object
Page
and user control level output caching can be a quick and easy way to boost
performance of your site, but the real flexibility and power of caching in
ASP.NET is exposed via the Cache object. Using the Cache object, you can store
any serializeable data object, and control. Dependencies can include time
elapsed since the item was cached, time elapsed since the item was last
accessed, changes to files and/or folders, changes to other cached items, or
(with a little work) changes to particular tables in a database.
Storing Data in the Cache
The
simplest way to store data in the Cache is simply to assign it, using a key,
just like a HashTable or Dictionary object:
Cache["key"] = "value";
This
will store the item in the cache without any dependencies, so it will not
expire unless the cache engine removes it in order to make room for additional
cached data. To include specific cache dependencies, the Add() or Insert() method
is used. Each of these has several overloads. The only difference between Add() and Insert() is that Add()
returns a reference to the cached object, while insert() has no return value (void in C#, Sub in VB).
Summary:
If you think that
information containing in the page is required repeatedly and this page doesn’t
have any customized usercontrol then you should go for page level caching but
if the page contains some user controls and data is generating dynamically from
datasource in some parts of web page then you should go for user control level caching , finally if you
don’t want to apply above techniques because you want to apply caching on
objects rather than page or usercontrol then you should go for the caching API , now you can apply caching
in customize manner .
References:
Great Talha Brother good effort and good piece of work:)
ReplyDelete