phpphp-5.4zend-optimizeropcache

Opcache - Clean cache in PHP5.4 and lower


Is there a way to clean/reset the cached files using Opcache with PHP5.4 or lower?
Here is the opcache_reset() function which just seems to work with PHP5.5

A workaround was to reboot...

Edit: I opened an issue on Github


Solution

  • zend_accelerator_module.c declares the two documented API calls: opcache_reset() and opcache_invalidate() as well as two undocumented ones: opcache_get_status() and opcache_get_configuration(). What they do is pretty obvious from the source.

    When you issue an opcache_reset() it will clearly only apply to the OPcache cache which is connected to the process which is executing your PHP script. And yes, you can have many such caches on the system.

    When you opcache.enable_cli=1 on a php-cli request, then OPcache will issue a restart request for the cache which is connected to that process; unfortunately the cli SAPI creates a private cache so this doesn't do much good.

    The main point to understand on *nix systems is that OPcache relies on some underlying processes manager, such as Apache or FPM to startup OPcache, causing it to mmap() the SMA which contains the cache. The process manager then forks of the child processes which serve requests and also incidentally inherit the mmapped region from the parent.

    So if you want to reset the OPcache cache connected to PHP-FPM then you must do this running through a script running under the PHP-FPM service. This only needs to be a 4-liner. If you want to do this from the command line then you can use wget, curl or a PHP CLI script which uses the curl extension to initiate this FPM script.

    But remember to use some strong authentication mechanism between the two to prevent 3rd-party exploitation.

    If you want to understand a little more, I've done this overview: The Zend Engine and opcode caching. If you've any feedback or Qs, then comment here or raise an issue at Github.