ASP.NET 2.0 Caching
http://www.csharpfriends.com
World's Greatest C# Community    
Home Articles C# Forums Books C# Syntax C# Spec C# Jobs free Source Code Advertise About
 

Control Panel

[ Sign In / register ]
Points   
Notes 
My Forums
My Tutorials
My Profile

Resources

Learn
 Articles
 QuickStarts
 C# Spec
 Whitepapers
 Tools
 Class Browser
 C# Code Generator
 Links
 Misc Rss Feeds
 Code Highlight
 411 Directory
 FREE magazines
 freevb.net

Reviews
  ASP.NET Hosting

Source Code
 Get Version 1.0



C# Consulting
AspDotNetStoreFront
Chapter:   Getting Started
Current Lesson:
ASP.NET 2.0 Caching
[Latest Content]
A | B | C | D | E | F | G | H | I | J | K | L | M | N | O | P | Q | R | S | T | U | V | W | X | Y | Z | ALL
[prev. Lesson]  Teach Yourself: ASP.NET 2.0 in 24 hours [next Lesson]  BVCommerce by BVSoftware
ASP.NET 2.0 Caching
  by: webmaster

Probably one of the coolest features with ASP.NET is caching, ASP.NET 2.0 has new caching features that I will explore in this article.

The new caching features in ASP.NET 2.0 include:

  • Custom Dependancies
  • Fragment Caching API
  • Post-cache Substitution
  • Cache Profiles
  • Cache Configuration
  • SQL Cache invalidation

    I will go over these new features in my next article, for now let’s try and understand the big picture.

    If you are new to caching, have a look at Rob Howards caching video here.

    To make things simple, let’s go ahead and look at the different kinds of caching that you can use in your web applications:

  • Output Caching
  • Fragment Caching
  • Data Caching
  • Cache Configuration

    Output Caching

    Performance is improved in an ASP.NET application by caching the rendered markup and serving the cached version of the markup until the cache expires. For example, if you have a page that displays user information from a database, caching will help improve performance by serving the page from memory instead of making a connection to the database on each page request.

    You can cache a page by using the OutputCache API or simply by using the @OutputCache directive.

    <%@ Page Language=”C#” %>
    <%@ OutputCache Duration=”15” VaryByParam=”none” %>
    

    The above cache directive will cache the page for 15 minutes.

    So output caching is great for when you want to cache an entire page.

    Fragment Caching

    In many situations caching the entire page just isn’t going to cut it, for some reason or another you require specific sections of the page to display live information. One way to improve performance is to analyze your page and identify objects that require a substantial overhead to run. You can build a list of these objects that are expensive to run, and then cache them for a period of time using fragment caching.

    For example, say your page default.aspx consists of three user controls. After looking over the code, you identified that you can cache one of them. You can simply add the caching directive to the top of the user control:

    <%@ Control %>
    <%@ OutputCache Duration=”5” VaryByParam=”none” %>
    

    Now keep in mind that the actual page that contains the control is not cached, only the user control. This means that the default.aspx page will be rendered each and every page request, but the user control is only ‘run’ every 15 minutes.

    Data Caching

    So we know that we can cache an entire page, or a fragment of a page by caching down to the user control level. Wouldn’t it be great if we could cache down to the object level? The good news is you can with ASP.NET Data caching.

    The cache consists of a dictionary collection that is private to each application in memory. To insert items in the cache simply provide the collection with a unique name:

    Cache[“someKey”] = myObject;
    

    Retrieving the object form the cache:

    myObject = (MyObject)Cache[“someKey”];
    

    It is a good time to point out that you should always remember to check for null, and be sure to caste to your datatype.

    Cache Configuration

    If you are familiar with how caching worked in ASP.NET 1.0, you realize that managing all the cache directions for all your pages could potentially get out of hand. ASP.NET 2.0 introduces Cache Profiles that helps you centrally manage your cache. Cache settings can be inherited by your pages, and overridden if required by using the OutputCache directive.

    The page directive looks pretty much the same, expect this time it references a cache profile that you defined in your web.config file.

    <%@ Page Language=”C#” %>
    <%@ OutputCache CacheProfile=”myCacheProfile1” VaryByParam=”none” %>
    
    <?xml version="1.0"?>
    <configuration>
        <system.web>
            <caching>
                <outputCacheSettings>
                    <outputCacheProfiles>
                        <add name=" myCacheProfile1" duration="60" />
                    </outputCacheProfiles>
                </outputCacheSettings>
            </caching>    
        </system.web>
    </configuration>
    

    So each and every page that references the ‘myCacheProfile1’ can be centrally managed in the web.config, this means that any changes to the cache settings in the web.config file will be automatically changed on all your referenced pages.

    References:

    Performance: http://msdn2.microsoft.com/en-us/library/44e5wy6k.aspx

    Caching: http://msdn2.microsoft.com/en-us/library/xsbfdd8c.aspx

    Health Monitoring: http://msdn2.microsoft.com/en-us/library/ms178701.aspx


  • 1 


    Build Your Own ASP.NET Website Using C# & VB.NET

    Chapter:  Getting Started
    Current Lesson:
    ASP.NET 2.0 Caching
    [Latest Content]
    A | B | C | D | E | F | G | H | I | J | K | L | M | N | O | P | Q | R | S | T | U | V | W | X | Y | Z | ALL
    [prev. Lesson]  Teach Yourself: ASP.NET 2.0 in 24 hours [next Lesson]  BVCommerce by BVSoftware


    Today's Top Movers
    vulpes 6800
    MadHatter 2220
    jal 867
    Jeff1203 857
    muster 791

    Yesterday Top Movers
    shakti sin.. 9
    MadHatter 3
    Al_Pennywo.. 2
    C#fanatic 2
    snaso 1

    Monthly Leaders
    vulpes 6800
    MadHatter 2260
    jal 867
    Jeff1203 857
    muster 791

    Top Members
    mosessaur 18457
    Rincewind 7074
    stanleytan 6995
    vulpes 6800
    Gsuttie 6046

    Great Offers
    .net hosting
    Go To My Pc
    Remote Pc Control
    zonealarm
    spam blocker
    web hosting directory
    ad server   C#
    snadtech GoToMyPc

    Top of Page

    Advertise | About | Link To Us | Privacy Notice Copyright © 2003 - 2005 CSharpFriends.com  All Rights Reserved  Visual C# Developer Center