[wp-hackers] WP_Object_Cache problem

Otto otto at ottodestruct.com
Fri Dec 11 17:39:11 UTC 2009

On Fri, Dec 11, 2009 at 10:42 AM, Brad Williams <bradw at illiams.com> wrote:
> I'm looking for some help in understanding the WP_Object_Cache class.
> But I'm having problems getting it to work across page loads.

The object cache can be tricky since it got reworked.

Back in pre-2.5, the object cache was persistent, by writing out to
files and such. Testing revealed that this didn't improve speed in any
realistic cases, and actually hurt speed in many commonplace cases.
Therefore, the object cache was reworked.

Now, the object cache is not persistent by default, it resides in
memory only and only for the duration of the session. This is
important, in that it affects how you use it. It should still be used,
but not treated as if it was persistent.

However, the object cache is now highly extensible. This allows
plugins to be created to persist the cache into one of several
options. As was pointed out, there's a file based extension, but
there's also extensions to use memcached setups, among others. These
can allow the object cache to persist across queries into fast memory
systems instead of to slow file systems.

So, if you're using other some fast data caching system, then you can
probably hook the object cache to it in order to make it persistent.
But if you're writing a plugin or something that uses it, then you
must always treat the object cache as if it was not persistent, in
order to maintain data integrity.

If you need a persistent but temporary data store, use the newer
transient system. I have not seen any documentation or tutorials for
it, but it's trivally simple to use. Transients are the same as
options, but they have an expiration date. There's only three
functions you need to know.
function get_transient($transient)
function set_transient($transient, $value, $expiration = 0)
function delete_transient($transient)

Advantage of using these is that they automatically get extended by
these special options cache plugins too, and so they can possibly use
fast memory caching and such. However, by default, they use the
database, like options do.

Sent from Memphis, TN, United States

More information about the wp-hackers mailing list