Opcache - Clean cache in PHP5.4 and lower
Asked Answered
U

3

6

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

Unship answered 18/7, 2013 at 6:59 Comment(1)
Also see https://mcmap.net/q/109328/-how-to-use-php-opcacheWaal
W
11

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.

Waal answered 19/7, 2013 at 18:23 Comment(2)
Thanks a lot for your detailed explanation. The solution by creating a script and run it via curl is kinda straight forward but doesn't feel right or at least not as simple as it could be.Unship
You can also reload php-fpm, this will do a graceful restart and should clear the opcache.Derangement
F
0

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.

Flatt answered 18/7, 2013 at 11:50 Comment(7)
Thanks for your answer! The functions exists but doesn't seem to have an effect. Does permissions (which user runs opcache_reset) play a role?Unship
Great question, I'm not sure. Can you try out using that php script linked on your server and see if it works? Lastly, what INI settings did you use? Are you sure they're taking effect? (whats your cache expiration time?)Flatt
Cleaning the cache using the script works. Running as "www-data" php -r "echo function_exists('opcache_reset')?'opcache_':(function_exists('accelerator_reset')?'accelerator_':'');" returns opcache_. I use PHP-FPM with nginxUnship
It seems that the script linked is doing the right thing the. That's likely got the answer in there - it's a little thick to wade through thoFlatt
Perhaps there are "two caches". One for each php implementation (CLI/FPM). The script does nothing else then run opcache_reset() but through the PHP-FPM instance. Manually I always tried to run the command via PHP-CLI. Is there a way to run a php command through PHP-FPM via the terminal? (Needed for deployment)Unship
See the answer to this question: #7315063 to get into the right head-space. I think the easiest solution would be to use the script linked above. To run it command-line, you could use curl to create the proper web-request. In that way you can also secure it down to a virtual host only localhost can access.Flatt
Well I came up with the same idea. But it doesn't feels right to accomplish that via curl a local webscript. Isn't there any better way to do this?Unship
A
0

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.

Anthesis answered 3/5, 2015 at 23:27 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.