gordalina/cachetool

通过CLI管理您的OPcache & APCu缓存

资助包维护!
gordalina

安装次数: 2,781,565

依赖关系: 9

建议者: 0

安全: 0

星标: 1,614

关注者: 33

分支: 153

开放问题: 14


README

Build Status Coverage Status Version Downloads

CacheTool 允许您通过CLI操作APCu、OPcache和文件状态缓存。它将连接到FastCGI服务器(如PHP-FPM)并对其缓存进行操作。

这有什么用?

  • 也许您想清除字节码缓存而不重新加载php-fpm或使用Web端点
  • 也许您想有一个处理缓存失效的cron任务
  • 也许您想从控制台查看一些统计数据
  • 还有很多...

注意,与APCu和Opcache不同,文件状态缓存是按进程划分的,而不是存储在共享内存中。这意味着运行stat:clear针对PHP-FPM将只影响响应请求的任何FPM工作进程,而不是整个池。 Julien Pauli 写了一篇关于文件状态缓存如何操作的详细文章

安装 - 最新版本

curl -sLO https://github.com/gordalina/cachetool/releases/latest/download/cachetool.phar
chmod +x cachetool.phar

您还可以通过以下URL下载压缩的phar。

# if your php installation has the zlib extension enabled
https://github.com/gordalina/cachetool/releases/latest/download/cachetool.phar.gz

# if your php installation has the bzip2 extension enabled
https://github.com/gordalina/cachetool/releases/latest/download/cachetool.phar.bz2

CacheTool 还作为docker容器打包,可在docker hubgithub容器注册库中找到。

以下为docker使用说明

安装 - 旧版本

在二进制文件名中使用标签名称。例如,要下载与PHP >=5.5.9 兼容的cachetool 3.2.2,请使用:cachetool-3.2.2.phar

curl -sO https://gordalina.github.io/cachetool/downloads/cachetool-3.2.2.phar
chmod +x cachetool-3.2.2.phar

使用

CacheTool需要一个适配器来连接,可以是 clifcgiweb。其中fcgi适配器是最常见的,因为它直接连接到php-fpm。

您可以将IP地址或Unix套接字传递给--fcgi适配器,或者将其留空,CacheTool将尝试为您找到php-fpm套接字。如果找不到它,它将默认为127.0.0.1:9000

  • 您可以让CacheTool为您找到Unix套接字,或者默认为IP。
php cachetool.phar apcu:cache:info --fcgi
  • 您可以使用IP地址连接到fastcgi服务器
php cachetool.phar apcu:cache:info --fcgi=127.0.0.1:9000
  • 您可以使用Unix套接字连接到fastcgi服务器
php cachetool.phar opcache:status --fcgi=/var/run/php5-fpm.sock
  • 要连接到chrooted fastcgi服务器,您需要设置--fcgi-chroot--tmp-dir参数
php cachetool.phar opcache:status --fcgi=/var/run/php5-fpm.sock --fcgi-chroot=/path/to/chroot --tmp-dir=/path/to/chroot/tmp
  • 使用CLI
php cachetool.phar opcache:status --cli
  • 使用HTTP接口
php cachetool.phar opcache:status --web --web-path=/path/to/your/document/root --web-url=http://url-to-your-document.root
  • 使用SymfonyHttpClient
php cachetool.phar opcache:status --web=SymfonyHttpClient --web-path=/path/to/your/document/root --web-url=http://url-to-your-document.root

您有一些有用的命令可以使用

 apcu
  apcu:cache:clear            Clears APCu cache
  apcu:cache:info             Shows APCu user & system cache information
  apcu:cache:info:keys        Shows APCu keys cache information
  apcu:key:delete             Deletes an APCu key
  apcu:key:exists             Checks if an APCu key exists
  apcu:key:fetch              Shows the content of an APCu key
  apcu:key:store              Store an APCu key with given value
  apcu:regexp:delete          Deletes all APCu key matching a regexp
  apcu:sma:info               Show APCu shared memory allocation information
 opcache
  opcache:compile:script      Compile single script from path to the opcode cache
  opcache:compile:scripts     Compile scripts from path to the opcode cache
  opcache:configuration       Get configuration information about the cache
  opcache:invalidate:scripts  Remove scripts from the opcode cache
  opcache:reset               Resets the contents of the opcode cache
  opcache:reset:file-cache    Deletes all contents of the file cache directory
  opcache:status              Show summary information about the opcode cache
  opcache:status:scripts      Show scripts in the opcode cache
 stat
  stat:clear                  Clears the file status cache, including the realpath cache
  stat:realpath_get           Show summary information of realpath cache entries
  stat:realpath_size          Display size of realpath cache

通过Docker使用

图像可在docker hub和github容器注册库中找到

  • gordalina/cachetool:latest
  • ghcr.io/gordalina/cachetool:latest

这是使用web适配器运行的一个示例

APPDIR="/var/www/example.com"
DOCROOT="/var/www/example.com/current/web"
URL="http://example.com"

docker run --rm -v $APPDIR:$APPDIR -w $DOCROOT gordalina/cachetool cachetool --web --web-url=$URL [options] [arguments]

如果网站位于代理和/或负载均衡器后面,您可能想直接询问Web服务器而不是面向公众的IP。此外,Web服务器可能在另一个端口上监听。以下是在此设置中从Web服务器主机运行cachetool的示例

DOMAIN="example.com"
PORT="8008"
APPDIR="/var/www/example.com"
DOCROOT="/var/www/example.com/current/web"
URL="http://$DOMAIN:$PORT"

docker run --rm --add-host $DOMAIN:172.17.0.1 -v $APPDIR:$APPDIR -w $DOCROOT sbitio/cachetool --web --web-url=$URL [options] [arguments]

感谢@jonhattan和@NITEMAN在docker上的工作,[点击此处](https://github.com/sbitio/docker-cachetool)。

配置文件

您可以使用配置文件来配置适配器,这样您就可以在调用CacheTool时无需使用--fcgi--cli--web选项。

您可以将--config <file>选项传递给应用程序,或者它将自动加载文件。

该文件必须命名为.cachetool.yml.cachetool.yaml。CacheTool将在当前目录及其任何父目录中查找此文件,直到找到为止。如果上述路径失败,它将尝试加载/etc/cachetool.yml/etc/cachetool.yaml配置文件。

以下是一个该文件的示例:

将连接到127.0.0.1:9000的fastcgi

adapter: fastcgi
fastcgi: 127.0.0.1:9000

将连接到cli(忽略fastcgi配置)

adapter: cli
fastcgi: /var/run/php5-fpm.sock

CacheTool将文件写入系统临时目录(由sys_get_temp_dir()给出),但如果您想更改此目录,例如,如果您的fastcgi服务使用PrivateTemp运行,您可以在配置文件中设置它

adapter: fastcgi
fastcgi: /var/run/php5-fpm.sock
temp_dir: /dev/shm/cachetool

Web适配器示例

adapter: web
webClient: SymfonyHttpClient # defaults to FileGetContents
webUrl: http://example.com
webPath: /var/www/example.com/current/web
webBasicAuth: user:password

您可以在配置文件中定义支持的扩展。默认情况下,已启用apcuopcache。要禁用apcu,请在您的配置文件中添加以下内容:

extensions: [opcache]

用法(作为库)

将其作为依赖项添加

composer require gordalina/cachetool

如果您想在Symfony 2.x项目中使用它,则需要要求1.x版本

composer require gordalina/cachetool:~1.0

创建实例

use CacheTool\Adapter\FastCGI;
use CacheTool\CacheTool;

$adapter = new FastCGI('127.0.0.1:9000');
$cache = CacheTool::factory($adapter, '/tmp');

您可以使用apcuopcache函数

$cache->apcu_clear_cache();
$cache->opcache_reset();

代理

CacheTool依赖于Proxies来提供功能,默认情况下,从工厂创建CacheTool实例时,所有代理都启用(ApcuProxyOpcacheProxyPhpProxy),您可以根据需要自定义它或扩展它,如下面的示例所示

use CacheTool\Adapter\FastCGI;
use CacheTool\CacheTool;
use CacheTool\Proxy;

$adapter = new FastCGI('/var/run/php5-fpm.sock');
$cache = new CacheTool();
$cache->setAdapter($adapter);
$cache->addProxy(new Proxy\ApcuProxy());
$cache->addProxy(new Proxy\PhpProxy());

更新CacheTool

运行php cachetool.phar self-update将更新phar安装到最新版本。

构建cachetool.phar

Cachetool使用box构建phar,有关在您的情况下最佳安装方式的详细信息,请参阅box-project/installation.md。要构建,请运行box compile,它将在项目根目录中输出cachetool.phar

测试

运行composer install后,运行./vendor/bin/phpunit

处理测试失败

sslip.io

tests/Adapter/Http/FileGetContentsTesttests/Adapter/Http/SymfonyHttpClientTest中的测试依赖于sslip.io来解决包含IP的主机名到包含IP的IP。为此,需要在运行这些测试的主机配置的DNS服务器上有一个sslip.io的nameserver,否则用于测试的_.127.0.0.1.sslip.io等主机名将无法解析。DNS服务器的IP地址可以在sslip.io上找到,如何配置它们取决于用于运行测试的系统。

版本兼容性

许可证

CacheTool根据MIT许可证授权 - 有关详细信息,请参阅LICENSE