简体   繁体   English

Opcache - 在PHP5.4及更低版本中清理缓存

[英]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? 没有办法使用 PHP5.4或更低版本的Opcache清理/重置缓存文件
Here is the opcache_reset() function which just seems to work with PHP5.5 opcache_reset()函数,它似乎与PHP5.5一起使用

A workaround was to reboot... 解决方法是重新启动...

Edit: I opened an issue on Github 编辑:我在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() . zend_accelerator_module.c声明了两个记录的API调用: opcache_reset()opcache_invalidate()以及两个未记录的API调用: opcache_get_status()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. 当您发出opcache_reset()它显然只适用于连接到正在执行PHP脚本的进程的OPcache缓存。 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; 当你在php-cli请求上opcache.enable_cli=1时,OPcache会为连接到该进程的缓存发出重启请求; unfortunately the cli SAPI creates a private cache so this doesn't do much good. 不幸的是,cli SAPI创建了一个私有缓存,所以这并没有太大的好处。

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. 在* nix系统上理解的要点是OPcache依赖于某些底层进程管理器(如Apache或FPM)来启动OPcache,从而使其mmap()包含缓存的SMA。 The process manager then forks of the child processes which serve requests and also incidentally inherit the mmapped region from the parent. 然后,进程管理器分叉处理请求的子进程,并偶然从父进程继承mmapped区域。

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. 因此,如果要重置连接到PHP-FPM的OPcache缓存,则必须通过在PHP-FPM服务下运行的脚本运行此操作。 This only needs to be a 4-liner. 这只需要一个4线。 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. 如果要从命令行执行此操作,则可以使用wget,curl或使用curl扩展的PHP CLI脚本来启动此FPM脚本。

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 . 如果你想了解更多,我已经完成了这个概述: Zend引擎和操作码缓存 If you've any feedback or Qs, then comment here or raise an issue at Github. 如果您有任何反馈或Q,请在此处发表评论或在Github上提出问题。

See if that method is available with function_exists in your environment. 查看您的环境中的function_exists是否可以使用该方法。

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. 它是PHP5.5中提供的,因为opcache附带它,如果你已经将OpCache安装到旧版本的php中,它也应该可用。 I believe that's what the docs note when it says PHP (PHP 5 >= 5.5.0, PECL ZendOpcache >= 7.0.0) . 我相信这是文档在说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). 我也成功地使用了这个快速而又脏的控制面板和PHP 5.4(使用opcache_ *方法)。

Edit After looking at the above linked control panel, I noticed it checks PHP version and if opcache_reset exists. 编辑查看上面的链接控制面板后,我注意到它会检查PHP版本以及是否存在opcache_reset。

It seems to try accelerator_* rather than opcache_* functions. 它似乎尝试使用accelerator_ *而不是opcache_*函数。

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. 如果您的服务器上有WordPress站点,只需安装插件OPcache Dashboard即可。 It gives you interactive control plus it triggers a cache reset after the automatic Wordpress upgrade process runs. 它为您提供交互式控制,并在自动运行Wordpress升级过程后触发缓存重置。

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. 关于运行同一CMS的多个实例的服务器的另一个注意事项是,如果它们运行不同版本的CMS或插件,会发生什么。 This would happen eg if you stagger upgrades of major releases. 例如,如果您错开升级主要版本,就会发生这种情况。 In this case your PHP.ini needs to include 在这种情况下,您的PHP.ini需要包含

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. 因此,相同的文件名将根据其所在的目录单独编译。如果您确定所有站点的CMS版本相同,则可以将其设置为0并获得效率提升,因为OpCache将编译每个例程一次然后为其提供服务服务器上的所有CMS实例。 This is also a memory efficiency gain, and would be quite significant if you have a large number of instances on your WP farm. 这也是内存效率的提升,如果您的WP场上有大量实例,这将非常重要。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM