Re: ASP, caching data in the Application object

Tech-Archive recommends: Fix windows errors by optimizing your registry

From: Michael D. Kersey (mdkersey_at_hal-pc.org)
Date: 03/06/04


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



Relevant Pages

  • Re: ASP, caching data in the Application object
    ... I didn't mention this by my getDataFromCache() uses a date internally to determine if the cached data has expired or not. ... the code that's getting the data has to request the latest data and store this in the cache again. ... 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. ...
    (microsoft.public.inetserver.asp.general)
  • Re: Preserving data within a whole application.
    ... That is Application or Cache, ... Cache supports all sorts of dependencies where cached data can be stalled ... Or you could do it in lazy way when first request for the data comes ... In web service you access cache with Context.Cache or just HttpRuntime.Cache ...
    (microsoft.public.dotnet.framework.aspnet)
  • Re: [PATCH] speed up SATA
    ... The kicker when handling any request larger than the on-disk cache ... insufficient buffer for the entire host request), ...
    (Linux-Kernel)
  • Re: IIS 6.0 Not Caching Images
    ... There is a known bug where if a 204/304 is fulfilled from the kernel ... regardless if it is fulfilled from the kernel response cache or not. ... request is cached in kernel mode (in addition to using WFetch to make the ... server or not -- you need perfmon to determine. ...
    (microsoft.public.inetserver.iis)
  • Re: IIS 6.0 Not Caching Images
    ... which the client would reuse on subsequent requests to the ... IIS is not sending the ETag header on the request correctly. ... > coming out of the kernel mode cache or not. ... There is a known bug where if a 204/304 is fulfilled from the kernel ...
    (microsoft.public.inetserver.iis)