[wp-hackers] wp_cache function tutorials?
otto at ottodestruct.com
Mon Jul 26 21:45:30 UTC 2010
On Mon, Jul 26, 2010 at 3:29 PM, Jeff Rose <jeff at jeffrose.ca> wrote:
> Just wondering if anyone has resources or tutorials for the various wp_cache
> functions or information on those versus using transients etc?
> For example, if my blog/site was going to allow people to search a song list
> (custom table of 20,000+ items) - should I cache it and how? That's just an
> example for size though.
> I've read the codex, but it's a little thin on practical use.
How you cache data depends on what exactly you're doing with that data.
If you're caching in order to reduce the number of queries to the
database (like if you make a particular query a lot and want to store
the data in memory instead after retrieval), then you should use the
Object Cache. The main way to use these is to use the wp_cache_get and
wp_cache_set functions. The data stored here may or may not be
persistent, depending on the particular configuration. Usually, it's
not persistent across page hits, but it can be and so it should be
used for storing data returned from the database that you don't expect
to change often. If you do change the data, you need to update the
cache as well. Object Caching like this is basically a performance
enhancement. The data in question is not guaranteed to be there when
you do a get on it.
If you want your data to expire on a timed basis, you should use the
Transients API. See http://codex.wordpress.org/Transients_API .
Advantage to this is that it is persistent across page hits, however
it stores data in the database by default. If a persistent object
caching plugin is installed (like memcached), then that automatically
gets used instead. So this should be used for any caching of data that
expires, in order to get maximum benefit from these. This data is much
more likely to persist across page hits, and so should be used for
storing data retrieved from some external source with a timeout. Like
the results from reading RSS feeds from other sites, for example.
Transients should not be used for storing data that you retrieved from
the database, since it stores them in the database by default anyway.
More information about the wp-hackers