如何检查 APC 操作码缓存在 PHP 中是否正常工作?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5017726/
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
How to check if APC opcode cache is working fine in PHP?
提问by Lina
I am using PHP with APC cache enabled:
我正在使用启用了 APC 缓存的 PHP:
apc.cache_by_default => On
apc.enabled => On
apc.ttl => 7200
Now how can I know if it is using the opcode cache 100%.
现在我怎么知道它是否 100% 使用操作码缓存。
For example, let us say that I have this PHP file:
例如,假设我有这个 PHP 文件:
<?php
echo "Hi there";
?>
Now after running this file, let us change it to echo "Bye there";
现在运行此文件后,让我们将其更改为 echo "Bye there";
Shouldn't it echo "Hi there" since the TTL of 7200 seconds is not over yet? Am I right? If so, why does it echo "Bye there"? And if I am wrong how can I force it to use the opcode cache even after changing the file?
因为 7200 秒的 TTL 还没有结束,它不应该回显“Hi there”吗?我对吗?如果是这样,为什么它会回响“再见”?如果我错了,即使在更改文件后,如何强制它使用操作码缓存?
采纳答案by ircmaxell
I don't think you'll want to do it in production, but you could always use apc_cache_info()
.
我不认为你会想在生产中这样做,但你总是可以使用apc_cache_info()
.
function is_file_cached($file) {
$info = apc_cache_info();
foreach ($info['cache_list'] as $cache) {
if ($cache['filename'] == $file) return true;
}
return false;
}
Note that this will iterate over every single file that's cached checking for the specified one, so it's not efficient.
请注意,这将遍历缓存检查指定文件的每个文件,因此效率不高。
And as far as your specific question, APC will automatically invalidate the cache for a file when it changes. So when you edit the file, APC silently detects this and serves the new file. You can disable this by setting apc.stat = 0
.
就您的具体问题而言,APC 会在文件更改时自动使其缓存无效。因此,当您编辑文件时,APC 会静默检测并提供新文件。您可以通过设置禁用此功能apc.stat = 0
。
回答by Simon East
The simplest way that I could find to tell whether APC is working was to create a new PHP file containing this code...
我能找到的判断 APC 是否正常工作的最简单方法是创建一个包含此代码的新 PHP 文件...
<pre><?php
print_r(apc_cache_info());
It dumps the contents of apc_cache_info() to the screen (be careful, on a large, live site this could be lotsof data!).
它将 apc_cache_info() 的内容转储到屏幕上(小心,在大型实时站点上,这可能是大量数据!)。
Every time you reload this PHP file, you should see num_hits
increase, meaning that the opcode cache was used. A miss indicates that APC had to recompile the file from source (usually done on every change).
每次重新加载此 PHP 文件时,您都应该看到num_hits
增加,这意味着使用了操作码缓存。未命中表示 APC 必须从源代码重新编译文件(通常在每次更改时都进行)。
For a nicer interface to this informationyou can use the apc.php
file that comes with APC. I copied this to my website directory using this console command (your folder locations may differ)...
要获得此信息的更好界面,您可以使用apc.php
APC 附带的文件。我使用此控制台命令将其复制到我的网站目录(您的文件夹位置可能不同)...
cp /usr/share/doc/php-apc/apc.php /usr/share/nginx/html/apc-stats.php
Running this file in your browser gives you nice colours and graphs!
在浏览器中运行这个文件会给你漂亮的颜色和图表!
See this link for further info:
http://www.electrictoolbox.com/apc-php-cache-information/
有关更多信息,请参阅此链接:http:
//www.electrictoolbox.com/apc-php-cache-information/