简体   繁体   English

如何清除APC缓存条目?

[英]How to clear APC cache entries?

I need to clear all APC cache entries when I deploy a new version of the site.当我部署站点的新版本时,我需要清除所有 APC 缓存条目。 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.php 有一个用于清除所有操作码缓存的按钮,但我没有看到用于清除所有用户条目、所有系统条目或所有每目录条目的按钮。

Is it possible to clear all cache entries via the command-line, or some other way?是否可以通过命令行或其他方式清除所有缓存条目?

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')将清除用户缓存。

I don't believe any of these answers actually work for clearing the APC cache from the command line. 我不相信这些答案中的任何一个实际上都可以用于从命令行清除APC缓存。 As Frank Farmer commented above, the CLI runs in a process separate from Apache. 正如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 web directory and accesses it and then deletes it. 我从命令行清除的解决方案是编写一个脚本,将APC清除脚本复制到web目录并访问它然后将其删除。 The script is restricted to being accessed from the localhost. 该脚本仅限于从localhost访问。

  1. apc_clear.php apc_clear.php

    This is the file that the script copies to the web directory, accesses, and deletes. 这是脚本复制到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'); } 
  2. Cache clearing script 缓存清除脚本

    This script copies apc_clear.php to the web directory, accesses it, then deletes it. 此脚本将apc_clear.php复制到Web目录,访问它,然后删除它。 This is based off of a Symfony task. 这是基于Symfony任务。 In the Symfony version, calls are made to the Symfony form of copy and unlink, which handle errors. 在Symfony版本中,调用Symfony形式的copy和unlink,它们处理错误。 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']); 

I know it's not for everyone but: why not to do a graceful Apache restart? 我知道并不适合所有人,但是:为什么不重新启动优雅的Apache?

For eg in case of Centos/RedHat Linux: 例如,对于Centos / RedHat Linux:

sudo service httpd graceful

Ubuntu: Ubuntu的:

sudo service apache2 graceful

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. 无论您使用什么版本,都无法从php cli脚本中清除mod_php或fastcgi APC缓存,因为cli脚本将以不同的进程运行为mod_php或fastcgi。 You must call apc_clear_cache() from within the process (or child process) which you want to clear the cache for. 您必须从要清除缓存的进程(或子进程)中调用apc_clear_cache()。 Using curl to run a simple php script is one such approach. 使用curl运行一个简单的PHP脚本就是这样一种方法。

If you want to clear apc cache in command : (use sudo if you need it) 如果要在命令中清除apc cache :(如果需要,请使用sudo)

APCu APCu

php -r "apcu_clear_cache();" 

APC APC

php -r "apc_clear_cache(); apc_clear_cache('user'); apc_clear_cache('opcode');"

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 (或者你的系统上的重载命令)

As defined in APC Document: 如APC文件中所定义:

To clear the cache run: 要清除缓存运行:

php -r 'function_exists("apc_clear_cache") ? apc_clear_cache() : null;'

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. 如果您正在使用stock 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. 将两行放在脚本中并在env中使用$ PASSWORD调用它。

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. 如其他答案所述,此脚本必须通过http或curl调用,如果它在应用程序的Web根目录中公开,则必须加以保护。 (by ip, token...) (通过ip,令牌......)

apc_clear_cache() only works on the same php SAPI that you want you cache cleared. apc_clear_cache()仅适用于您希望缓存清除的同一个php SAPI。 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. 如果你有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/

The stable of APC is having option to clear a cache in its interface itself. APC的稳定性可以选择在其界面中清除缓存。 To clear those entries you must login to apc interface. 要清除这些条目,您必须登录到apc界面。

APC is having option to set username and password in apc.php file. APC可以选择在apc.php文件中设置用户名和密码。

在此输入图像描述

如果你在ubuntu下运行fpm,需要运行下面的代码(在12和14上检查)

service php5-fpm reload

apc.ini apc.ini

apc.stat = "1" will force APC to stat (check) the script on each request to determine if it has been modified. apc.stat =“1”将强制APC对每个请求上的脚本进行统计(检查)以确定它是否已被修改。 If it has been modified it will recompile and cache the new version. 如果它已被修改,它将重新编译并缓存新版本。

If this setting is off, APC will not check, which usually means that to force APC to recheck files, the web server will have to be restarted or the cache will have to be manually cleared. 如果此设置关闭,APC将不会检查,这通常意味着强制APC重新检查文件,必须重新启动Web服务器或必须手动清除缓存。 Note that FastCGI web server configurations may not clear the cache on restart. 请注意,FastCGI Web服务器配置可能无法在重新启动时清除缓存。 On a production server where the script files rarely change, a significant performance boost can be achieved by disabled stats. 在脚本文件很少更改的生产服务器上,禁用统计信息可以显着提高性能。

New APC Admin interface have options to add/clear user cache and opcode cache, One interesting functionality is to add/refresh/delete directory's from opCode Cache 新的APC Admin界面有添加/清除用户缓存和操作码缓存的选项,一个有趣的功能是从opCode Cache添加/刷新/删除目录

APC Admin Documentation APC管理员文档

在此输入图像描述

Create APC.php file 创建APC.php文件

foreach(array('user','opcode','') as $v ){
    apc_clear_cache($v);
}

Run it from your browser. 从浏览器运行它。

A good solution for me was to simply not using the outdated user cache any more after deploy. 对我来说一个很好的解决方案是在部署后不再使用过时的用户缓存。

If you add prefix to each of you keys you can change the prefix on changing the data structure of cache entries. 如果为每个键添加前缀,则可以在更改缓存条目的数据结构时更改前缀。 This will help you to get the following behavior on deploy: 这将有助于您在部署时获得以下行为:

  1. Don't use outdated cache entries after deploy of only updated structures 在仅部署更新的结构后,请勿使用过时的缓存条目
  2. Don't clean the whole cache on deploy to not slow down your page 不要在部署时清理整个缓存以减慢页面速度
  3. Some old cached entries can be reused after reverting your deploy (If the entries wasn't automatically removed already) 恢复部署后,可以重用一些旧的缓存条目(如果条目未自动删除)
  4. APC will remove old cache entries after expire OR on missing cache space APC将在到期后删除旧缓存条目删除缓存空间

This is possible for user cache only. 这仅适用于用户缓存。

My work-around for Symfony build having loot of instances at the same server: 我在Symfony构建的解决方案是在同一台服务器上有一堆实例:

Step 1. Create trigger or something to set a file flag (eg. Symfony command) then create marker file .. 步骤1.创建触发器或其他东西以设置文件标志(例如,Symfony命令),然后创建marker file

file_put_contents('clearAPCU','yes sir i can buggy')

Step 2. On index file at start add clearing code and remove marker file . 步骤2.在开始时的索引文件中添加清除代码并删除marker file

if(file_exists('clearAPCU')){
    apcu_clear_cache();
    unlink('clearAPCU');
}

Step 2. Run app. 第2步。运行应用。

TL;DR: delete cache files at /storage/framework/cache/data/ TL;DR:删除/storage/framework/cache/data/缓存文件

I enabled APC but it wasn't installed (and also couldn't be installed), so it threw Call to undefined function Illuminate\\Cache\\apc_store() .我启用了 APC 但它没有安装(也无法安装),所以它抛出了Call to undefined function Illuminate\\Cache\\apc_store()

"Ok, I'd just disable it and it should work". “好吧,我只是禁用它,它应该可以工作”。

Well, nope.嗯,不。 Then I got stuck with Call to undefined function Illuminate\\Cache\\apc_delete()然后我陷入了Call to undefined function Illuminate\\Cache\\apc_delete()

We had a problem with APC and symlinks to symlinks to files -- it seems to ignore changes in files itself. 我们遇到了APC和符号链接到文件的符号链接问题 - 它似乎忽略了文件本身的变化。 Somehow performing touch on the file itself helped. 以某种方式对文件本身执行触摸有帮助。 I can not tell what's the difference between modifing a file and touching it, but somehow it was necessary... 我不知道修改文件和触摸文件之间有什么区别,但不知何故有必要......

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM