ASP, caching data in the Application object

J

J. Baute

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 :
 
K

Kindler Chase

J. Baute said:
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
 
D

dlbjr

Are you running the server locally with full control or do you push to a
provider?
 
K

Kindler Chase

dlbjr said:
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
 
M

Michael D. Kersey

J. Baute said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,780
Messages
2,569,611
Members
45,270
Latest member
TopCryptoTwitterChannels_

Latest Threads

Top