Cache / Object Caching / Performance

Everything You Need to Know about WordPress Object Caching

| 5 min read |

Everything You Need to Know about WordPress Object Caching

Speed is a contributing factor to your site's success, but it's also one of the most difficult areas to diagnose problems with. If you haven't already tested your site, Google's PageSpeed Insights Tool lets you know if your site could use improvements. Each suggestion will help you speed up your site, but WordPress developers can take advantage of an internal  API called WP Object Cache. This API reduces the number of queries sent to the database server by storing specific option data in a temporary table.

Caching Basics

Every time your visitors open your website, the content is retrieved from the backend database (most likely MySQL). The database processes queries and returns data to the web server. The web server then renders the results.

This process is fine for low-traffic sites, but imagine that you have a million pageviews a day. If each page has five queries during load time, you have five million queries sent to your database every day. The load on your database can be significant enough to degrade performance, which your visitors will experience. A slow website has a tremendous effect on your visitor retention. With just a 4 second delay, you've potentially already lost 25% of your visitors.

If you do a search, you'll find several types of caching. You can cache on the web server using its memory. You can use browser caching to force the user to keep data on their computer. Proxy servers can cache data for enterprise users. WordPress has its own API and type of caching called object caching. It is specific to queries made on stored options coded into themes and plugins.

Caching's main objective is to speed up your site, so any kind of caching is a benefit, but you might not always have the ability to control some kinds of caching. The WordPress Object Cache API is openly available to your developers, so it's a way to speed up your site without changing any server configurations.

More about WordPress Object Cache

While most caching options store content or pages, WP Object Cache uses the options stored in your theme or content. Each plugin or theme that you install on WordPress has its own options that it stores. WordPress has an Options API that stores each value. The most commonly used functions are add_option()  and get_option(). The Options API stores options in the database, so each time an option is retrieved a database query runs. It's not uncommon to have a dozen or more options in just one page of a WordPress site.

The Object Cache API stores specified objects in the database for one page load. One page load might seem like a short lifetime, but it's enough to increase the speed of your site. You cut the number of queries on your database in half (or more), because each time the user loads and reloads a page, only one query is needed for dozens of options.

WordPress has just a few Object Cache functions. The gist of the functions is that you add data to the cache with a corresponding key that is used to retrieve it again. You can optionally group data to use the same key across several options. A expiration time in seconds can be set on the cache. The default is 0, which means that it persists forever or until used but you can set a numerical value to delete cache for certain instances such as when the data might become obsolete.

WordPress also has a Transients API, which works similarly to the Options API, except Transients require an expiration time (in seconds). Transients’ data can also be stored in memory rather than the database. The Transients API should be used carefully, or it could cause technical errors on your site.

Redis with Object Cache

Redis (REmote DIctionary Server) is an open-source database store that uses in-memory stored data that sits as a "go-between messenger" for your MySQL database and the front-end of your WordPress application. It speeds up your site even more by providing an in-memory cache that WordPress can interact with instead of using MySQL resources. It's mainly used for data structures such as lists, maps, sets and sorted sets. If you have complex data storage, Redis is a performance upgrade option.

In terms of WordPress, Redis is popular for caching. It has other features, but caching is its main benefit when working with a WordPress site. You can use plugins to interact with Redis, or design a custom one to work with key features that you want to support on your site. The combination of storing data  in a way that is easy to retrieve, and keeping it in memory make Redis a great addition to performance optimizations  on your site.

Where to Go from Here?

The first step is to identify if your site has any speed issues. Google PageSpeed Insights is helpful, but you can also use GTmetrix as an alternative. You can find suggestions on what you can do to speed up your site, but if these suggestions don't offer enough of a performance boost, it's time to take other measures.

You can use plugins from the WordPress repository that implement Object Cache and Redis, but your options are limited. In some cases, it's better to build a custom plugin specific to your own site.

Whether you go for a custom plugin or a pre-made one from the WordPress repository, caching is an important part of performance optimization that you can leverage for a faster site. Take a look at your site’s speed to determine if it’s time to implement some of the available caching methods.

Enter your email to get weekly blog updates