Caching with OPcache

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. OPCache can be optimized to boost performance. 

Configuration

opcache.memory_consumption: Sets the OPcache shared memory storage size for caching all the compiled scripts. Defaults are either 64 MB or 128 MB depending on PHP version.

opcache.max_accelerated_files: The maximum number of keys (scripts) in the OPcache hash table. To find a good value, you can search for the number of PHP files in your project.

$ find project/ -iname *.php|wc -l
9607

opcache.max_wasted_percentage: The maximum percentage of "wasted" memory until a restart is triggered.


With this said, you will need to alter the configuration to match your hosting environment. A good starting point may be

opcache.memory_consumption=128
opcache.max_accelerated_files=10000
opcache.max_wasted_percentage=10

One more configuration option worth mentioning is opcache.validate_timestamps. By default on, this options ensures that OPcache checks the timestamp on the PHP file vs what it has in cache to ensure that the file has not been updated. Disabling this option can offer performance improvements as OPcache will not need to validate the timestamp on the file. This can be set when your website is not being updated often. When a change is made to the PHP file you will need to clear the cache. Because of this gotcha, we will not include it as a standard option.

opcache.validate_timestamps=0

Monitoring

You can visually observe caching statistics with a multitude of different monitoring tools. A very easy to use one is the OPcache Status tool. This is a single PHP file that you can place on your server and display very useful caching information. Monitoring tools such as these help in configuring the proper cache settings.  

Segfaults

There is a currently known issue relating to the opcache.fast_shutdown=1 directive with some versions of PHP. IF you notice segfaults, most likely when the cache is restarted, you may need to disable this directive. You can track the progress on this bug here.