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 |
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!
Declare the variables
|Shared itemRemoved As Boolean = False|
Shared reason As CacheItemRemovedReason
Dim onRemove As CacheItemRemovedCallback
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.
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)
Server Intellect assists companies of all sizes with their hosting needs by offering fully configured server solutions coupled with proactive server management services. Server Intellect specializes in providing complete internet-ready server solutions backed by their expert 24/365 proactive support team.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
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 = TrueEnd Sub
reason = r
Server Intellect assists companies of all sizes with their hosting needs by offering fully configured server solutions coupled with proactive server management services. Server Intellect specializes in providing complete internet-ready server solutions backed by their expert 24/365 proactive support team.
|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("Reason: <B>" + reason.ToString() + "")
Response.Write("Value of cache key: <B>" + Server.HtmlEncode(CType(Cache("Key1"), String)) + "</B>")
Need help with Windows Dedicated Hosting? Try Server Intellect. I'm a happy customer!
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">
<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"/>
Looking for the C#.NET 2005 Version? Click Here!
Looking for more ASP.NET Tutorials? Click Here!