Introduction to caching - Opcode caches

Last revision:

What are opcode caches

Drupal's is written in the PHP programming language.

PHP code is compiled at runtime. In other words, every time time PHP code needs to be executed, it fist needs to be changed from human-readable source code into byte code that can be understood and executed by machines.

Compiling the same code every time a user interacts with your Drupal site wastes valuable time and resources.

Opcode caches store compiled PHP scripts in-memory so the compilation step can be skipped on future requests.

Because the accelerated PHP execution, PHP opcode caches are also called PHP accelerators.

Zend OpCache

Zend Opcache is an opensource opcode cache, and is usually bundled with PHP itself.

Though there are a good number of configuration options, you typically only need to set a handful of them, such as how much memory to use and how long to keep bits compiled code in its cache.

Advantages

Opcache is often bundled with PHP, so there's nothing to do to install it.

OpCache is usually set-and-forget: once it's configured it doesn't need much attention.

Depending on the size and complexity, using OpCache can significantly improve PHP execution time.

Disadvantages

Every time you install updates to Drupal core or contributed modules, or change your own module or theme code, you need to remember to clear the opcode cache.

Depending on your server, you may need to:

  • restart your web server
  • restart your PHP engine
  • use a tool like Cachetool

Summary

  • Every time PHP code needs to be executed, it is compiled at run-time.
  • Repeatedly compiling the same code wastes time and resources.
  • Opcode caches store compiled PHP code for reuse later.
  • Zend OpCache often comes bundled with PHP and requires little configuration.
  • Opcaches need to be cleared after every update of core, contributed, or custom module or theme code.