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
 

What are Cache Profiles and how to use cache profiles in ASP.NET?

Author: Richard Wand

Cache profiles allow developers to define particular settings for multiple pages in ASP.NET.

 
What are Cache Profiles?
 
At individual page level, “@ OutputCache” directive is used to set different attributes. Many times we need to set same values for some attributes for all pages that uses “@ Output Cache” directive. Cache Profiles are used for same OutputCache settings in multiple pages and these are added in web.config file of the application. Individual page cache settings can be set with the normal way of “@OutputCache” directive attributes. Cache profiles are very helpful when you want to give different type of access to your users. Three different profiles can have three different settings for three types of users.

Cache profiles make it easy for developers to set and change cache settings that are same for different pages. Developers only need to set cache setting in web.config file and need to provide name of the profile in “@ OutputCache” directive. When they need to change some cache settings, they can change only in web.config file and these changes will make its effect on all pages that have “@ OutputCache” directive with name of this profile.

 

How to use cache profiles?
 
  1. Open Visual Studio 2010, go to File from menu > New > Web Site
  2. Create ASP.NET Empty Web Site either in VB.NET or C#
  3. Go to Website from menu > Add New Item > Web Form  and Click on Add
  4. Go to Website from menu again > Add New Item > Web Form  and Click on Add
  5. Write code below in between <div> tag in Default.aspx page

     
    <h2>Cache Profile Example, Duration: 10 seconds</h2>
       
    <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
       
    <br />
       
    <asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl="~/Default2.aspx">Go to Second Page</asp:HyperLink>
     
  6.  Write code below in Page Load method in code behind file Default.aspx.cs or Default.aspx.vb

    C#
     
    Label1.Text = "Now the Time is: " + DateTime.Now.ToString();
     
    VB.NET
     
    Label1.Text = "Now the Time is: " & DateTime.Now.ToString()
     
  7.  Write code below in between <div> tag in Default2.aspx page

    <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
       
    <br />
       
    <asp:HyperLink ID="HyperLink1" runat="server" NavigateUrl="~/Default.aspx">Go Back to First Page</asp:HyperLink>
     
  8.  Write code below in Page Load method in code behind file Default2.aspx.cs or Default2.aspx.vb

    C#
     
    Label1.Text = "Now the Time is: " + DateTime.Now.ToString();
     
    VB.NET
     
    Label1.Text = "Now the Time is: " & DateTime.Now.ToString()
  9. Open web.cofig file and add code below in between <system.web> tag.

    <system.web>
       
           <caching>
       
                  <outputCacheSettings>
       
                         <outputCacheProfiles>
       
                                <add name="CacheProfileSample" duration="10"/>
       
                         </outputCacheProfiles>
       
                  </outputCacheSettings>
       
           </caching>
       
    </system.web>


    First add <caching> tag in between <system.web> tag. Add <ouputCacheSettings> tag in between <caching> tag. <outputCacheProfiles> tag has to be in between <ouputCacheSettings> tag. Provide name of the Cache profile and any other attribute you want to define for your cache settings in <add> tag. For this example name of the profile is “CacheProfileSample” and “duration” attribute is set to 10 seconds.
     
  10.  Now add “@ OutputCache” directive below “@ Page” directive at second line in both Default.aspx and Default2.aspx files.

    <%@ OutputCache CacheProfile="CacheProfileSample" VaryByParam="None" %>


    We have to provide Cache profile name in “CacheProfile” attribute and “VaryByParam” attribute. For this example Cache profile name is “CacheProfileSample” and “None” is set for “VaryByParam” attribute.
     
  11. Save files, build website and press “F5” or “Ctrl + F5”.
  12. Refresh page, you will see that the time is cached for 10 seconds in Default.aspx page.
  13. Go to second page by clicking on the “Go to Second Page” link.
  14. Refresh page, you will see that the time is cached for 10 seconds in Default2.aspx page also. 

 Using Cache profiles is a better approach to set Cache settings.

   
Add Article Comment:
Name :
Email Address :
   
Comments :
 
   
<< Page Output Caching in ASP.NET

Disclaimer - Privacy
© 2002-2017 DevASP.net