PHP中的APC模块在CLI模式下运行时是否支持代码优化? 例如,当我使用 php -f
运行文件时,文件在执行之前是否会使用 APC 进行优化? 假设 APC 设置为在配置文件中加载。 另外,require_once
中包含的脚本也会被优化吗?
我知道优化在 fastcgi 模式下运行时效果很好,但我想知道它是否也适用于 CLI。
apc_* 函数可以工作,但我想知道代码优化,这是我在这里追求的主要内容。
快乐的一天,
马蒂奇
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_once
be also optimized?
I know optimization works fine when running in fastcgi mode, but I'm wondering if it also works in CLI.
apc_* functions work, but I'm wondering about the code optimization, which is the main thing I'm after here.
Happy day,
Matic
发布评论
评论(4)
apc.enable_cli
的文档,它控制是否应在 CLI 模式下激活 APC,(引用) 表示:也许 APC 会将操作码存储在内存中,但随着 PHP 可执行文件在脚本结束时终止,该内存将丢失:它不会在脚本执行之间持续存在。
因此,APC 中的操作码缓存在 CLI 模式下毫无用处:它不会优化任何内容,因为每次启动 PHP 的可执行文件时,PHP 仍然必须将源代码重新编译为操作码。
实际上,APC 并没有“优化”:执行 PHP 脚本的标准方法是这样的:
APC 所做的是将操作码存储在内存中,因此 PHP 的执行脚本变成:
但这意味着您必须在内存中有某个位置来存储操作码。 当 PHP 作为 Apache 模块运行时,Apache 负责该内存段的持久性...当 PHP 从 CLI 运行时,没有任何东西可以保留该内存段,因此它在 PHP 执行结束时被销毁。
(我不知道它到底是如何工作的,但至少在原理上是这样的,即使我的话不是很“技术”^^)
或者,“优化”指的是操作码缓存之外的其他东西,例如配置指令 apc.优化 ? 如果是的话,这个已经在APC 3.0.13中被删除了
The documentation of
apc.enable_cli
, which control whether APC should be activated in CLI mode, says (quoting) :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.
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.
Actually, APC doesn't "optimize" : the standard way of executing a PHP script is like this :
What APC does is store in opcodes in memory, so the execution of a PHP script becomes :
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" ^^ )
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
如果您的 CLI 代码可以根据环境生成任何配置,则 CLI 代码将认为 APC 未启用。 例如,当通过 CLI 生成 Symfony 的 DI 容器时,它会告诉 Doctrine 不要使用 APC (详细信息)。
另外,我还没有测试过它,但 APC 有可能提高
pcntl_fork()
之后包含的文件的脚本速度。 编辑:我问过关于 APC &pcntl_fork()
此处。为了完整起见,要在 CLI 上启用 APC(在 Ubuntu 中):
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).
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.For completeness, to enable APC on the CLI (in Ubuntu):
在 CLI 模式下使用它还有另一个原因:某些脚本能够 将其用作缓存
There is another reason to use it in CLI mode: some scripts are able to use it as a cache
那么,APC 在 CLI 模式下有一个很好的理由:
单元测试:我想使用尽可能接近后期生产环境的环境进行单元测试。 Zend Framework 有一个内部缓存解决方案,它可以使用 APC 的变量缓存作为存储后端 - 我想使用它。
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.