smart-core / accelerator-cache-bundle
提供从控制台清除PHP加速器缓存的命令行任务
1.3.0
2017-11-21 06:48 UTC
Requires
- symfony/console: >=2.1
- symfony/framework-bundle: >=2.1
Requires (Dev)
- composer/composer: 1.0.*@dev
- matthiasnoback/symfony-dependency-injection-test: ~2.1
- phpunit/phpunit: ~6.3
README
提供命令行以从控制台清除PHP加速器缓存。
加速器缓存(如APC、Wincache、Opcache)的问题是从命令行无法清除它。因为即使你为PHP CLI启用APC,它也是一个与Apache PHP或PHP-CGI APC实例不同的实例。
这里的技巧是在Web目录中创建一个文件,通过HTTP执行它,然后删除它。
先决条件
如果你要清除APC的Apache部分,你需要在php.ini中启用allow_url_fopen
以允许打开类似URL的对象文件,或设置curl选项。
安装
- 将其添加到你的composer.json文件中
{ "require": { "smart-core/accelerator-cache-bundle": "dev-master" } }
or:
composer require smart-core/accelerator-cache-bundle composer update smart-core/accelerator-cache-bundle
- 将此包添加到你的应用程序内核中
// app/AppKernel.php public function registerBundles() { return array( // ... new SmartCore\Bundle\AcceleratorCacheBundle\AcceleratorCacheBundle(), // ... ); }
- 配置
accelerator_cache
服务
# app/config/config.yml accelerator_cache: host: http://example.com web_dir: %kernel.project_dir%/web
- 如果你想使用curl而不是fopen,设置以下选项
# app/config/config.yml accelerator_cache: ... mode: curl # additional options can be passed to the command # curl_opts: # CURLOPT_*: custom_value
使用方法
清除所有加速器缓存(opcode+user)
$ php app/console cache:accelerator:clear
仅清除opcode缓存
$ php app/console cache:accelerator:clear --opcode
仅清除用户缓存
$ php app/console cache:accelerator:clear --user
仅清除CLI缓存(opcode+user)
$ php app/console cache:accelerator:clear --cli
Composer使用
要自动在每次composer安装/更新后清除加速器缓存,你可以在项目的composer.json中添加一个脚本处理器
"post-install-cmd": [ "SmartCore\\Bundle\\AcceleratorCacheBundle\\Composer\\ScriptHandler::clearCache" ], "post-update-cmd": [ "SmartCore\\Bundle\\AcceleratorCacheBundle\\Composer\\ScriptHandler::clearCache" ]
+你可以在extra
部分指定命令参数
--opcode
(仅清理opcode缓存)
"extra": { "accelerator-cache-opcode": "yes" }
--user
(仅清理用户缓存)
"extra": { "accelerator-cache-user": "yes" }
--cli
(仅通过CLI清除缓存)
"extra": { "accelerator-cache-cli": "yes" }
--auth
(HTTP认证)
"extra": { "accelerator-cache-auth": "username:password" }
Capifony使用
要在每次capifony部署后自动清除apc缓存,你可以定义一个自定义任务
namespace :symfony do desc "Clear accelerator cache" task :clear_accelerator_cache do capifony_pretty_print "--> Clear accelerator cache" run "#{try_sudo} sh -c 'cd #{latest_release} && #{php_bin} #{symfony_console} cache:accelerator:clear #{console_options}'" capifony_puts_ok end end
并添加以下钩子
# clear accelerator cache after "deploy", "symfony:clear_accelerator_cache" after "deploy:rollback:cleanup", "symfony:clear_accelerator_cache"
Nginx配置
如果你使用nginx并且限制传递给fpm的PHP脚本,你需要允许以'apc'为前缀的php文件。否则,你的Web服务器将返回请求的PHP文件作为文本,系统将无法清除加速器缓存。
示例配置
# Your virtual host
server {
...
location ~ ^/(app|app_dev|apc-.*)\.php(/|$) { { # This will allow accelerator cache (apc-{MD5HASH}.php) files to be processed by fpm
fastcgi_pass 127.0.0.1:9000;
...