473,322 Members | 1,778 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

ASP, caching data in the Application object

I'm caching data in the Application object to speed up certain pages on a website
The main reason is that the retrieval of this data takes quite a while (a few seconds) and fetching the same data from the "cache" hardly takes any time

A basic pattern used to get the data from disk, or from cache is this

data = getDataFromCache("mydata"
if data = "" the
data = getDataFromDisk(
storeDataInCache(data, "mydata"
end i

Now this works fine as long as there aren't a lot of simultaneous request for this page when the cache has expired
If however the cache has expired and several request are made to the same page, each instance of this page will be taking the task upon him to recreate the cached data, which is unnecessary

I'm wondering if there's a tried and tested mechanisme for plain old ASP that handles this problem that anyone knows of. If there is, I'd be happy to find out :

A few options I can think of myself right now are
1 - create some sort of a locking system. Application.Lock/Unlock could be used for this, but that would mean that every ASP page writing something into the Application object will be put on hold for that time (there are hardly any of those pages though, but still)
2 - change my cache retrieval methods in such a way that they return the expired data anyway, and appoint the first calling page to update the cache, and simply return the old data to the other calling pages as if it hadn't expired yet

feel free to spill your comments :

Jul 19 '05 #1
9 3261
J. Baute wrote:
I'm caching data in the Application object to speed up certain pages
on a website.
The main reason is that the retrieval of this data takes quite a
while (a few seconds) and fetching the same data from the "cache"
hardly takes any time.

A basic pattern used to get the data from disk, or from cache is this:

data = getDataFromCache("mydata")
if data = "" then
data = getDataFromDisk()
storeDataInCache(data, "mydata")
end if

Now this works fine as long as there aren't a lot of simultaneous
request for this page when the cache has expired.
If however the cache has expired and several request are made to the
same page, each instance of this page will be taking the task upon
him to recreate the cached data, which is unnecessary.

I'm wondering if there's a tried and tested mechanisme for plain old
ASP that handles this problem that anyone knows of. If there is, I'd
be happy to find out :)

A few options I can think of myself right now are:
1 - create some sort of a locking system. Application.Lock/Unlock
could be used for this, but that would mean that every ASP page
writing something into the Application object will be put on hold for
that time (there are hardly any of those pages though, but still). 2
- change my cache retrieval methods in such a way that they return
the expired data anyway, and appoint the first calling page to update
the cache, and simply return the old data to the other calling pages
as if it hadn't expired yet.

feel free to spill your comments :)


The basic premise of caching information like that is to use 2 application
variables, one to hold the date cached and the other to hold the data.

*****global.asa file:

Sub Application_OnStart
'used to cache your data
Application("dtData") = Now()-1
Application("cacheData") = ""
End Sub

*****On the page calling the data:

'check to make sure you actually have a cahced date
If Not IsDate(Application("dtData")) Then Application("dtData") = Now()-2

'check if date is more tha X days old or does not exist
If (DateDiff("d", Application("dtData"), Now()) > 1) Or _
Application("dtData") Then

Application.Lock()

'place the data in the application variable
Application("cacheData") = getDataFromDisk()

'******This is important, we have to reinitialize our cached variable
'with the current time, otherwise, the time differenece will always
'be greater than our If/Then here and the db will always
'be called and this is then uselss.
'*******
'the same application variable as found in the global.asa file
Application("cacheData") = Now()

Application.UnLock()

End If
Finally, on the page creating/updating the data, be sure to reset the
Application("cacheData") to Application("cacheData") = Now() - 2 so the page
calling the data will reset it self.

--

kindler chase
http://www.ncubed.com
Home of SuperInvoice: The Online Invoicing Application.
Organize your billing process and impress your clients.

news://news.ncubed.com/support
n3 Support Group
Jul 19 '05 #2
Are you running the server locally with full control or do you push to a
provider?

--
-dlbjr

Discerning resolutions for the alms
Jul 19 '05 #3
dlbjr wrote:
Are you running the server locally with full control or do you push
to a provider?


The technique I described works on either.

--

kindler chase
http://www.ncubed.com
Home of SuperInvoice: The Online Invoicing Application.
Organize your billing process and impress your clients.

news://news.ncubed.com/support
n3 Support Group
Jul 19 '05 #4
J. Baute wrote:
I'm caching data in the Application object to speed up certain pages on a website.
The main reason is that the retrieval of this data takes quite a while (a few seconds) and fetching the same data from the "cache" hardly takes any time.
A basic pattern used to get the data from disk, or from cache is this:
data = getDataFromCache("mydata")
if data = "" then
data = getDataFromDisk()
storeDataInCache(data, "mydata")
end if
Now this works fine as long as there aren't a lot of simultaneous request for this page when the cache has expired.
If however the cache has expired and several request are made to the same page, each instance of this page will be taking the task upon him to recreate the cached data, which is unnecessary.
I'm wondering if there's a tried and tested mechanisme for plain old ASP that handles this problem that anyone knows of. If there is, I'd be happy to find out :)
A few options I can think of myself right now are:
1 - create some sort of a locking system. Application.Lock/Unlock could be used for this, but that would mean that every ASP page writing something into the Application object will be put on hold for that time (there are hardly any of those pages though, but still).
2 - change my cache retrieval methods in such a way that they return the expired data anyway, and appoint the first calling page to update the cache, and simply return the old data to the other calling pages as if it hadn't expired yet.


The current code waits for a request before updating the cached data,
which is slowing your pages.

You can eliminate the wait by using a "double buffer" technique: store
the data alternately in one of _two_ Application variables, e.g.,
Application("mydata1") and Application("mydata2").

Use another Application variable, e.g., Application("whereisit") whose
value is either "mydata1" or "mydata2". Application("whereisit") always
points to the most current data.

To update the cached data, periodically execute an ASP page containing
code similar to the following (the META REFRESH tag is good for this).
This code loads the latest data into the currently unused buffer and
then swaps the value of Application("whereisit"):

IF ( Application("whereisit") == "mydata1" ) THEN
Application( "mydata2" ) = getNewData()
Application.Lock
Application("whereisit") = "mydata2"
Application.UnLock
ELSE
Application( "mydata1" ) = getNewData()
Application.Lock
Application("whereisit") = "mydata1"
Application.UnLock
END IF

Your pages fetch the latest data with:
LatestData = Application( Application("whereisit") )

And in the Application OnStart event:
Application.Lock
Application( "mydata1" ) = getNewData()
Application("whereisit") = "mydata1"
Application.UnLock

This ensures that no page will ever wait since the latest data is
immediately available.

Good Luck,
Michael D. Kersey

Jul 19 '05 #5
I didn't mention this by my getDataFromCache() uses a date internally to determine if the cached data has expired or not
If it has, no data is returned from the function. When this is the case, the code that's getting the data has to request the latest data and store this in the cache again

This works fine except when the cached data has expired and several requests are made at the same time
Let's say 5 requests are made in one second to the page when the cache has expired. If it takes about a second to request the latest data from disk, this will make all 5 calls to the page request the new data, instead of just the page that was called first

Luckily for the page I'm using the cache in I don't think this will be happening frequently, but it would be nice if this situation could be prevented anyway

Jul 19 '05 #6

"Michael D. Kersey" <md******@hal-pc.org> wrote in message
news:uD**************@TK2MSFTNGP10.phx.gbl...
The current code waits for a request before updating the cached data,
which is slowing your pages.

You can eliminate the wait by using a "double buffer" technique: store
the data alternately in one of _two_ Application variables, e.g.,
Application("mydata1") and Application("mydata2").

Use another Application variable, e.g., Application("whereisit") whose
value is either "mydata1" or "mydata2". Application("whereisit") always
points to the most current data.

To update the cached data, periodically execute an ASP page containing
code similar to the following (the META REFRESH tag is good for this).
This code loads the latest data into the currently unused buffer and
then swaps the value of Application("whereisit"):

IF ( Application("whereisit") == "mydata1" ) THEN
Application( "mydata2" ) = getNewData()
Application.Lock
Application("whereisit") = "mydata2"
Application.UnLock
ELSE
Application( "mydata1" ) = getNewData()
Application.Lock
Application("whereisit") = "mydata1"
Application.UnLock
END IF

Your pages fetch the latest data with:
LatestData = Application( Application("whereisit") )

And in the Application OnStart event:
Application.Lock
Application( "mydata1" ) = getNewData()
Application("whereisit") = "mydata1"
Application.UnLock

This ensures that no page will ever wait since the latest data is
immediately available.


Using a date to determine if the cache has expired is more efficient IMO.
With this double caching technique you need to have this seperate
cache-update page running somehow in a iframe or hidden frame or something,
which will probably make the site appear slower to the user.


Jul 19 '05 #7
What is being done with the data your caching? Drop Down List
Are you caching it as xml?
Can you run scripts on the server?

-dlbjr

Discerning resolutions for the alms
Jul 19 '05 #8
fyi, currently I've solved it like this:

the first call that is made when the data has expired gets the task to
update the data,
this is done by setting an extra application variable that indicates the
data is being updated,

every other page that requests the cached data while it's being updated gets
the expired data, and does not bother updating,

when the new data has finally been retrieved by the first page, it's saved
in the cache, and the update flag is removed,

from then on all pages get the new cached data, until it expires again


Jul 19 '05 #9
J. Baute wrote:
<snipped>
Using a date to determine if the cache has expired is more efficient IMO.
What I wanted to convey was that, regardless of what criteria you use to
decide when the cache has expired (date or arrival of new data), a
double-buffering technique would ensure that no user ever waits.
With this double caching technique you need to have this seperate
cache-update page running somehow in a iframe or hidden frame or something,
which will probably make the site appear slower to the user.


Rather than make cache refresh part of your current page(s), the intent
was that the cache-update page would run independently in a separate
"user-agent" (e.g., a browser window on a dedicated client somewhere).
The refresh page would run asynchronously w.r.t. your users' pages. IOW
a refresh.asp page would run every 30 seconds and would look like:
<html>
<head>
<META http-equiv="refresh" content="30,URL=http://www.acme.com/refresh.asp">

<%IF ( Application("whereisit") == "mydata1" ) THEN
Application( "mydata2" ) = getNewData()
Application.Lock
Application("whereisit") = "mydata2"
Application.UnLock
ELSE
Application( "mydata1" ) = getNewData()
Application.Lock
Application("whereisit") = "mydata1"
Application.UnLock
END IF %>
</head>
<body>
<% response.write "last refresh - " & Now %>
</body>
</html>

which submits a request to check for new data every 30 seconds. This
also tests whether your site is up.

Other ways to trigger the above script would be to invoke it with a
timed Perl Script, VB program or service, or WSH script.

But the solution you have arrived at seems a good one given the
circumstances and requirements.

Good Luck,
Michael D. Kersey

Jul 19 '05 #10

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

Similar topics

5
by: dave | last post by:
There seems to be plenty of discussion about caching in the Application Object, however, I cant find any info on what realistic limitations there are and how they are measured What are the...
7
by: Greg Collins [MVP] | last post by:
Hi, I couldn't find what I was looking for by searching the newsgroup, but perhaps these have already been discussed somewhere. This is a bit long with a lot of interrelated questions. What I've...
1
by: Steve Wark | last post by:
If I create two aspx pages, place three text boxes (working with VS .net 2003 and web form controls) and a button on both forms. On the first page, the button is set to use the "onClick" to open...
0
by: Rick Hein | last post by:
I've got a problem with an app I've been working on, the Caching object and events not firing correctly. In a nutshell: When I'm debugging, and I set a breakpoint in the removed item call back, the...
17
by: Fred Nelson | last post by:
Hi: I have written several web applications that obtain their connection strings from the web.config file. This is very easy to use and it makes it easy to move an app from development into...
4
by: Fred Nelson | last post by:
Hi: This is probably a newby question however I can't figure it out! I'm trying to use application data caching in a web application. I have read an article on 4 guys from rolla that shows...
1
by: Nemisis | last post by:
Hi everyone, We are currently re-developing our software, to asp.net 2.0 and taking a 3 tier architecture. I have been thinking about it, and i think that it belongs in the business tier. ...
2
by: George1776 | last post by:
All, I've recently upgraded our production ASP.NET/C# application from framework 1.1 to 2.0. Since then I've been plagued by out-of-memory errors and problems with the cache object (which may...
1
by: Aryan | last post by:
Hi, I have problem related to Caching of data. I am reading large xml file and putting this xml in dataset, since this dataset will contain many datatable's inside. And each datatable might be big...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.