Search - Articles
DevASP.NET for ASP.NET, VB.NET, XML and C# (C-Sharp) Developers Tuesday, March 03, 2009
Dev Articles
Search Directory
ASP.NET
VB.Net
C-Sharp
SQL Server
 

Caching in ASP.NET

Author: Richard Wand

Caching is one of the important features in ASP.NET which provides a powerful way to improve performance of the web application.

 
What is caching?

Caching is the technique to store frequently used data in memory that can be retrieved immediately from memory when needed. Caching provides scalability and improves performance of the web application to a certain extent. It allows you to store page output or application data in memory and reuse it. It saves time and resources of the server because server does not have to recreate data. Caching provides faster page rendering, lower database hits and less consumption of server resources.

Caching in ASP.NET
 
ASP.NET caching provides efficient and fast applications. It provides flexibility for caching at different levels of application. There are three types of caching available in ASP.NET.
 
  1. Output Caching (Page Level Caching)
  2. Fragment Caching (Partial Page Caching)
  3. Data caching (Application Level Caching)

     
  1. Output Caching (Page Level Caching)

    Output caching is used for pages and it is also known as Page Output Caching or Page Level Caching. When the page requested first time, output of the page is cached. This cached output is used to server all subsequent requests for this page. Whenever a page is requested, the output cache engine looks for the cache entry for this page. If it finds the entry, the page request is served from cache which saves rendering and processing time of the complete page otherwise this page request is considered as a first time request.  Output Caching is often used when we have static pages. As the same output is required for the page again and again so the page is cached and subsequent requests are served from cache.

    Output caching requires “@ OutputCache” directive in the aspx page to implement caching at page level. The syntax would be like below:

    <%@ OutputCache  Duration=”seconds”  VaryByParam=”None” %>

    There are other attributes in @ OutputCache directive like “Location”, “VaryByCustom”, “VaryByControl” and “Shared” but “Duration” and “VaryByParam” attributes are required. If you do not provide one of them or both, then a parser error occurs on the first request of the page.

    In “Duration” attribute, you have to provide number of seconds for which you want the page to be cached. “VaryByParam” attribute is used to vary the attribute cache depending on Get or Post parameters. If you do not need to use the functionality provide by “”VaryByParam” attribute, you can set it to “None”. The “Location” attribute is used to set the location where the output is cached. It has the value “Any” by default but you can specify any value like “Server”, “Client” and “ServerAndClient” for OutputCache to server, client and both server and client respectively. 


     
  2. Fragment Caching (Partial Page Caching)

    Most of the times the entire page cannot be cached as some portions of the page are dynamic and others are static so there is need to cache a portion of the page; it is called Fragment Caching or Partial Page Caching. Partial Page Caching allows specific regions of the page to be cached. This technique is more powerful and more useful than entire page caching.

    A portion of the page is wrapped into a user control and “@ OutputCache” directive is included in user control. In this way, the portion of the page which is wrapped into the user control will only be cached and all other part in the page will continue to serve dynamic content. This is also called control level caching. The “@ OutputCache” directive should be in the user control not at the page level.  

    The syntax for the “@ OutputCache” directive is the same as for the page caching.

    <%@ OutputCache  Duration=”seconds”  VaryByParam=”None” %>

     
  3. Data caching (Application Level Caching)

    Data caching allows caching of data as objects for multiple pages in an application. As the cached objects are available at application level so it is also called Application level caching. The cached data for objects are available until the application is restarted. Data caching allows developers to programmatically maintain data across requests.
     
    A Key-Value pair is used to store a value in cache.

    Cache[“Key”] = Value;        // C#
    Cache(“Key”) = Value        ‘ VB.NET

    To retrieve the cached Value, the syntax will be like below:
     
    Value = Cache[“Key”]         // C#
    Value = Cache(“Key”)         ‘ VB.NET

    There is another way to store value in cache in Key-Value pair format with Add() method or Insert() method.

    Cache.Insert(“Key”, Value);        //C#
    Cache.Insert(“Key”, Value)         ‘ VB.NET
     
    Insert() method has overloaded versions. We can also give CacheDependency, SlidingExpiration, AbsoluteExpiration and onRemoveCallBack parameters to Insert() method.

    Cache Dependency

    Dependency defines validity of the cache items. Cache Dependency object indicates what this cache entry’s data is dependent on. This parameter helps to set the dependency value of the data cached. When the dependency changes the cached items are invalidate and removed from the cache.

    There are several types of cache dependencies including dependency on file, dependency on database and dependency on cache items. You can link cache item to a file or a database table or any other cache item.

    Dependency on File

    Cache items can be dependent on a file. It could be an XML file. This is called File Dependency.  The cache items will be expired when there is any modification to the file and the items will be cached again.

    Dependency on Database

    Cache items can also be dependent on database table. This is called Database Dependency. When that database table is altered or updated, the cache items will be expired.

    Dependency on Cache Items

    Cache items can also be dependent on other cache items. A cache item can be removed from the cache when another item is removed.

    Sliding Expiration and Absolute Expiration

    Sliding Expiration means that the cache will expire if the request is not made within a specified duration. It is useful when there are so many data to cache. It will place those items in the cache those are frequently used.

    Absolute Expiration removes the cache items after a specific time period. It means the items will be removed from cache whether these are used are not.

    Both cannot be specified at the same time. When Sliding Expiration is used Absolute expiration will be set to “DateTime.MaxValue” and if the Absolute Expiration is specified then set Sliding Expiration to “TimeSpan.Zero”.

    OnRemoveCallback

    You can specify a function to callback when item is removed.

    Cache Priority

    CacheItemPriority can also be set for the item at the time of inserting. This specifies the remove priority of the item. It is in the following sequence for removal of items. Low priority items are most likely to be removed first.

    Low, BelowNormal, Normal, AboveNormal, High 

 

ASP.NET support different level of caching with flexibility to improve performance at control level, page level and application level.

 

   
Add Article Comment:
Name :
Email Address :
   
Comments :
 
   
<< How to retrieve resource values programmatically in ASP.NET

Disclaimer - Privacy
© 2002-2017 DevASP.net