Thursday, May 28, 2009

Caching Overview (ASP.Net)

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 Output 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.


  • 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:

<%@ 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.


  • Data Caching
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

Share:

0 comments:

About Me

My photo
Based in Bangalore, INDIA, Amit is a MCC. He has an MCA from the U.P.T.U., INDIA and holds Microsoft certifications including SharePoint 2007/2010/2013. He is an contributor to MSDN and http://amitkumarmca04.blogspot.com/. Amit have experience in SharePoint,ASP.Net with C#, Classic ASP, VB,Macros,HTML,JavaScript,JSON,BootStrap and AngularJS.

Followers

Labels