Article: Caching Hat-trick: Zend Opcache, Etags and Query Caching

An excerpt from, by Wern Ancheta

In this article, we will be looking at some of the common caching techniques in PHP: Opcache, Expires Headers and Query Caching in MySQL. We’ll look at additional approaches in part 2.

PHP’s Request Lifecycle

Before we move on, it’s important to understand PHP’s request lifecycle. What happens behind the scenes when you access a PHP file from the browser?

  1. A file is fetched from the file system – it doesn’t really matter if it has changed since the last request.
  2. Lexical analysis – the human-readable code is converted into something (tokens) that the parser can understand.
  3. Parsing – the machine-readable strings are analyzed for potential errors. This is like grammar-checking.
  4. Opcode Creation – the tokens that we got from step 2 are converted into machine executable code.
  5. Machine code execution – machine code is processed and then executed.

For a more in-depth explanation, check out this article on how PHP echos a Hello World.

All of those steps happen really quickly, and yet there is a lot of time and resources wasted on every request. That is because for every request for each PHP file on the server, the server has to go through all of them.

The caching techniques that we will be going through in these two articles help bypass steps 2, 3 and 4. That means less time and fewer resources wasted, leading to faster page load times for the user.

Zend Opcache

The first caching tool we’re going to look at is Zend Opcache.

It comes pre-installed with PHP 5.5 and newer. If running php --version from your terminal gives you something like the following, you’re good to go:

Zend Engine v2.5.0, Copyright (c) 1998-2014 Zend Technologies
    with Zend OPcache v7.0.3, Copyright (c) 1999-2014, by Zend Technologies

Alternatively, you can create a new PHP file, call phpinfo(), then access the file from your browser. Look for ‘Zend Opcache’. It should give you something similar to the following:


Once you have confirmed that Zend Opcache is installed, you can enable it by opening your php.ini file, then searching for the term ‘opcache’, uncommenting opcache.enable=0 and setting its value to 1:

; Determines if Zend OPCache is enabled

Don’t forget to restart PHP to activate the changes.

If you are using a PHP version below 5.5, you can still take advantage of Zend Opcache by installing it via PECL. You can do so by executing the following command:

pecl install zendopcache-beta


You can configure Zend Opcache by editing the opcache.ini file under /etc/php5/mods-available/ on Debian-based distributions. By default, the configuration file contains the following:

; configuration for php ZendOpcache module
; priority=05

Here are the most important configuration options you can tweak to your liking:

  • opcache.memory_consumption – the size of the shared memory storage used. This is expressed in megabytes and defaults to 64. You can go higher, depending on how much power your server packs and how much memory you think your application is going to need.

  • opcache.interned_strings_buffer – The amount of memory used to store interned strings, also in megabytes. An interned string is an approach in which only a single copy of each unique string is stored in memory.

  • opcache.max_accelerated_files – The maximum number of files that can be accelerated by opcache. Only numbers between 200 and 100000 are allowed.

  • opcache.revalidate_freq – The number of seconds after which to check PHP files for changes. Giving it a value of 1 means checking for changes once per second, but only once per request. 0 means it will always check for changes. This is the optimal setting for development environments.

  • opcache.max_file_size – Allows exclusion of large files from being cached. By default, it has a value of 0 which means that all files are cached. The value for this option is expressed in bytes.

  • opcache.fast_shutdown – When enabled, the deconstructors get a speed boost at the end of each request. This means that the subsequent request becomes faster. By default, it’s set to 0. Setting it to 1 will enable it.

More configuration options are available here: OPCache Runtime Configuration.

To check if opcache is working properly, you can install opcache-gui by executing the following command from the terminal

composer require amnuts/opcache-gui

Once installed, you can copy the index.php file from the vendor/amnuts/opcache-gui directory to the root of the desired project directory and access it from your browser. It will then show you something similar to the following:

It will show you the memory usage, hit rate, and the configuration options that you have set. You can also reset the cache or invalidate files manually.

Continue reading this article on SitePoint!

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.