Re: ASP, caching data in the Application object
From: Michael D. Kersey (mdkersey_at_hal-pc.org)
Date: 03/06/04
- Next message: Matt: "How to pass data back and forth between ASP and JSP page?"
- Previous message: Miloud Kabsh: "Re: trouble shooting/debugging asp hang"
- In reply to: J. Baute: "ASP, caching data in the Application object"
- Next in thread: J. Baute: "Re: ASP, caching data in the Application object"
- Reply: J. Baute: "Re: ASP, caching data in the Application object"
- Messages sorted by: [ date ] [ thread ]
Date: Sat, 06 Mar 2004 00:15:57 -0600
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
- Next message: Matt: "How to pass data back and forth between ASP and JSP page?"
- Previous message: Miloud Kabsh: "Re: trouble shooting/debugging asp hang"
- In reply to: J. Baute: "ASP, caching data in the Application object"
- Next in thread: J. Baute: "Re: ASP, caching data in the Application object"
- Reply: J. Baute: "Re: ASP, caching data in the Application object"
- Messages sorted by: [ date ] [ thread ]
Relevant Pages
|