Opcache - Clear Cache in PHP5.4 and Below

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

The workaround was to reboot ...

Edit: I opened issue on Github

+6
source share
3 answers

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

When opcache_reset() it will explicitly apply only to the OPcache cache, which is connected to the process that runs your PHP script. And yes, you can have many such caches in the system.

When you opcache.enable_cli=1 in a php-cli request , then OPcache issues a restart request for the cache that is connected to this process; Unfortunately, cli SAPI creates a private cache, so this is not very good.

The main thing to understand on * nix systems is that OPcache relies on some process manager, such as Apache or FPM, to run OPcache, forcing it with mmap() SMA, which contains the cache. Then, the process manager forks the child processes that serve the requests, and also accidentally inherits the mmapped area from the parent.

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

But be sure to use some kind of strong authentication mechanism between them to prevent third-party developers from working.

If you want to understand a little more, I did this review: Zend Engine and operation code caching . If you have feedback or questions, comment here or raise a question on Github.

+11
source

See if this method is available with function_exists in your environment.

 if( function_exists('opcache_reset') ) echo 'yay!'; 

While it is available in PHP5.5, since opcache comes with it, it should also be available if you installed OpCache in an older version of php. I believe that what documents note when writing PHP (PHP 5 >= 5.5.0, PECL ZendOpcache >= 7.0.0) .

I also used this quick and dirty control panel with PHP 5.4 successfully (uses opcache_ * methods).

Edit Having looked at the control panel linked above, I noticed that it checks the version of PHP, and if opcache_reset exists.

It seems try accelerator_ * instead of opcache_* functions.

I suggest that you check the script to make sure that this works for you, then we can work in reverse order to find out what exactly is installed on your server and what methods to use.

0
source

If you have a WordPress site on your server, just install the OPcache Dashboard plugin. It gives you interactive control and also starts the reset cache after starting the automatic Wordpress update process.

Another point about a server running multiple instances of the same CMS is what happens if they use different versions of the CMS or plugins. This will happen, for example. if you sway updates of major releases. In this case, your PHP.ini should include

 opcache.use_cwd=1 

therefore, the same file name will be compiled separately depending on the directory in which it is located. If you are sure that your versions of CMS are identical on all sites, you can set it to 0 and get an increase in efficiency, because OpCache will compile each procedure once, and then it will be used for all CMS instances on your server. It also improves memory efficiency and will be very significant if you have a large number of instances on your WP farm.

0
source

Source: https://habr.com/ru/post/949711/


All Articles