
Navigator : Home > Tutorials > Network Tutorials > ...
Web caching in ASP.NET 2.0 (VB.NET)
Web caching technology in ASP.NET and VB.NET is helpful for popular website reducing its server workload and improving access times. This tutorial will show you how to use web caching save data to RAM, and improve data access times therefore.
First, import the namespace of System.Web.Caching
| import System.Web.Caching |
Declare the variables
If you're ever in the market for some great Windows web hosting, try Server Intellect. We have been very pleased with their services and most importantly, technical support.
Shared itemRemoved As Boolean = False Shared reason As CacheItemRemovedReason Dim onRemove As CacheItemRemovedCallback |
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 |
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 |
We chose Server Intellect for its dedicated servers, for our web hosting. They have managed to handle virtually everything for us, from start to finish. And their customer service is stellar.
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 |
Page_Load
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 |
We are using Server Intellect and have found that by far, they are the most friendly, responsive, and knowledgeable support team we've ever dealt with!
The 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> |
We used over 10 web hosting companies before we found Server Intellect. Their dedicated servers and add-ons were setup swiftly, in less than 24 hours. We were able to confirm our order over the phone. They respond to our inquiries within an hour. Server Intellect's customer support and assistance are the best we've ever experienced.
Looking for the C#.NET 2005 Version? Click Here!
Looking for more ASP.NET Tutorials? Click Here!