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.
To delete cache data for a key, here is the opposite.
Fetching data for a key is done by using:
Should within the cache to a key the content to be replaced, then the following function will work.
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.
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.