如何检查 APC 操作码缓存在 PHP 中是否正常工作?

发布于 2024-10-17 19:43:13 字数 402 浏览 9 评论 0原文

我正在使用启用了 APC 缓存的 PHP:

apc.cache_by_default => On
apc.enabled => On
apc.ttl => 7200

现在我如何知道它是否 100% 使用操作码缓存。

例如,假设我有这个 PHP 文件:

<?php
echo "Hi there";
?>

现在运行该文件后,让我们将其更改为 echo "Bye There";

自从 TTL 以来,它不应该 echo "Hi There"吗? 7200秒还没结束?我说得对吗?如果是这样,为什么它会回响“再见”?如果我错了,即使在更改文件后,如何强制它使用操作码缓存?

I am using PHP with APC cache enabled:

apc.cache_by_default => On
apc.enabled => On
apc.ttl => 7200

Now how can I know if it is using the opcode cache 100%.

For example, let us say that I have this PHP file:

<?php
echo "Hi there";
?>

Now after running this file, let us change it to 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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

丶情人眼里出诗心の 2024-10-24 19:43:13

我能找到的判断 APC 是否正常工作的最简单方法是创建一个包含此代码的新 PHP 文件...

<pre><?php
print_r(apc_cache_info());

它将 apc_cache_info() 的内容转储到屏幕上(小心,在大型实时站点上,这可能是大量数据!)。

每次重新加载此 PHP 文件时,您都应该看到 num_hits 增加,这意味着操作码缓存已被使用。未命中表明 APC 必须从源代码重新编译文件(通常在每次更改时完成)。


要获得此信息的更好界面,您可以使用 APC 附带的 apc.php 文件。我使用此控制台命令将其复制到我的网站目录(您的文件夹位置可能有所不同)...

cp /usr/share/doc/php-apc/apc.php /usr/share/nginx/html/apc-stats.php

在浏览器中运行此文件将为您提供漂亮的颜色和图形!

有关详细信息,请参阅此链接:
http://www.electrictoolbox.com/apc-php-cache-information/< /a>

The simplest way that I could find to tell whether APC is working was to create a new PHP file containing this code...

<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 lots of data!).

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).


For a nicer interface to this information you 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)...

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/

何必那么矫情 2024-10-24 19:43:13

我认为您不想在生产中执行此操作,但您始终可以使用 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;
}

请注意,这将遍历缓存的每个文件以检查指定的文件,因此效率不高。

至于您的具体问题,APC 会在文件更改时自动使文件的缓存失效。因此,当您编辑文件时,APC 会默默地检测到这一点并提供新文件。您可以通过设置 apc.stat = 来禁用此功能0

I don't think you'll want to do it in production, but you could always use 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.

滥情空心 2024-10-24 19:43:13

通常,APC 会检查所请求的文件自缓存以来是否已被修改。您可以使用 apc.stat 来控制它。

Normally APC checks if the requested file has been modified since it's been cached. You can control this with apc.stat.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文