如何使 APC(PHP 缓存)工作?

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

How to make APC (PHP Cache) work?

phpcachingcentosapc

提问by Tomasz Smykowski

I've read about APC that it speeds up multiple php file sites. So I have this particular project in PHP with many files and i discover that require_once and parsing only class definitions (without executing them) takes most time.

我读过有关 APC 的文章,它可以加速多个 php 文件站点。所以我在 PHP 中有这个包含许多文件的特定项目,我发现 require_once 和只解析类定义(不执行它们)需要大部分时间。

So I've installed APC on my CentOS 5 server. I moved apc.php to my webserver and it displays

所以我在我的 CentOS 5 服务器上安装了 APC。我将 apc.php 移动到我的网络服务器并显示

Hits: 1 (50.0%)
Misses: 1 (50.0%)

Cached Files    1 (281.1 KBytes)

And I can go to website and change subpages and so on, and apc.php still shows only one cached file ??

我可以去网站并更改子页面等等,而 apc.php 仍然只显示一个缓存文件??

And in phpinfo() it shows that:

在 phpinfo() 中它显示:

APC Support enabled

已启用 APC 支持

I don't know what to do. Is APC working (like phpinfo() sais) or is it not? Only one cached file is not much after browsing some pages on my website.

我不知道该怎么办。APC 是否正常工作(如 phpinfo() sais)?在我的网站上浏览了一些页面后,只有一个缓存文件并不多。

And further more how to diagnose errors and make sure that APC works? I've browsed

以及更多如何诊断错误并确保 APC 正常工作?我浏览过

apc.mmap_file_mask /tmp/apc.QnLqNf /tmp/apc.QnLqNf

apc.mmap_file_mask /tmp/apc.QnLqNf /tmp/apc.QnLqNf

Directory /tmp but I don't have any files of apc there like it's stated in phpinfo should occur.

目录 /tmp 但我没有任何 apc 文件,就像 phpinfo 中所说的那样应该发生。

Please help me check if APC is working and if not, what can be a problem.

请帮我检查 APC 是否正常工作,如果没有,可能是什么问题。

采纳答案by cletus

APCcan be used in two ways:

APC可以通过两种方式使用:

  1. As an opcode cache. You have support enabled so this is working; and
  2. As a general cache. You have to explicitly use the APIfor this.
  1. 作为操作码缓存。您已启用支持,因此这是有效的;和
  2. 作为一般缓存。为此,您必须明确使用API

(1) is the main benefit. It reduces time for script execution by storing the opcode results of "compiling" scripts.

(1) 主要利益。它通过存储“编译”脚本的操作码结果来减少脚本执行的时间。

As for it working, from the installation instructions:

至于它的工作,从安装说明

Note:On Windows, APC needs a temp path to exist, and be writable by the web server. It checks TMP, TEMP, USERPROFILE environment variables in that order and finally tries the WINDOWS directory if none of those are set.

注意:在 Windows 上,APC 需要一个临时路径才能存在,并且可由 Web 服务器写入。它按顺序检查 TMP、TEMP、USERPROFILE 环境变量,如果没有设置这些变量,则最后尝试 WINDOWS 目录。

So you shouldn't need it (or see any activity) on CentOS.

所以你不应该在 CentOS 上需要它(或查看任何活动)。

That being said, require/include are better than require_once/include_once/autoload but it should only matter if you are doing that with hundreds of files.

话虽如此,require/include 比 require_once/include_once/autoload 更好,但只有在处理数百个文件时才重要。

回答by John Cavan

Is your site basically one PHP file that then includes or requires other files? That may register as just a single file. Also, as far as I know, conditional include/require logic may not cache as expected though that may just be hearsay!

您的站点基本上是一个包含或需要其他文件的 PHP 文件吗?这可能仅注册为单个文件。此外,据我所知,条件包含/要求逻辑可能不会按预期缓存,尽管这可能只是道听途说!

回答by Quamis

Did you check thr log files? take a look at /var/log/apache2/error.log or however its named under your distribution.

您是否检查了 thr 日志文件?看看 /var/log/apache2/error.log 或者它在您的发行版下命名。

Also, try to make 2-3 php files, point thebrowser at them, and then check how many php files are cached by the APC cache..

另外,尝试制作2-3个php文件,将浏览器指向它们,然后查看APC缓存缓存了多少个php文件。

Not sure about APC, but i'd recommend Eaccelerator (http://eaccelerator.net/). I've tested it some time ago and really made a difference.

不确定 APC,但我推荐 Eaccelerator ( http://eaccelerator.net/)。我前段时间测试过它,确实有所作为。

回答by Benjamin Cremer

APC actually doesnt share cache between proceses. Try to configure your PHP-Process to fork Childs and let the Master PHP-Process stay running.

APC 实际上并不在进程之间共享缓存。尝试将您的 PHP 进程配置为派生 Childs 并让主 PHP 进程保持运行。

See: How to share APC cache between several PHP processes when running under FastCGI?

请参阅:在 FastCGI 下运行时如何在多个 PHP 进程之间共享 APC 缓存?

回答by smokris

In the APC 3.1.5dev branch, a new option was introducedallowing you to disable the opcode cache. It appears to be turned off by default.

在 APC 3.1.5dev 分支中,引入了一个新选项,允许您禁用操作码缓存。 它似乎默认关闭。

Try adding this to your APC config file:

尝试将其添加到您的 APC 配置文件中:

apc.enable_opcode_cache=1

回答by ünsal Korkmaz

http://www.litespeedtech.com/support/forum/showthread.php?t=3761

http://www.litespeedtech.com/support/forum/showthread.php?t=3761

"APC does not work well with PHP suEXEC as each PHP process will use its own cache memory, not shared.

“APC 不能很好地与 PHP suEXEC 配合使用,因为每个 PHP 进程将使用自己的缓存内存,而不是共享的。

You should try eAccelerator + /dev/shm if you need to use PHP suEXEC."

如果您需要使用 PHP suEXEC,您应该尝试 eAccelerator + /dev/shm。”

well got same problem, it seems PHP suEXEC + APC is problem

也有同样的问题,看来 PHP suEXEC + APC 是问题