ARTICLE

Caching Mechanism in ASP.NET Using VB.NET

Posted by Dea Saddler Articles | ASP.NET using VB.NET March 16, 2011
In this article we demonstrate the Caching Mechanism and how it helps to achieve important aspect of Quality of Services.
Download Files:
 
Reader Level:


Caching is a mechanism for keeping content in memory for later use. It helps web applications operate with higher performance by reducing the amount of work. Different people use different terms for explaining the concept  of cache i.e. managing data, state management and, cache management. Although cache management is not an issue in Windows applications, it has always been a challenge in the web environment.
Caching helps us to achieve three important aspects of QoS (Quality Of Service):

  • Performance - Caching improves application performance by minimizing data retrieval and formatting operations.
  • Scalability - Since caching minimizes data retrieval and formatting operations, it reduces the load on server resources thus increasing the scalability of the application.
  • Availability - Since the application uses data from a cache, the application will survive failures in other systems and databases.

How to use web caching save data to RAM, and improve data access times therefore.
1.Declare the variables

Shared itemRemoved As Boolean = False
  Shared
reason As CacheItemRemovedReason
 
  Dim onRemove As
CacheItemRemovedCallback

2.Define the method of AddItemToCache, it will use Cache.Add to add items to cache

Public Sub AddItemToCache(ByVal sender As Object, ByVal e As EventArgs) Handles Submit1.ServerClick
    itemRemoved = False
   
onRemove = New CacheItemRemovedCallback(AddressOf Me.RemovedCallback)
   If
(IsNothing(Cache("Key1"))) Then
     
Cache.Add("Key1", "Caching", Nothing, DateTime.Now.AddSeconds(30),
      TimeSpan
.Zero, CacheItemPriority.High, onRemove)
   End
If
End
Sub

3.Define the method of RemoveItemFromCache, it will use Cache. Remove to remove items from cache

Public Sub RemoveItemFromCache(ByVal sender As Object, ByVal e As EventArgs) Handles Submit2.ServerClick
       If
(Not IsNothing(Cache("Key1"))) Then
          
Cache.Remove("Key1")
       End
If
  End Sub

4.When using the method of Cache.Remove , it will be leaded to invoke RemovedCallback method

Public Sub RemovedCallback(ByVal k As String, ByVal v As Object, ByVal r As CacheItemRemovedReason)
    itemRemoved = True
   
reason = r
End Sub

5.Page Load coding

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    If (itemRemoved) Then
      
Response.Write("Removed event raised.")
       Response.Write("<BR>")
       Response.Write("Reason: <B>" + reason.ToString() + "")
   Else
      Response.Write("Value of cache key: <B>" + Server.HtmlEncode(CType(Cache("Key1"), String)) + "</B>")
   End
If
End Sub

6.Coding of HTML of the Web Page

<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<
body>
<
Form id="Form1" runat="server">
<input id="Submit1" type="submit" OnServerClick="AddItemToCache" value="Add Item To Cache"
runat="server"/>
<
input id="Submit2" type="submit" OnServerClick="RemoveItemFromCache" value="Remove Item From Cache" runat="server"/>
</Form>
</
body>
</
html>

Output:

Caching-macanism.gif


Caching-Machanism2.gif

Login to add your contents and source code to this article
share this article :
post comment
 
6 Months Free & No Setup Fees ASP.NET Hosting!
Become a Sponsor
PREMIUM SPONSORS
  • The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
    Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites - Click Here!
Become a Sponsor