Use WordPress Cache

WordPress has an internal cache, also for extensions can be used. There are various functions available and you don't have to create something new, you can easily use the cache functionality of WordPress.
To get to know and understand the features a little bit, I use a small example, therefore I cache in the following tutorial a feed, which should be displayed in the frontend.

All functions of cache are in the Codex by WordPress listed, so a look at the Codex is worthwhile if you deal with the syntax.

The first cache solution came with WordPress 2.3 and was file based. The cache was optional and had some parameters to configure.
You were able to activate via following constant: define ( 'ENABLE_CACHE', true);

The biggest improvement happened in version 2.6, in which the cache has changed to an object-oriented solution. Therefore the opportunities for cache usage are lying rather on the server and not explicitly on WordPress. This was mainly realized in order to maximize the resources of the server and not to be handed over to WordPress. With this introduction, the cache of WordPress has no longer explicitly be activated, it is always active. Therefore, it is important that the server has a certain minimum amount of RAM available, WordPress requires 32 MByte - but that is not always the case, for example, when updating the core, it contains a call which defines the RAM to 128MByte, which in many cases is not available and therefore the update does not work.
But this is not the topic of this post today, because I want to explain how to use the cache in your own extensions. So back to the syntax and I just start with the key functions to realize a small example.

All functions can be found in wp-includes/cache.php, or alternatively in Codex.

To reset the cache, insofar there is no data for this key, you can use the following function.

/**
 * @param int|string $key The cache ID to use for retrieval later
 * @param mixed $data The data to add to the cache store
 * @param string $flag The group to add the cache to
 * @param int $expire When the cache data should be expired
 */
wp_cache_add($key, $data, $flag = '', $expire = 0)

To delete cache data for a key, here is the opposite.

/**
 * @param int|string $id What the contents in the cache are called
 * @param string $flag Where the cache contents are grouped
 * @return bool True on successful removal, false on failure
 */
wp_cache_delete($id, $flag = '')

Fetching data for a key is done by using:

/**
 * @param int|string $id What the contents in the cache are called
 * @param string $flag Where the cache contents are grouped
 * @return bool|mixed False on failure to retrieve contents or the cache
 */
wp_cache_get($id, $flag = '')

Should within the cache to a key the content to be replaced, then the following function will work.

/**
 * @param int|string $id What to call the contents in the cache
 * @param mixed $data The contents to store in the cache
 * @param string $flag Where to group the cache contents
 * @param int $expire When to expire the cache contents
 * @return bool False if cache ID and group already exists, true on success
 */
wp_cache_replace($key, $data, $flag = '', $expire = 0)

But now a small example, which caches the feed. The feed gets loaded by fetch_rss(), a function of WordPress which is available since version 1.5.

$mycache = wp_cache_get( 'mycache' ); // fetch data from cache to the key "mycache"
if ($mycache == false) { // if no data, then
	$mycache = fetch_rss("http://mycache.com/feed/"); // parse feed
	wp_cache_set( 'mycache', $mycache ); // save feed content to key "mycache"
}
var_dump( $mycache ); // display content

FYI: You get an insight into the cache of WordPress easily via the variable $wp_object_cache or using the Plugin Debug Objects or WP Cache Inspect; whereas Debug Objects explicitly has been made for this and should be used in development environments only.

Comments are closed.

6 comments

  1. John (Human3rror)

    sweet! thanks for this.

  2. Rarst

    One thing I don't see in post and found confusing - this cache is not persistent and only stays in memory until page is generated.

    So it only make sense if some data has to be used few times in a single page load. To cache between page loads - options and transients should be used.

    PS fetch_rss must die, long live fetch_feed 🙂

  3. pete

    @Rast
    What do you mean with 'transients'?

  4. DD32

    > this cache is not persistent and only stays in memory until page is generated.

    It is Persistent if someone loads an External Object cache.

    >>To cache between page loads - options and transients should be used.
    >What do you mean with 'transients'?

    Transients should be used.
    Transients are like options, Which are only stored in the database if(and only if) an External Object cache is not loaded.

    For example, If i use a generic WP install, with nothing else added, Transients will be saved as an option. If i use a WordPress install with the Memcache object cache enabled, Transients will -only- be stored in Memcached.

    > The biggest improvement happened in version 2.6, in which the cache has changed to an object-oriented solution.

    Not sure what you mean by that.. The API and underlying structure hasnt changed since nearly 2.0 IIRC. Its just that recently, The file-cache was striped out of WordPress so people wouldnt enable it on some hosts.. Unless its running on a decenthost with plenty of IO, it would actually slow things down. (Many shared hosts with Networked filesystems rings a bell)

  5. Frank

    @DD32: i think, WordPress use not a filebased cache with the version 2.6. Only a cache with possibilities of the server was used higher as 2.6. The functions in xore have the same name. I hope, i understand your comment right.

One pingback

  1. Wordpress Resources #1 | wpcrunchy