Caching Mechanism in ASP.NET Using VB.NET

In this article we demonstrate the Caching Mechanism and how it helps to achieve important aspect of Quality of Services.
  • 2800
 

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 ObjectByVal e As EventArgsHandlesSubmit1.ServerClick
    itemRemoved = False
    
onRemove = New CacheItemRemovedCallback(AddressOf Me.RemovedCallback)
   If
 (IsNothing(Cache("Key1"))) Then
      
Cache.Add("Key1""Caching"NothingDateTime.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 ObjectByVal e As EventArgsHandlesSubmit2.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 StringByVal v As ObjectByVal r AsCacheItemRemovedReason)
    itemRemoved = True
    
reason = r
End Sub

5.Page Load coding

Protected Sub Page_Load(ByVal sender As ObjectByVal e As System.EventArgsHandles 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

Categories

More Articles

© 2020 DotNetHeaven. All rights reserved.