Caching Overview:
Abstract: Caching is a feature of ASP.NET that can dramatically improve the performance of your application by storing the page output or application data across 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 pages or data across HTTP request and reuse them without the expense of recreating 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 level, or output caching, caches the HTML output of dynamic requests to ASP.NET Web pages. The way ASP.NET implements this 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 easy to implement. By simply using the @OuputCache page directive, ASP.NET Web pages can take advantage of this powerful technique. The following is the complete syntax of page output caching directive<%@ OutputCache
Duration="no of seconds“ * required field
Location="Any | Client | Downstream | Server | None"
VaryByControl="control"
VaryByCustom="browser |customstring"
VaryByHeader="headers"
VaryByParam="parameter“ * required field
%>
Example:
<%@OutputCache Duration="60" VaryByParam="none" %>
The Duration parameter specifies how long, in seconds, the HTML output of the Web page should be held in the cache.
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 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:
<%@ OutputCache Duration="60" VaryByParam="none" VaryByControl="MyRadioButtonList"%>
The Duration parameter specifies how long, in seconds, the HTML output of the Web page should be held in the cache.
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.
The VaryByControl parameter allows different cache entries to be made based on the values for a specified control.
ASP. Net provides a full-featured cache engine, Programmatic or data caching takes advantage of the .NET Runtime cache engine to store any data or object between responses. That is, you can store objects into a cache, similar to the storing of objects in Application scope in classic ASP.
The ASP. Net cache is private to each application and stores objects in memory. The lifetime of the cache is equivalent to the lifetime of the application.
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
Comments