php Zend OPCache - opcache.enable_cli 1 还是 0?它有什么作用?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25044817/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Zend OPCache - opcache.enable_cli 1 or 0? What does it do?
提问by Hyman
In the documentation it says "mostly used for debugging" which would lead me think "never enable it unless you've a problem and need to do some debugging," however reading mostly everything that I could find about it says to enable it "opcache.enable_cli 1" but why? I could not find any information concerning this matter, so if anybody knows, why should I enable it if the documentation basically says to keep it on 0?
在文档中它说“主要用于调试”这会让我认为“除非你有问题并且需要做一些调试,否则永远不要启用它”,但是阅读我能找到的几乎所有关于它的内容都说要启用它“opcache .enable_cli 1" 但为什么呢?我找不到有关此事的任何信息,所以如果有人知道,如果文档基本上说将其保持在 0,我为什么要启用它?
回答by iquito
With PHP7 and file-based caching, it can now make sense to enable opcache for CLI. The best possibility would be to have a separate php.ini for CLI with the following configuration:
使用 PHP7 和基于文件的缓存,现在可以为 CLI 启用 opcache。最好的可能性是为 CLI 使用单独的 php.ini 并具有以下配置:
opcache.enable=1
opcache.enable_cli=1
opcache.file_cache="/tmp/php-file-cache"
opcache.file_cache_only=1
opcache.file_cache_consistency_checks=1
opcache.file_cache_only=1
makes sure that the in-memory opcache is disabled and only files are used, which is what you want for CLI. This should boost execution time by quite a bit.
opcache.file_cache_only=1
确保禁用内存中的 opcache 并且只使用文件,这正是 CLI 所需要的。这应该会大大提高执行时间。
In the php.ini for FPM, you will want to have the same settings but use opcache.file_cache_only=0
, so in-memory opcache is used and the file cache is used as a fallback (which also makes FPM faster, because the file cache reduces warmup time when FPM is restarted or opcache is reset, because the cached files remain).
在用于 FPM 的 php.ini 中,您将希望具有相同的设置但使用opcache.file_cache_only=0
,因此使用内存中的 opcache 并将文件缓存用作后备(这也使 FPM 更快,因为文件缓存减少了预热时间重新启动 FPM 或重置 opcache,因为缓存的文件仍然存在)。
This way, CLI and FPM share the file cache, and FPM has the in-memory cache as a second primary cache for maximum speed. A great improvement in PHP7! Just make sure to choose a directory for opcache.file_cache
that both CLI and FPM can write to, and that the same user does the writing/reading.
这样,CLI 和 FPM 共享文件缓存,而 FPM 将内存缓存作为第二个主要缓存以实现最高速度。PHP7 的重大改进!只需确保为opcache.file_cache
CLI 和 FPM 都可以写入的目录选择一个目录,并且同一用户进行写入/读取。
UPDATE 2017
2017 年更新
I would not recommend to use the file cache with FPM anymore (only use it for CLI), because there is no way to reset the cache when setting opcache.validate_timestamps=0
- the file cache prevents PHP-FPM from recognizing any changes, because opcache_reset()
or even a complete PHP-FPM restart does not affect the file cache and there is no equivalent for the file cache, so changed scripts are never noticed. I reported this as a "bug"/"feature request" in March 2016, but this is currently not seen as an issue. Just beware if you use opcache.validate_timestamps=0
!
我不建议再将文件缓存与 FPM 一起使用(仅用于 CLI),因为在设置时无法重置缓存opcache.validate_timestamps=0
- 文件缓存会阻止 PHP-FPM 识别任何更改,因为opcache_reset()
甚至是完整的 PHP -FPM restart 不会影响文件缓存并且文件缓存没有等效项,因此永远不会注意到更改的脚本。我在 2016 年 3 月将此报告为“错误”/“功能请求”,但这目前不被视为问题。如果您使用,请注意opcache.validate_timestamps=0
!
回答by duskwuff -inactive-
Leave it off. It's primarily there for use while debugging issues with OPcache itself.
丢开。它主要用于调试 OPcache 本身的问题。
The opcache.enable_cli
option enables PHP OPcache when running PHP scripts from the command line (using the php
command). However, keep in mind that for PHP 5.x the OPcache extension works by storing cached opcodes in the memory of the current process. This is only useful when the process that's running PHP is going to be handling multiple requests that can reuse these opcodes, like in a web server or under FastCGI. For a process like the PHP CLI, which runs one "request" and exits, it just wastes memory and time.
该opcache.enable_cli
选项在从命令行运行 PHP 脚本时启用 PHP OPcache(使用php
命令)。但是,请记住,对于 PHP 5.x,OPcache 扩展通过将缓存的操作码存储在当前进程的内存中来工作。这仅在运行 PHP 的进程将处理多个可以重用这些操作码的请求时有用,例如在 Web 服务器中或在 FastCGI 下。对于像 PHP CLI 这样的进程,它运行一个“请求”并退出,它只会浪费内存和时间。
回答by kenorb
As per PHP docs:
根据PHP 文档:
opcache.enable_cli boolean
enables the opcode cache for the CLI version of PHP. This is mostly useful for testing and debugging.
opcache.enable_cli boolean
为 CLI 版本的 PHP 启用操作码缓存。这主要用于测试和调试。
Therefore it should be disabled unless you're really need this.
因此,除非你真的需要它,否则它应该被禁用。
This can be useful when you've some long-term migration process running from the command-line (personally I've tested OPcache v7.0.3 for CLI by running some extensive migration script and I didn't see much performance improvements).
当您从命令行运行一些长期迁移过程时,这会很有用(我个人已经通过运行一些广泛的迁移脚本测试了 CLI 的 OPcache v7.0.3,但我没有看到太多的性能改进)。