Page 1 of 1

Query or Cache

Posted: Wed Jun 22, 2005 10:49 am
by programmermatt
Ok, suppose that you have a large script. You have serveral functions that access the database and the overall memory usage of the script is getting rather large. Currently those functions cache their data so that if the function is called again in the same page load it will load the data from the cache instead of re-quering the database. When the function is first called, it collects all the data it needs to answer any feasible, valid call to it, generating rather large caches. Is it worth it? Would it be better just to re-query the database instead of building up a huge cache of information probably never to be used?

If it is worth it, should this information be, instead of being lost at the termination of the script, cached even further to the effect that it will last several page loads? If so, what do you think would be a good exipiry timeout?

Posted: Wed Jun 22, 2005 11:47 am
by nielsene
It depends.

How likely are you to have repeated calls to any given one of these functions on a given page? How likely are you to have multiple hits on the same page at once? How often does the cached data change? How "long" does a more normal, directed query take to resolve?

It sounds like the you answered the first question as "low" -- therefore this caching isn't buying you much or anything.

Caching huge amounts of data on first call of a function that is only called once per page seems like a mistake. Perhaps make two versions of the function, one with and one without the cacheing. There might be a few pages (admin type pages spring to mind) where you do often need to slurp up massive quantities of data and re-hit the database along the same query vecter a lot.


If the underlying data is slowly changing and the query is "complicated" in terms of execution time, not just data transfer time, you could beendit from cacheing an hourly, for instance, copy of the result into a cache file and then parsing that -- as long as you have a cheap/simple parser you should beat the database overhead. But that only works if you can live with slightly stale data...