php 如何清除APC缓存条目?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/911158/
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 clear APC cache entries?
提问by lo_fye
I need to clear all APC cache entries when I deploy a new version of the site. APC.php has a button for clearing all opcode caches, but I don't see buttons for clearing all User Entries, or all System Entries, or all Per-Directory Entries.
当我部署站点的新版本时,我需要清除所有 APC 缓存条目。APC.php 有一个用于清除所有操作码缓存的按钮,但我没有看到用于清除所有用户条目、所有系统条目或所有每目录条目的按钮。
Is it possible to clear all cache entries via the command-line, or some other way?
是否可以通过命令行或其他方式清除所有缓存条目?
采纳答案by Travis Beale
You can use the PHP function apc_clear_cache.
您可以使用 PHP 函数apc_clear_cache。
Calling apc_clear_cache()will clear the system cache and calling apc_clear_cache('user')will clear the user cache.
调用apc_clear_cache()将清除系统缓存,调用apc_clear_cache('user')将清除用户缓存。
回答by Jeremy Kauffman
I don't believe any of these answers actually work for clearing the APC cache from the command line. As Frank Farmercommented above, the CLI runs in a process separate from Apache.
我不相信这些答案中的任何一个实际上都适用于从命令行清除 APC 缓存。正如Frank Farmer在上面评论的那样,CLI 在一个独立于 Apache 的进程中运行。
My solution for clearing from the command line was to write a script that copies an APC clearing script to the webdirectory and accesses it and then deletes it. The script is restricted to being accessed from the localhost.
我从命令行清除的解决方案是编写一个脚本,将 APC 清除脚本复制到web目录并访问它,然后将其删除。该脚本仅限于从本地主机访问。
apc_clear.php
This is the file that the script copies to the web directory, accesses, and deletes.
<?php if (in_array(@$_SERVER['REMOTE_ADDR'], array('127.0.0.1', '::1'))) { apc_clear_cache(); apc_clear_cache('user'); apc_clear_cache('opcode'); echo json_encode(array('success' => true)); } else { die('SUPER TOP SECRET'); }Cache clearing script
This script copies apc_clear.php to the web directory, accesses it, then deletes it. This is based off of a Symfony task. In the Symfony version, calls are made to the Symfony form of copy and unlink, which handle errors. You may want to add checks that they succeed.
copy($apcPaths['data'], $apcPaths['web']); //'data' is a non web accessable directory $url = 'http://localhost/apc_clear.php'; //use domain name as necessary $result = json_decode(file_get_contents($url)); if (isset($result['success']) && $result['success']) { //handle success } else { //handle failure } unlink($apcPaths['web']);
apc_clear.php
这是脚本复制到 Web 目录、访问和删除的文件。
<?php if (in_array(@$_SERVER['REMOTE_ADDR'], array('127.0.0.1', '::1'))) { apc_clear_cache(); apc_clear_cache('user'); apc_clear_cache('opcode'); echo json_encode(array('success' => true)); } else { die('SUPER TOP SECRET'); }缓存清除脚本
此脚本将 apc_clear.php 复制到 web 目录,访问它,然后将其删除。这是基于 Symfony 任务。在 Symfony 版本中,调用 Symfony 形式的复制和取消链接来处理错误。您可能想要添加检查它们是否成功。
copy($apcPaths['data'], $apcPaths['web']); //'data' is a non web accessable directory $url = 'http://localhost/apc_clear.php'; //use domain name as necessary $result = json_decode(file_get_contents($url)); if (isset($result['success']) && $result['success']) { //handle success } else { //handle failure } unlink($apcPaths['web']);
回答by Tadas Sasnauskas
I know it's not for everyone but: why not to do a graceful Apache restart?
我知道这并不适合所有人,但是:为什么不优雅地重启 Apache?
For e.g. in case of Centos/RedHat Linux:
例如在 Centos/RedHat Linux 的情况下:
sudo service httpd graceful
Ubuntu:
Ubuntu:
sudo service apache2 graceful
回答by ColinM
This is not stated in the documentation, but to clear the opcode cache you must do:
这在文档中没有说明,但要清除操作码缓存,您必须执行以下操作:
apc_clear_cache('opcode');
EDIT: This seems to only apply to some older versions of APC..
编辑:这似乎只适用于一些旧版本的 APC..
No matter what version you are using you can't clear mod_php or fastcgi APC cache from a php cli script since the cli script will run from a different process as mod_php or fastcgi. You must call apc_clear_cache() from within the process (or child process) which you want to clear the cache for. Using curl to run a simple php script is one such approach.
无论您使用什么版本,您都无法从 php cli 脚本中清除 mod_php 或 fastcgi APC 缓存,因为 cli 脚本将在与 mod_php 或 fastcgi 不同的进程中运行。您必须从要为其清除缓存的进程(或子进程)中调用 apc_clear_cache()。使用 curl 运行一个简单的 php 脚本就是这样一种方法。
回答by Léo Benoist
If you want to clear apc cache in command : (use sudo if you need it)
如果要清除命令中的 apc 缓存:(如果需要,请使用 sudo)
APCu
铜
php -r "apcu_clear_cache();"
APC
APC
php -r "apc_clear_cache(); apc_clear_cache('user'); apc_clear_cache('opcode');"
回答by passion4code
If you are running on an NGINX / PHP-FPM stack, your best bet is to probably just reload php-fpm
如果您在 NGINX / PHP-FPM 堆栈上运行,最好的办法是重新加载 php-fpm
service php-fpm reload(or whatever your reload command may be on your system)
service php-fpm reload(或任何您的系统上的重新加载命令)
回答by codersofthedark
As defined in APC Document:
如 APC 文档中所定义:
To clear the cache run:
要清除缓存运行:
php -r 'function_exists("apc_clear_cache") ? apc_clear_cache() : null;'
回答by COil
If you want to monitor the results via json, you can use this kind of script:
如果你想通过json监控结果,你可以使用这种脚本:
<?php
$result1 = apc_clear_cache();
$result2 = apc_clear_cache('user');
$result3 = apc_clear_cache('opcode');
$infos = apc_cache_info();
$infos['apc_clear_cache'] = $result1;
$infos["apc_clear_cache('user')"] = $result2;
$infos["apc_clear_cache('opcode')"] = $result3;
$infos["success"] = $result1 && $result2 && $result3;
header('Content-type: application/json');
echo json_encode($infos);
As mentioned in other answers, this script will have to be called via http or curl and you will have to be secured if it is exposed in the web root of your application. (by ip, token...)
如其他答案中所述,必须通过 http 或 curl 调用此脚本,如果它在应用程序的 Web 根目录中公开,则必须确保安全。(通过IP,令牌...)
回答by Andy Triggs
Another possibility for command-line usage, not yet mentioned, is to use curl.
命令行使用的另一种可能性(尚未提及)是使用 curl。
This doesn't solve your problem for all cache entries if you're using the stock apc.php script, but it could call an adapted script or another one you've put in place.
如果您使用的是库存 apc.php 脚本,这并不能解决所有缓存条目的问题,但它可以调用一个改编的脚本或您已经放置的另一个脚本。
This clears the opcode cache:
这会清除操作码缓存:
curl --user apc:$PASSWORD "http://www.example.com/apc.php?CC=1&OB=1&`date +%s`"
Change the OB parameter to 3 to clear the user cache:
将 OB 参数更改为 3 以清除用户缓存:
curl --user apc:$PASSWORD "http://www.example.com/apc.php?CC=1&OB=3&`date +%s`"
Put both lines in a script and call it with $PASSWORD in your env.
将这两行放在一个脚本中,并在你的环境中用 $PASSWORD 调用它。
回答by Samuel Gordalina
apc_clear_cache() only works on the same php SAPI that you want you cache cleared. If you have PHP-FPM and want to clear apc cache, you have do do it through one of php scripts, NOT the command line, because the two caches are separated.
apc_clear_cache() 仅适用于您希望清除缓存的相同 php SAPI。如果你有 PHP-FPM 并且想要清除 apc 缓存,你必须通过 php 脚本之一来完成,而不是命令行,因为这两个缓存是分开的。
I have written CacheTool, a command line tool that solves exactly this problem and with one command you can clear your PHP-FPM APC cache from the commandline (it connects to php-fpm for you, and executes apc functions)
我写了CacheTool,这是一个命令行工具,可以解决这个问题,你可以用一个命令从命令行清除你的 PHP-FPM APC 缓存(它为你连接到 php-fpm,并执行 apc 函数)
It also works for opcache.
它也适用于 opcache。
See how it works here: http://gordalina.github.io/cachetool/
在这里查看它是如何工作的:http: //gordalina.github.io/cachetool/

