Cache / OPCache

How Enabling OpCache Makes WordPress Faster

0
| 3 min read |

Introduction

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:

OPCache
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
opcache.enable=1

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.

Notes

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.

Conclusion

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.

Enter your email to get weekly blog updates