This document discusses different types of caching in ASP.NET, including output caching, data caching, object caching, class caching, and configuration caching. Output caching stores rendered HTML pages in memory to return cached copies to subsequent requests rather than regenerating pages. Data caching stores data from data sources in memory to fulfill future requests from the cache rather than accessing the data source again. Object caching stores objects like data-bound controls in server memory. Class caching caches compiled web pages or services in server memory. Configuration caching stores application configuration information in server memory.
2. Caching
Caching is a technique of storing frequently used
data/information in memory, so that, when the same
data/information is needed next time, it could be directly
retrieved from the memory instead of being generated by the
application.
Caching places frequently used data in quickly accessed media
such as the random access memory of the computer.
3. Caching in ASP.Net
Different Types of Caching:
Output Caching : Output cache stores a copy of the finally
rendered HTML pages or part of pages sent to the client. When
the next client requests for this page, instead of regenerating
the page, a cached copy of the page is sent, thus saving time.
Data Caching : Data caching means caching data from a
data source. As long as the cache is not expired, a request for
the data will be fulfilled from the cache. When the cache is
expired, fresh data is obtained by the data source and the cache
is refilled.
4. Object Caching : Object caching is caching the objects on
a page, such as data-bound controls. The cached data is stored
in server memory.
Class Caching : Web pages or web services are compiled
into a page class in the assembly, when run for the first time.
Then the assembly is cached in the server. Next time when a
request is made for the page or service, the cached assembly
is referred to When the source code is changed, the CLR
recompiles the assembly.
Configuration Caching : Application wide
configuration information is stored in a configuration file.
Configuration caching stores the configuration information in
the server memory.
5. Output Caching:
Rendering a page may involve some complex processes such as,
database access, rendering complex controls etc. Output caching
allows bypassing the round trips to server by caching data in
memory. Even the whole page could be cached.
The Output Cache directive is responsible of output caching. It
enables output caching and provides certain control over its
behavior.
Syntax:
<%@ OutputCache Duration="15" VaryByParam="None" %>
protected void Page_Load(object sender, EventArgs e)
{
Thread.Sleep(10000); Response.Write("This page was
generated and cache at:" + DateTime.Now.ToString());
}
6. Data Caching
The main aspect of data caching is caching the data source
controls. like a database file. These controls derive from the
abstract class DataSourceControl and have the following
inherited properties for implementing caching:
CacheDuration - It sets the number of seconds for which
the data source will cache data.
CacheExpirationPolicy - It defines the cache behavior
when the data in cache has expired.
CacheKeyDependency - It identifies a key for the controls
that auto-expires the content of its cache when removed.
Enable Caching - It specifies whether or not to cache the
data.
7. Example Of Data Caching
For this example, add a label to the page, which would show the
response time for the page.
<asp:Label ID="lbltime" runat="server"></asp:Label>
Add an event handler for the page load event:
protected void Page_Load(object sender, EventArgs e) {
lbltime.Text = String.Format("Page posted at: {0}",
DateTime.Now.ToLongTimeString()); }
8. Object Caching
Object caching provides more flexibility than other cache
techniques. You can use object caching to place any object in the
cache. The object can be of any type a data type, a web control,
a class, a dataset object, etc. The item is added to the cache simply
by assigning a new key name, shown as follows Like:
Cache["key"] = item;