Caching in ASP.NetIntroduction
Cachingis a feature of ASP.NET that can dramatically improve the performanceof your application by storing the page output or application dataacross HTTP requests.
This article cover:
* What is Caching ?
* Benefits of Caching
* Introducing the Problems that Caching Solves
* Types of Caching
What is caching?
* Caching is a feature that stores data in memory, allowing incoming requests to be served from memory directly.
* In the context of a web application, caching is used to retain pagesor data across HTTP request and reuse them without the expense ofrecreating them.
Benefits of Caching
The following are the benefits of using Caching:
* Faster page rendering
* Minimization of database hits
* Minimization of the consumption of server resources
Types of Caching
* Page Output Caching
* Page Fragment Caching
* Data Caching
* Page Output Caching:
Pagelevel, or output caching, caches the HTML output of dynamic requests toASP.NET Web pages. The way ASP.NET implements this is through an OutputCache engine. Each time an incoming ASP.NET page request comes in, thisengine checks to see if the page being requested has a cached outputentry. 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.
Outputcaching is easy to implement. By simply using the @OuputCache pagedirective, ASP.NET Web pages can take advantage of this powerfultechnique.
The Duration parameter specifies how long, in seconds, the HTML output of the Web page should be held in the cache.
TheVaryByParam parameter is used to indicate whether any GET (QueryString)or POST (via a form submit with method="POST") parameters should beused in varying what gets cached.
* Page Fragment Caching
In addition to output caching an entire page, ASP. Net provides Partial-Page Output
Caching, or Page Fragment Caching. Page Fragment Caching allows specific regions of pages to be cached.
Page Fragment Caching is easy to implement. By simply using the @OuputCache
page directive, ASP.NET Web pages can take advantage of this powerful technique.
The syntax looks like this:
The Duration parameter specifies how long, in seconds, the HTML output of the Web page should be held in the cache.
TheVaryByParam parameter is used to indicate whether any GET (QueryString)or POST (via a form submit with method="POST") parameters should beused in varying what gets cached.
The VaryByControl parameter allows different cache entries to be made based on the values for a specified control.
* Data Caching
ASP.Net provides a full-featured cache engine, Programmatic or data cachingtakes advantage of the .NET Runtime cache engine to store any data orobject between responses. That is, you can store objects into a cache,similar to the storing of objects in Application scope in classic ASP.
TheASP. Net cache is private to each application and stores objects inmemory. The lifetime of the cache is equivalent to the lifetime of theapplication.
To store a value in the cache, use syntax like this:
Cache[“myKey"] = myValue; // C#
Cache(“myKey") = myValue ' VB.NET
To retrieve a value, simply reverse the syntax like this:
myValue = Cache[“myKey"]; // C#
myValue = Cache(“myKey") ' VB.NET