By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,165 Members | 1,089 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,165 IT Pros & Developers. It's quick & easy.

ASP, caching data in the Application object

P: n/a
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
Share this Question
Share on Google+
9 Replies


P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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

P: n/a

"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

P: n/a
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

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.