You probably know that caching improves performance, but if you run a WordPress site you can take advantage of its own caching class called WP_Object_Cache. This class saves a copy of complex queries and stores results in a database table. Instead of running complex queries frequently on a popular page, using WordPress’ cache object you can store results and drastically reduce load times on your site.
You can find plugins that will persist data across page loads, but be aware that the cache object alone does not store data permanently. It’s only good for one page load, so it should be used for data results that aren’t needed across several user requests. If you need to persist data, WordPress has a Transients API that also has several plugins available for it.
Using the WP_Object_Cache class, you can speed up perceived performance. Some developers report that incorporating object cache cuts down load times by almost half, because each time a query runs it’s stored in a table and delivered much more quickly the next time its data retrieved.
WordPress Codex instructs the developer not to directly instantiate the class from code, but instead use functions they provide through the API.
Important Functions to Cache Data
Most developers use WordPress functions such as wp_options to store data that will later be retrieved. The object cache is a bit different, because it’s mainly for very large data sets and complex queries. If you have a query that takes a second or two to run due to its code or the amount of data, object cache is a helpful way to eliminate the number of times you need to run it. Data isn’t stored in memory like you would normally think of when you think “cache,” but it’s stored in a WordPress table so that the data can be retrieved without running the high-priced query.
To add data to the object cache, you use the wp_cache_add function. You can set an expiration date for the data, but remember that the data stored is short-lived. After the page is reloaded, it’s already removed.
Another important function is wp_cache_set. The difference between this function and the wp_cache_add function is that set will overwrite existing data if it already exists. It’s a way to create or overwrite data as users navigate your pages.
To get the cached data from object cache, use the wp_cache_get function. With the add, get and set functions, you can store and retrieve data as your users navigate through pages that would otherwise take too long to load. You only need to run the query once, and the rest of the processing is a much less complex SQL query.
Plugins That Persist Data Stored with Object Cache
Unless you want to create your own plugin, you can also install one already on the market. Most of them persist data, so you don’t have to deal with keeping track of data that’s stored and deleted. The following are caching plugins that use WordPress’ object cache:
- W3 Total Cache
- Memcached Object Cache
- FileCache: this plugin shifts storage to the hard disk of your server, which is essentially faster but be aware that you need space to store query data.