Tom Schuermans

PHP - Clearing the OPcache

Problem with OPCache

I recently made the switch to PHP 5.5, because … features! Thanks to my colleague Mattias, I discovered that my Capistrano deploys were not automatically updated on the server. This was caused by the fact that PHP 5.5+ OPcache ignores Capistrano’s symlinked deploys.

As states:

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.

Cause of the problem

Contrary to APC, OPcache is not inode-based. TL;DR: OPcache resolves symlinks and stores them in a cache, so a new deploy’s symlink resolves to a different path than the one currently stored in OPcache. This causes PHP-FPM to load the old files, because the old resolved symlink is still cached.

Solution to this problem

As Mattias already stated in his post, you can solve this problem by restarting PHP-FPM. Another solution might be to execute a cUrl call to a php script which clears your cache. An example of such script can be found below:

A very detailed explanation on how OPcache works can be found in a post by Julien Pauli