ASP.NET - Caching Feature

          

ASP.NET 2.0


ASP.NET 2.0 Features
Developer Productivity
            Master Pages
            New Code-Behind Model in ASP.NET 2.0
            Creating & Sharing Reusable Components 
            New ASP.NET 2.0 Controls
                       Data Controls
                       Security Controls
                       Other New Controls
                       Validation Groups
            Themes
            Web Parts Framework
            Visual Studio 2005 Improvements
Administration and Management
Speed and Performance
            Caching Feature
ASP.NET Caching Feature 

Professional ASP.NET 2.0 Databases

This excerpt from Professional ASP.NET 2.0 Databases   by Thiru Thangarathinam, is printed with permission from
Wrox Publication.
<<Previous

Caching Feature
Caching with the DataSource Controls
Using SQL Cache Invalidation
Using the Substitution Control

Also read

Caching interview questions with answers

Caching Feature

Caching is defined as temporary storage of data for faster retrieval on subsequent requests. In ASP .NET 2.0, the caching support is integrated with the DataSource controls to cache data in a web page. ASP.NET 2.0 also now includes automatic database server cache invalidation. This powerful and easy-to-use feature allows developers to aggressively output cache database-driven page and partial page content within a site, and have ASP.NET automatically invalidate these cache entries and refresh the content whenever the back-end database changes. ASP .NET 2.0 also introduces the Substitution control, which allows you to link dynamic and cached content in a web page.

Caching with the DataSource Controls

The DataSource controls enable you to cache database data while connecting a .NET application to a database. The DataSource control provides various properties, such as EnableCaching, which you can use to automatically cache the data represented by a DataSource control. The syntax to cache a database table in a memory for 120 seconds is:

<asp:SqlDataSource ID=”SqlDataSource1” EnableCaching=”True” CacheDuration=”120”
ConnectionString=”Server=localhost;database=AdventureWorks;uid=user;pwd=word;”
SelectCommand=”SELECT * FROM Production.ProductCategory” Runat=”server”/>

The above syntax caches a database table, ProductCategory, by setting the EnableCaching property of the DataSource control to True. The CacheDuration property of the DataSource control specifies the time, in seconds, for caching the data before it is updated in a database containing the ProductCategory table. The value of the Time parameter is set to 120 to cache data for two minutes.

Using SQL Cache Invalidation

The Cache API introduced with ASP.NET 1.x was a powerful feature that can be immensely useful in increasing the performance of a web application. The Cache API also allows you to invalidate items in the cache based on some predefined conditions, such as change in an XML file, change in another cache item, and so on. Using this feature, you can remove or invalidate an item from the cache when the data or another cached item changes. However, the Cache API in ASP.NET 1.x versions did not provide a mechanism to invalidate an item in the cache when data in a SQL Server database changed. This is a very common capability that many web applications require. Now with ASP.NET 2.0, Microsoft has introduced a new cache invalidation mechanism that works with SQL Server as well. Using this new capability, you can invalidate an item in the Cache object whenever the data in a SQL Server database changes. This built-in cache invalidation mechanism works with SQL Server 7.0 and above. However, with SQL Server 7.0 and 2000, only table-level cache invalidation mechanism is supported. The next release of SQL Server (named SQL Server 2005) will also feature a row-level cache invalidation mechanism, providing a finer level of accuracy over the cached data. To enable the SQL Server–based cache invalidation mechanism, you need to do the following:

  1. Add a <caching> element to the Web.config file, and specify the polling time and the connection string information.
  2. Enable SQL cache invalidation at the database and table levels by using either the aspnet_regsql utility or the SqlCacheDependencyAdmin class. This is not required if you are using SQL Server 2005 as your database.
  3. Specify the SqlCacheDependency attribute in the SqlDataSource control.

That’s all you need to do to leverage SQL Server cache invalidation from your ASP.NET pages.

Using the Substitution Control

ASP.NET 2.0 provides a new control called the Substitution control, which enables you to insert dynamic content into a cached web page. For example, you can display the name of an end user, which is dynamically generated in a cached web page containing some text or images. The Substitution control provides a property called MethodName, which represents the method called to return the dynamic content. Listing 1-4 shows an example of the Substitution control in action.

Listing 1-4: Partial Page Caching Using Substitution Control

<%@ Page Language=”C#” %>
<%@ OutputCache Duration=”6000” VaryByParam=”none” %>
<script runat=”server”>
        static string GetRandomNumber(HttpContext context)    
        {
         int randomNumber;
         randomNumber = new System.Random().Next(1, 10000);
         return randomNumber.ToString();
        }
</script>
<html>
     <head>
              <title>Use of Substitution control to implement Partial Caching</title>
     </head>
<body>
            <form id=”form1” runat=”server”>
                                 The random number generated is:
                       <asp:Substitution ID=”Substitution1” MethodName=”GetRandomNumber”
                       Runat=”Server” />
                       <p>
                                The current time is <%= DateTime.Now.ToString(“t”) %>.
                                    It never changes since the page is cached.
                       </p>
            </form>
</body>
</html>

At the top of the page, the OutputCache directive is used to cache the contents of the page in memory. The Duration attribute of the OutputCache directive is set to 6000 milliseconds. The VaryByParam attribute indicates whether or not ASP.NET should consider the parameters passed to the page when caching. When VaryByParam is set to none, no parameters will be considered; all users will receive the same page no matter what additional parameters are supplied. The MethodName attribute of the Substitution control is set to a method named GetRandomNumber, which simply returns a random number between 1 and 10,000. Note that the return value of the GetRandomNumber method is a string, because the HttpResponseSubstitutionCallback delegate always requires a return type of string. When you make a request for the page through the browser, you will find that the displayed current time always remains the same, whereas the portion of the page that is generated by the substitution control keeps changing every time. In this case, it displays a random number between 1 and 10,000 every time someone requests the page.

<<Previous



Write your comment - Share Knowledge and Experience


More Related links 

ASP.NET Navigation Methods

This articles describes the navigation ways available in ASP.NET.

.Net Framework 

This includes introduction of .Net framework, .Net framework architecture, role of assembly and GAC.

ASP.NET Master Pages 

This article has content about master page, its advantages and how to create master page in ASP.NET. It also describes about multiple master page.

ADO.NET

This is complete article on ADO.NET with code and interview questions

Transparent caching with AOP 

To get better results in terms of speed and resources used, it's suggested to use a cache. We can store in it the results corresponding to the methods' invocations as key-value pairs: method and arguments as key and return object as value..............

ASP.NET 2.0 New Controls

In addition to the new controls, ASP.NET 2.0 also provides numerous enhancements to existing controls that make these controls more versatile than ever before in building component-based web pages. For example, the Panel control now has a DefaultButton property that specifies which button should be clicked if the user presses the Enter key while the panel has the focus.........

ASP.NET 2.0 Validation Groups

With ASP.NET 2.0, Microsoft introduces a new feature known as validation groups, which enables you to create different groups of validation controls and assign them to input controls, such as text boxes. You can assign a validation group to a collection of input controls if you want to validate the collection of input controls on the same criteria............

 

Latest placement tests
Latest links
 
Latest MCQs
» General awareness - Banking » ASP.NET » PL/SQL » Mechanical Engineering
» IAS Prelims GS » Java » Programming Language » Electrical Engineering
» English » C++ » Software Engineering » Electronic Engineering
» Quantitative Aptitude » Oracle » English » Finance
Home | About us | Sitemap | Contact us | We are hiring