免责声明: APC 在 CLI 中运行时非常棒,而且非常棒,但它同样令人沮丧。以健康的耐心使用,彻底,如果您正在旋转,请远离问题,请记住您正在使用缓存,这就是为什么它似乎什么都不做,实际上什么都不做。删除转储文件,从基础开始,如果不起作用,忘记它尝试新机器,新操作系统,如果它正在工作,请制作副本,逐个扩展功能 - 有很多东西不起作用,如果它正在工作提交或制作副本,请添加另一部分并再次测试,以进行完整性检查,重新检查之前工作的副本,无论是否陈词滥调;如果一开始你没有成功再试一次,你就不能一直做同样的事情来期待新的结果。
准备好了吗?这就是你一直在等待的:
为 cli 启用 apc
apc.enable-cli=1
在每个 CLI 请求上创建、填充和销毁 APC 缓存并不理想
- previous answer by unknown poster since removed.
你说得对,这很糟糕,让我们解决它好吗?
如果您尝试在 CLI 下使用 APC 但未启用,您将收到警告。
类似:
PHP Warning: apc_bin_loadfile(): APC is not enabled,
apc_bin_loadfile not available.
PHP Warning: apc_bin_dumpfile(): APC is not enabled,
apc_bin_dumpfile not available.
警告:我建议你不要在 php.ini 中启用 cli,这不值得沮丧,你会忘记你做了它并且对其他脚本有很多其他的头痛,相信我不值得,改用启动器脚本。 (见下文)
cli中的apc_loadfile和apc_dumpfile
根据mightye php 的评论,我们需要禁用apc.stat,否则您会收到警告
类似:
PHP Warning: apc_bin_dumpfile(): Excluding some files from apc_bin_dump[file].
Cached files must be included using full path with apc.stat=0.
启动器脚本 - php-apc.sh
我们将使用此脚本来启动启用 apc 的脚本(例如 ./php-apc.sh apc-cli.php),而不是直接更改 php.ini 中的属性。
#/bin/sh
php -d apc.enable_cli=1 -d apc.stat=0 $1
准备好使用基本功能了吗?你当然是 =)
基本 APC 持续存在 - apc-cli.php
<?php
/** check if dump file exists, you don't want to use file_exists */
if (false !== $dump_file = stream_resolve_include_path('apc.dump'))
/** so where were we lets have a look see shall we */
if (false !== apc_bin_loadfile($dump_file))
/** fetch what was stored last run just for fun */
if (false !== $value = apc_fetch('my.awesome.apc.store'))
echo "$value from apc\n";
/** store what gets fetched the next run just for fun */
apc_store('my.awesome.apc.store', 'awesome in cli');
/** what a shlep lets not do that all over again shall we */
apc_bin_dumpfile(array(),null,'apc.dump');
注意:为什么不使用 file_exists?因为file_exists == stat你看到了,我们想要获得apc.stat=0的奖励;所以;在包含路径中工作;使用绝对路径而不是相对路径 - 正如 stream_resolve_include_path(); 返回的那样,避免 include_once、require_once 使用非 *_once 对应的路径;在 不使用 APC(Muchos 重要传感器)时检查您的统计使用情况,借助 StreamWrapper 回显来调用方法 url_stat; 糟糕:致命的范围溢出错误!中止通知线程。见url_stat
消息:StreamWrapper 导致的错误超出了本次讨论的范围。
烟雾测试
使用启动器执行基本脚本
./php-apc.sh apc-cli.php
一大堆什么都没发生,这正是我们想要的,为什么还要使用缓存?如果它确实输出了任何东西,那么它就不起作用了,对不起。
应该有一个叫apc.dump的dump文件,看看能不能找到?如果你找不到它,那么它没有工作,对不起。
很好,我们有转储文件,没有错误,让我们再次运行它。
./php-apc.sh apc-cli.php
你想看的:
awesome in cli from apc
成功! =)
PHP 中很少有能像 APC 实现那样令人满意。
开心!