icedevelopment / apc-bundle
提供从控制台清除APC缓存的命令行任务
v5.0
2022-05-27 12:08 UTC
Requires
- symfony/framework-bundle: >=4.4
README
提供从控制台清除APC缓存的命令行
APC的问题在于无法从命令行清除它。因为即使你启用了PHP CLI的APC,它也是一个不同于Apache PHP或PHP-CGI APC实例的不同实例。
这里的技巧是在Web目录中创建一个文件,通过HTTP执行它,然后删除它。
先决条件
如果你想清除APC的Apache部分,你需要在php.ini
中启用allow_url_fopen
以允许打开类似URL的对象文件,或者设置curl选项。
安装
-
将其添加到你的composer.json
{ "require": { "ornicar/apc-bundle": "1.0.*" } }
或者
composer require ornicar/apc-bundle composer update ornicar/apc-bundle
-
将此包添加到你的应用程序内核
// app/AppKernel.php public function registerBundles() { return array( // ... new Ornicar\ApcBundle\OrnicarApcBundle(), // ... ); }
-
配置
ornicar_apc
服务# app/config/config.yml ornicar_apc: host: http://example.com web_dir: %kernel.root_dir%/../web
-
如果你想使用curl而不是fopen设置以下选项
# app/config/config.yml ornicar_apc: ... mode: curl
用法
清除所有APC缓存(opcode+user)
$ php app/console apc:clear
仅清除opcode缓存
$ php app/console apc:clear --opcode
仅清除user缓存
$ php app/console apc:clear --user
清除CLI缓存(opcode+user)
$ php app/console apc:clear --cli
Capifony使用
要自动在每次capifony部署后清除apc缓存,你可以定义一个自定义任务
namespace :symfony do desc "Clear apc cache" task :clear_apc do capifony_pretty_print "--> Clear apc cache" run "#{try_sudo} sh -c 'cd #{current_path} && #{php_bin} #{symfony_console} apc:clear --env=#{symfony_env_prod}'" capifony_puts_ok end end
并添加以下钩子
# apc after "deploy", "symfony:clear_apc" after "deploy:rollback:cleanup", "symfony:clear_apc"
Nginx配置
如果你使用nginx并且限制传递给fpm的PHP脚本,你需要允许以'apc'开头的php文件。否则,你的web服务器将返回请求的PHP文件作为文本,系统将无法清除apc缓存。
示例配置
# Your virtual host
server {
...
location ~ ^/(app|app_dev|apc-.*)\.php(/|$) { { # This will allow apc (apc-{MD5HASH}.php) files to be processed by fpm
fastcgi_pass 127.0.0.1:9000;
...