Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 228
schickling Avatar asked Jul 18 '13 06:07

schickling


People also ask

How do I clear my OPcache cache?

Whenever you want to flush your Opcache, just browse to the flush_cache. php file. The file will call the ocache_reset() for the entire Opcache. The site will be populated with the cache again with the new PHP request.

What is op cache?

OPcache is a caching engine built into PHP. When enabled, it dramatically increases the performance of websites that utilize PHP. From php.net: 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.


1 Answers

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.

like image 64
TerryE Avatar answered Sep 21 '22 21:09

TerryE