Caching Query Results with QueryArrayCached
If your application has a component that needs to issue the same query
multiple times, you should know about
QueryArrayCached. This method is
identical to
QueryArray, except that the results of your query will be
stored in the QCache object, and if there's a match, the query won't be
sent to the database server - cached results will be used instead.
Why is this cool? Because this can give you amazing performance improvements.
What if your page has a list of product categories that rarely changes?
And that list appears on a highly-trafficked page on your site? Just change
your query calls to use
QueryArrayCached method, and the rest will
be taken care of for you.
In the example below, pay attention to the Number of Queries performed at the very
bottom - the first time you load this page, two queries; subsequent page reloads
will not cause any queries at all. That is, of course, until you clear the cache -
you can do so by passing in a $blnForceUpdate parameter set to true to
QueryArrayCached, or by using the
QCache::ClearNamespace()
method - see more in the sample code of this example.
You should also be aware of persistent controls (
example)
- a higher-level abstraction that allows you to cache entire QControls.