Consider
an Example. An application needs to query a database which has 10 million records
after every four seconds to retrieve same type of result. Let us assume that database
will get updated in an hour. Now, in this situation data retrieved from the
database will remain the same for about 50 minutes. So querying this database
after every four seconds will cause network congestion and the load on the
database /web server. To solve the above problem we use caching. Caching is
used to store data either on web server or client machine for a specified
duration. So, in the above example, we cache the data for a fixed period of time
(assume 50 minutes), then when the first time query is executed, results will
be stored in cache. Now in the next time frame( here it is 50 minutes)the same
type of requests for the data will get
the data from the query itself without executing query again on 10 million records.
Hence, caching enhances user experience and gain performance in web applications.
ASP.NET
provides three kinds of caching techniques:
1)
Page level or output caching:
In this caching, the
complete HTML output of a presented ASP.NET page is stored in the cache and any
other requests for the same page is sent from the cache itself. For each
request entry of that page is find the cache. If an entry is found HTML output
is sent to the client. If it doesn’t find an entry, then it saves a copy of
HTML output in the cache for later use. It is extremely beneficial in static pages
(pages whose content doesn’t change) as everything that is presented on the
page is cached. It is implemented by adding @output
cache directive in the web page as shown below:
<%@
OutputCache duration=”100” VaryByParam=”none” %>
Duration
specifies time for which page exists in cache and varyByParam attribute is used
to cache different copies of the page based on the parameters received in the
form of HTTP POST or HTTP GET .For example, when a user requests for personal
record of an employee with id =009 and VaryByParam=”none” , page will be
presented and output will be cached . Now when another user seeks records for
the employee id=007 then the caching engine will not be able to see the
difference and the cached output for id=009 will be sent. This problem can be avoided
by setting VaryByParam=”emp_id”
2)
Partial page output caching: As the name suggests, it is used when
we do not require entire page to be cached rather only some of the contents of
the page need to be cached. Here we create a single or more user controls for
those parts of the page and decide the caching policies of those user controls.
To implement this caching we use <partial caching (500)> .When
someone requests for the page where this user control is used, the cache engine
will store the user control for 500 sec in its output cache and will be
retrieved for the subsequent requests
3) Data caching: it is used on data which is very common.
It is very useful when you need to cache your own custom data into cache. For
example, when we need to cache a data set which can be used later in the
application, we require data caching. It is implemented by cache class or system.web.caching.cache
namespace. We use cache class to store the data while handling simple data. It
has the format as cache (“myvalue”)= value. Following tasks need to be
performed during data caching:
a)
Adding items in cache: To add items in cache we use- Cache. Insert (“My value”,
value).We can also add expiration time of 5 mins using-Cache.Insert(“myvalue”,value,nothing,datetime.now.addminutes(5))
b)
Retrieving data from the cache: It is done by the following statement-
value=Cache(“myvalue”).
c)
Deleting data from the cache: Although it is done automatically based on
expiration time, we can manually do so by- Cache.Remove(“mydata”).
No comments:
Post a Comment