CLI 模式下的 PHP APC

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1245242/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 01:42:31  来源:igfitidea点击:

PHP APC in CLI mode

phpperformanceoptimizationcommand-line-interfaceapc

提问by Matic

Does APC module in PHP when running in CLI mode support code optimization? For example, when I run a file with php -f <file>will the file be optimized with APC before executing or not? Presuming APC is set to load in config file. Also, will the scripts included with require_oncebe also optimized?

PHP 中的 APC 模块在 CLI 模式下运行时是否支持代码优化?例如,当我运行一个文件时,php -f <file>该文件在执行前是否会使用 APC 进行优化?假设 APC 设置为加载到配置文件中。另外,附带的脚本也会require_once被优化吗?

I know optimization works fine when running in fastcgi mode, but I'm wondering if it also works in CLI.

我知道在 fastcgi 模式下运行时优化工作正常,但我想知道它是否也适用于 CLI。

apc_* functions work, but I'm wondering about the code optimization, which is the main thing I'm after here.

apc_* 函数可以工作,但我想知道代码优化,这是我在这里追求的主要内容。

Happy day, Matic

快乐的一天,马蒂奇

回答by Pascal MARTIN

The documentation of apc.enable_cli, which control whether APC should be activated in CLI mode, says (quoting):

apc.enable_cli控制是否应在 CLI 模式下激活 APC的文档说(引用)

Mostly for testing and debugging. Setting this enables APC for the CLI version of PHP. Under normal circumstances, it is not ideal to create, populate and destroy the APC cache on every CLI request, but for various test scenarios it is useful to be able to enable APC for the CLI version of PHP easily.

主要用于测试和调试。设置此项为 PHP 的 CLI 版本启用 APC。在正常情况下,在每个 CLI 请求上创建、填充和销毁 APC 缓存并不理想,但是对于各种测试场景,能够轻松地为 CLI 版本的 PHP 启用 APC 很有用。

Maybe APC will store the opcodes in memory, but as the PHP executable dies at the end of the script, that memory will be lost : it will not persist between executions of the script.

也许 APC 会将操作码存储在内存中,但是随着 PHP 可执行文件在脚本结束时死亡,该内存将丢失:它不会在脚本执行之间持续存在。

So opcode-cache in APC is useless in CLI mode : it will not optimize anything, as PHP will still have to re-compile the source to opcodes each time PHP's executable is launched.

因此,APC 中的操作码缓存在 CLI 模式下是无用的:它不会优化任何内容,因为每次启动 PHP 的可执行文件时,PHP 仍必须将源代码重新编译为操作码。


Actually, APC doesn't "optimize" : the standard way of executing a PHP script is like this :


实际上,APC 不会“优化”:执行 PHP 脚本的标准方式是这样的:

  • read the file, and compile it into opcodes
  • execute the opcodes
  • 读取文件,并将其编译为操作码
  • 执行操作码

What APC does is store in opcodes in memory, so the execution of a PHP script becomes :

APC 所做的是将操作码存储在内存中,因此 PHP 脚本的执行变为:

  • read the opcodes from memory (much faster than compiling the source-code)
  • execute the opcodes
  • 从内存中读取操作码(比编译源代码快得多)
  • 执行操作码

But this means you must have some place in memory to store the opcodes. When running PHP as an Apache module, Apache is responsible for the persistence of that memory segment... When PHP is run from CLI, there is nothing to keep the memory segment there, so it is destroyed at the end of PHP's execution.
(I don't know how it works exactly, but it's something like that, at least in the principles, even if my words are not very "technical" ^^ )

但这意味着您必须在内存中有一些地方来存储操作码。当 PHP 作为 Apache 模块运行时,Apache 负责该内存段的持久性......当 PHP 从 CLI 运行时,没有任何东西可以将内存段保留在那里,因此它在 PHP 执行结束时被销毁。
(我不知道它究竟是如何工作的,但至少在原则上是这样的,即使我的话不是很“技术”^^)


Or, by "optimization" you mean something else than opcode cache, like the configuration directive apc.optimization? If so, this one has been removed in APC 3.0.13


或者,“优化”指的是操作码缓存以外的其他东西,例如配置指令apc.optimization?如果是这样,这个已经在 APC 3.0.13 中删除了

回答by dave1010

If you have CLI code that generates any configuration based on the environment, then the CLI code will think that APC isn't enabled. For example, when generating Symfony's DI container through the CLI, it will tell Doctrine not to use APC (details).

如果您有根据环境生成任何配置的 CLI 代码,那么 CLI 代码会认为 APC 未启用。例如,当通过 CLI 生成 Symfony 的 DI 容器时,它会告诉 Doctrine 不要使用 APC(详细信息)。

Also, I have not tested it but there's a chance APC may improve the speed of scripts for files included after a pcntl_fork(). Edit: I've asked the question about APC & pcntl_fork()here.

此外,我还没有测试过它,但 APC 可能会提高pcntl_fork(). 编辑:我已经问过关于APC & pcntl_fork()here的问题。

For completeness, to enable APC on the CLI (in Ubuntu):

为了完整起见,要在 CLI 上启用 APC(在 Ubuntu 中):

echo 'apc.enable_cli = 1' > /etc/php5/cli/conf.d/enable-apc-cli.ini

回答by Laph

Well, there's a good reason for APC in CLI Mode: UnitTesting: I wanna do my unit test using an environment as close to the later production environment as possible. Zend Framework has an internal caching solution, which may use APC's Variable Cache as Storage Backend - and I wanna use this.

好吧,在 CLI 模式下使用 APC 有一个很好的理由: 单元测试:我想使用尽可能接近后期生产环境的环境来进行单元测试。Zend Framework 有一个内部缓存解决方案,它可以使用 APC 的变量缓存作为存储后端 - 我想使用它。

回答by Natmaka

There is another reason to use it in CLI mode: some scripts are able to use it as a cache

在 CLI 模式下使用它还有另一个原因:一些脚本能够将它用作缓存