Вопрос

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

Это было полезно?

Решение

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.

Другие советы

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

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

Whiles it's available in PHP5.5 because opcache comes with it, it should also become available if you've installed OpCache into an older version of php. I believe that's what the docs note when it says PHP (PHP 5 >= 5.5.0, PECL ZendOpcache >= 7.0.0).

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

Edit After looking at the above linked control panel, I noticed it checks PHP version and if opcache_reset exists.

It seems to try accelerator_* rather than opcache_* functions.

I suggest trying out that script to see if that works for you, then we can work backwards to see what's installed exactly on your server and what methods to use.

If you have a WordPress site on your server, just install the plugin OPcache Dashboard. It gives you interactive control plus it triggers a cache reset after the automatic Wordpress upgrade process runs.

Another thing to note about a server running multiple instances of the same CMS is what happens if they are running different versions of the CMS or plugins. This would happen e.g. if you stagger upgrades of major releases. In this case your PHP.ini needs to include

opcache.use_cwd=1

so the same filename will be compiled separately depending on the directory it's in. If you are certain your CMS versions are identical across all sites, you can set it to 0 and get efficiency gains because OpCache will compile each routine once and then serve it for all the CMS instances on your server. This is also a memory efficiency gain, and would be quite significant if you have a large number of instances on your WP farm.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top