Adding Cache Dependencies in ASP.NET using VB.NET

In this article you will learn about what is Caching,types of Cache Dependencies and how you can add Cache dependencies.
  • 1487
 

When you use or develop any application the performance is a key requirement for every application. The browser helps with client-side caching of text and images whereas the server-side caching you choose to implement is vital for craeting the best possible performance.
Caching is the Process of storing frequently used data on the server to fulfill subsequent request. Caching increases your application's performance, availbility, scalability.
The class Sysetm.Web.Caching.CacheDependency can monitor a single file or an array of files and folders for changes. when you add an item to cache you can define the Dependency relationships that can force that item to be removed from the Cache under specific activities of Dependencies. Like if the Cache is dependent on File and when the file data changes you want the cache object to be updated.


Types of Cache Dependencies:-

  1. File Dependency:-  This allows to  invalidate a specific Cache item when a disk based file or files change. 
  2. Time Based Expiration:- This allows to invalidate a specific cache item depending on predefined Time. 
  3. Key Dependency:- This allows to invalidate a specific Cache item depending when another Cache item changes.
     

Implementation Requirement:-

To implement Cache Dependencies simply add an OutputCache directive to the page.

<%@ OutputCache Duration="300" varyByParam="None" %>
This directive, as with other page directives should appear at the top of the ASPX page, before any Output it supports five attribute. Two of which are required and others are optional.

  • Duration:- Required. Time, in seconds, the page should be Cached must be a positive integer.
  • Location:- Specified where the OutPut should be Cached.
  • VaryByParam:- Required. The names of the variables in the Request which should result in, separate Cache entries.
  • VaryByHeader:- Varies Cache enteries based on variations in a specified header.
  • VaryByCustom:- Allow custom variations to be specified in the Global.asax.

    How to add Cache Dependencies:-
     
  • Simply open a new web application as shown below

    Caching1.1.gif
     
  • Add the Below Code to Default.aspx

    <%@ Page Language="vb" %>
    <%@ OutputCache Duration="300" varyByParam="None" %>
    <html>
       <head>
          <title>Adding cache dependencies in ASP.NET</title>
          <script runat="server">
             Sub Page_Load()
                Dim myArrayList As New ArrayList
                myArrayList.Add("Key1")
                myArrayList.Add("Key2")
                Response.AddCacheItemDependencies(myArrayList)
                Message.Text = DateTime.Now()
             End Sub
             Sub Button1_Click(sender As Object, e As EventArgs)
                Cache("Key1") = "value for key1"
             End Sub
             Sub Button2_Click(sender As Object, e As EventArgs)
                Cache("Key2") = "value for key2"
             End Sub
          </script>
       </head>
    <body>
       <form id="Form1" runat="server">
          <asp:label id="Message" runat="server"/>
     
          &nbsp;<br />
    &nbsp;<br />
    &nbsp;<asp:button id="Button1" text="Change Key 1" onClick="Button1_Click" runat="server"/>
          &nbsp;&nbsp;&nbsp;
          <br />
    &nbsp;<asp:button id="Button2" text="Change Key 2" onClick="Button2_Click" runat="server"/>
       </form>
    </body>
    </
    html>

    Output:-

    Caching2.gif
    Caching3.gif
    Caching4.gif

Categories

More Articles

© 2020 DotNetHeaven. All rights reserved.