Thursday, July 17, 2008

Caching in ASP .NET

ASP.NET supports multiple levels of caching:
1. Output caching
2. Fragment caching
3. Data caching


Benefits:
Can increase performance by reducing trips to the database


Challenges:
Requires correct design and enough memory to be effective


ASP.NET provides built-in caching support that enables re-use of work:
1. Full Page Caching (Vary by params, language, user-agent)
2. Partial Page Caching (Enables portions of pages to be cached)
3. Web Service Caching (Vary by parameters and methods)
4. Cache Engine: Extensible Cache API (Enables arbitrary objects to be cached)

Cache Dependencies:
1. File Based dependencies
2. Key Based dependencies
3. Time Based dependencies

How to do Caching:
Cache the content of an entire ASP.NET page
–Declarative:
<%@ OutputCache Duration="60" VaryByParam="None"%>
–Programmatic:
•Response.Cache.SetExpires(DateTime.Now.AddSeconds(60))
•Response.Cache.SetCacheability(HttpCacheability.Public)
•Response.Cache.SetSlidingExpiration(True)

Example of Output Cache:
<%@ OutputCache Duration="20" VaryByParam="none" %>

Output Cache

Now :<%= Now() %>


The content of the page will be same till 20 Sec.
of the creation of cache of this output.





No comments: