DotNet Tutorials

V4 Dot Net Tutorials

Server Intellect Cloud Hosting

 Web caching in ASP.NET 2.0 (C#)

Web caching technology in ASP.NET and C# 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

using System.Web.Caching

Declare the variables

Server Intellect offers Windows Hosting Dedicated Servers at affordable prices. I'm very pleased!

static bool itemRemoved = false;
static CacheItemRemovedReason reason;
CacheItemRemovedCallback onRemove = null;

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

public void AddItemToCache(Object sender, EventArgs e)
{
itemRemoved = false;

onRemove = new CacheItemRemovedCallback(this.RemovedCallback);

if (Cache["Key1"] == null)
Cache.Add("Key1", "Caching", null, DateTime.Now.AddSeconds(60), TimeSpan.Zero, CacheItemPriority.High, onRemove);
}

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

public void RemoveItemFromCache(Object sender, EventArgs e)
{
if (Cache["Key1"] != null)
Cache.Remove("Key1");
}

Try Server Intellect for Windows Server Hosting. Quality and Quantity!


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

public void RemovedCallback(String k, Object v, CacheItemRemovedReason r)
{
itemRemoved = true;
reason = r;
}

Page_Load

protected void Page_Load(object sender, EventArgs e)
{
if (itemRemoved)
{
Response.Write("RemovedCallback event raised.");
Response.Write("<BR>");
Response.Write("Reason: <B>" + reason.ToString() + "</B>");
}
else
{
Response.Write("Value of cache key: <B>" + Server.HtmlEncode(Cache["Key1"] as string) + "</B>");
}
}

I just signed up at Server Intellect and couldn't be more pleased with my Windows Server! Check it out and see for yourself.

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 VB.NET 2005 Version? Click Here!

Looking for more ASP.NET Tutorials? Click Here!

Download Project Source - Enter your Email to be emailed a link to download the Full Source Project used in this Tutorial!



100% SPAM FREE! We will never sell or rent your email address!