Everything You Need to Know about WordPress Object Caching

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.

What is Static Page Caching for WordPress?

Poor website speed is a killer for any website. With today's broadband speeds, consumers no longer accept dialup speed load-times. A slow website can be a sign of a poor quality business, so page speed should be top priority when you design and build your web presence. According to surveys done by Akamai and Gomez.com, half of your visitors expect a website to load in two seconds or less. If your site loads in three seconds, you've already lost 79% of your potential sales. Website speed is the foundation for your site's success. Build a strong website on a fast foundation, and the sky's the limit. Have poor speed, and your website can fall apart at any time.

Every second counts in website design, so even the smallest change can have enormous benefits in terms of speed. One change that's a lot less complex and low cost than many other performance enhancements is caching. Several factors go into site speed, but caching can shave several seconds off of a slow website and decrease page load time to less than 100 milliseconds.

Understanding the Way a Web Page Works

Before you can understand caching and its benefits, you should first understand the way a web page renders in a user's browser.

Each time a customer types your business URL into a browser, the browser first does a lookup for the IP address using DNS. After the IP is found, the browser requests the URL's content from the server.

Twenty years ago, it was common for a website to have standard, static HTML pages. These pages did not change regardless of who, when, or where the page was rendered from. There was no database storing information for pages to load. It was just a static page that did not change.

As technology evolved, databases were integrated into web development. Take a standard e-commerce store as an example. The e-commerce pages display products based on a user's queries. Most elements on an ecommerce page are dynamic, including the main content, breadcrumbs, and parts of the navigation menu. Each time a page is requested, the web server makes a call to the database, waits for it to process the query, and retrieves the data. The web server then renders the data on the page dynamically.

As you can probably guess, a dynamic page takes much longer to load because of the processing time between sending a query to the database server, waiting for it to process the request and then waiting for it to return the data. The entire process takes milliseconds, but it leaves a lot open for performance degradation. Too little bandwidth, poor programming, or a slow database can hinder performance.

How Caching Works

Several elements on a web page never change. Images, CSS and JavaScript files, footers and headers rarely change. These elements change when there is a major upgrade but stay the same for months at a time. They don't use data from the database, so no dynamic content is loaded.

Caching speeds up your site by storing these files in RAM (AKA "cache"). Loading content from RAM is much faster than loading from a hard drive. Since static files don't change, the server moves them to RAM, so each time the content is requested it's loaded from the fastest storage component available on the server.

With page caching, you can also store dynamic pages that still load dynamic content but where that content remains the same for a long period of time. For instance, your navigation page could be dynamic but it stays the same until you deploy an upgrade. Caching stores common dynamic pages and their data that are requested frequently. Any page stored in cache (RAM) speeds up the website load-times, but remember that each page uses some of your memory. You should determine which pages are requested frequently enough to cache them, because too much caching can exhaust server resources.

WordPress also has an internal API that allows a developer to control caching objects. Developers can create plugins specifically for WordPress sites and reduce the number of times pages make calls to the database.

Testing Your Site

You might not  know if your site needs to use caching, or that it's loading too slow. There are two main testing tools on the market.

Google PageSpeed Insights is a speed tool that tests your site on both desktop and mobile devices. It even gives you suggestions on what you can do to speed up your site. Compression, caching, and minifying elements are three areas Google will give you advice on should your pages load too slowly

Another major tool is GTmetrix. It's less popular because it's based on Yahoo's speed guidelines. It's still a good way to ensure that you've covered all possibilities for your site's performance.

Remember that you don't need to comply with every suggestion. If your pages load quickly, then having compressed images or browser caching might not be necessary. It's up to you and your developer to decide which caching options are necessary to keep a site within recommended load times.

Sample Benchmarks

If you’re curious about facts and results, WP Dev Shed has an excellent table of speed results from various plugins on the market. The table should help you decide which one is best for your site.

WordPress also has an API that lets developers manipulate cached objects, so you can customize caching using a developer and some innovation. If you want the fastest speeds available for your site and customers, caching is an inexpensive way to give your WordPress site a speed boost.


How Enabling OpCache Makes WordPress Faster


There are a number of optimizations that you can make to your site in order to increase its power to give you the results you want. Some of these have been explored in recent articles or will feature soon on our blog.

Many of these optimizations involve some kind of caching, such as page caching and the use of CDNs. Another type of caching that works at the level of the PHP code that powers all WordPress sites is PHP opcode caching. This particular optimization is very simple to implement and usually provides a significant decrease in overall latency for you site. Depending on the complexity of your site, it is possible that this optimization could double your site speed.

How does it work?

From the official PHP documentation:

“OPcache improves PHP performance by storing precompiled script bytecode in shared memory, thereby removing the need for PHP to load and parse scripts on each request.”

Take a look at this diagram:

The basic principle here is reuse. Usually, a PHP script will be compiled into opcodes at runtime each time the script executes. This requires time and computing power, which can be saved if the results are reused. What OPcache does is save the results of the first compilation into cache memory so that when they are needed again, they are accessible at very high speeds and the compilation step is bypassed.

As well as this caching functionality, OPcache also performs a lot of optimisations within the code itself, such as replacing $i++ with ++$i when the return value isn’t used, for example.

How to Enable PHP OPcache on your server

To enable PHP OPcache, you will need to edit your php.ini file.

As a minimum you will need the following lines:

zend_extension=full/path/to/opcache.so (linux) (Linux/Mac)
zend_extension=full/path/to/php_opcache.dll (Windows)
Determines if Zend OPCache is enabled

NB Don’t forget to restart your web server for changes to take effect.

For a full list of possible configuration settings, check out the documentation here.


The PHP OPcache extension is bundled with PHP version 5.5.0 and later. This means that it if you are following WordPress.org guidelines and using PHP version 7 or greater, it should be available. However, It is important to note that not all hosting providers offer PHP OPcache as part of their service. If this particular optimization is essential to you then you will need to choose a provider that does.

In order to get PHP OPcache enabled and configured on your site, you should talk to your developer and/or hosting provider. The process is fairly straightforward, but there are a couple of “gotchas” to look out for. For example, depending on configuration settings, changes to PHP scripts may or may not be automatically noticed. If they are not, then you will need to remember to flush the opcode cache when changes are made to your PHP scripts.


If you have a large website that with high volumes of traffic, you should definitely use the PHP opcode cache. It is a simple way to gain a dramatic improvement in your site's performance.