473,508 Members | 2,214 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Timed refresh of Cache data trick

I have a page that displays real-time data (road sensors updated every
5 minutes), but the query to display the data takes 15-20 seconds (big
table in a DB w/o indexes, not my fault). This is unacceptable, so I
decided to use the Cache with a 5 minute expiration. I retrieved the
data on AppStart in the global.asax but that resulted in a slow first
page hit. I added a callback method for the CacheItemRemovedCallback
event, but sometimes the browser would make a request during the 15-20
seconds it takes to reload the cache and get a null object.

I ended up using an Application scope variable to hold my data (a
DataTable) and the Cache engine to fire the callback that refreshes
the Application variable. It now seems to work perfectly: the
DataTable is always available and the slow database call happens
behind the scenes and does not affect requests. See code below:

public class Global : System.Web.HttpApplication
{
protected static DateTime LastDataCheck;
private static CacheItemRemovedCallback onRemove = null;
private static EventLog myEventLog = null;
private static Cache appCache = null;
private static HttpApplicationState appVar = null;

/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;

public void RemovedCallback(string key,
object value, CacheItemRemovedReason r)
{
ReloadData();

}

protected void Application_Start(Object sender, EventArgs e)
{
onRemove = new CacheItemRemovedCallback(this.RemovedCallback);

// Grab the Cache and App references while the Context is valid
appCache = Context.Cache;
appVar = Context.Application;

ReloadData();
}

private void ReloadData()
{
try
{
object myDataTable = DataManager.GetRecentData();

LastDataCheck = DateTime.Now;

// Just put the string "abc" to act as a placeholder in the Cache
appCache.Insert("CachePlaceholder", "abc", null,
LastDataCheck.AddMinutes(4),
TimeSpan.Zero, CacheItemPriority.Low, onRemove);

// Store myDataTable DataTable in the HttpApplicationState variable
appVar["myDataTable"] = myDataTable;
appVar["LastDataCheck"] = LastDataCheck;

myEventLog.WriteEntry("Cache Updated: " +
DateTime.Now.ToString(), EventLogEntryType.Information);
}
catch(Exception e)
{
myEventLog.WriteEntry("Error occured retrieving data: " +
e.Message, EventLogEntryType.Error);

}
}
}
Nov 18 '05 #1
0 1277

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
1558
by: Matt | last post by:
(can't find any of my previous post for same issue. please CC to my email address as well. Thanks!) Hi all, What is the possible reason that HttpWebRequest.GetRequestStream() may hang there...
2
2657
by: Rainer Sinsch | last post by:
Hello everyone, does anyone know, how I can refresh a webform image by pressing an asp.net-button? I have a statistics-jpg which gets updated every 5 minutes and want to provide a manual...
4
4361
by: Erick | last post by:
i have an asp.net application and I want to save the results of an sql query in cache. Because the queries take 28 seconds to run (there are twelve similar queries) I want to run it all at 4am, ...
6
1969
by: Charts | last post by:
I used HttpContext.Current.Cache To cache data from database. The code is like that. public static DataView GetCategories() { if ( HttpContext.Current.Cache == null ) {...
3
3154
by: david | last post by:
I do not have the experience with it. For example, update the data in the ASP form every minute by retrieving data from database. Or do you have other methods? Thank you for any advice and...
4
2534
by: Paul Drummond | last post by:
Is there a way to force a page refresh, so users will not receive a cached version of a page, even if they have their browser set to never check for a newer version of page? Thank you!
4
9519
by: morc | last post by:
hi, is there any way to refresh the page when the user clicks back button?I have a jsp page that i need to regerenate everything everytime the user visits it. its either that or i want to...
11
5649
by: Jerry J | last post by:
How can I get an asp:Image to refresh when a user uploads a different jpg. I disabled caching using this command on Page_Load(): Response.Cache.SetCacheability(HttpCacheability.No Cache); but it...
5
495
by: SimonZ | last post by:
When user insert some data on page and click save button this data is inserted to database. If he clicks refresh button, the data is saved again each time when he clicks this button. I would...
0
7132
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7336
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7401
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7063
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7504
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
5059
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3211
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3196
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
432
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.