gordalina / cachetool
通过CLI管理您的OPcache & APCu缓存
Requires
- php: >=8.1.0
- consolidation/self-update: ~2.1.0
- hollodotme/fast-cgi-client: ^3.0
- monolog/monolog: ^3.0
- psr/log: ^2.0 || ^3.0
- symfony/console: ^6.0
- symfony/dependency-injection: ^6.0
- symfony/finder: ^6.0
- symfony/http-client: ^6.0
- symfony/http-foundation: ^6.0
- symfony/process: ^6.0
- symfony/yaml: ^6.0
Requires (Dev)
- phpunit/phpunit: ^9.0
- dev-main
- 9.1.0
- 9.0.4
- 9.0.3
- 9.0.2
- 9.0.1
- 9.0.0
- 8.x-dev
- 8.6.1
- 8.5.1
- 8.5.0
- 8.4.1
- 8.4.0
- 8.3.1-alpha.0
- 8.3.0
- 8.2.0
- 8.1.0
- 8.1.0-alpha.1
- 8.1.0-alpha.0
- 8.0.3-alpha.0
- 8.0.2
- 8.0.1
- 8.0.1-alpha.1
- 8.0.1-alpha.0
- 8.0.0
- 7.x-dev
- 7.1.0
- 7.0.0
- 7.0.0-alpha.3
- 6.x-dev
- 6.6.0
- 6.6.0-alpha.0
- 6.5.0
- 6.4.0
- 6.4.0-alpha.1
- 6.4.0-alpha.0
- 6.3.1
- 6.3.0
- 6.2.0
- 6.1.2
- 6.1.1
- 6.1.0
- 6.0.0
- 5.x-dev
- 5.1.3
- 5.1.2
- 5.1.1
- 5.1.0
- 5.0.0
- 4.x-dev
- 4.1.1
- 4.1.0
- 4.0.1
- 4.0.0
- 3.x-dev
- 3.2.2
- 3.2.1
- 3.1.0
- 3.0.0
- 2.x-dev
- 2.1.0
- 2.0.6
- 2.0.5
- 2.0.4
- 2.0.3
- 2.0.2
- 2.0.1
- 2.0.0
- 1.x-dev
- 1.12.0
- 1.11.0
- 1.10.2
- 1.10.1
- 1.10.0
- 1.9.1
- 1.9.0
- 1.8.1
- 1.8.0
- 1.7.1
- 1.7.0
- 1.6.1
- 1.6.0
- 1.5.0
- 1.4.3
- 1.4.2
- 1.4.1
- 1.4.0
- 1.3.6
- 1.3.5
- 1.3.4
- 1.3.3
- 1.3.2
- 1.3.1
- 1.3.0
- 1.2.2
- 1.2.1
- 1.2.0
- 1.1.2
- 1.1.1
- 1.1.0
- 1.0.3
- 1.0.2
- 1.0.1
- 1.0.0
This package is auto-updated.
Last update: 2024-09-02 18:08:09 UTC
README
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 hub和github容器注册库中找到。
以下为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需要一个适配器来连接,可以是 cli
、fcgi
和 web
。其中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
您可以在配置文件中定义支持的扩展。默认情况下,已启用apcu
和opcache
。要禁用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');
您可以使用apcu
和opcache
函数
$cache->apcu_clear_cache(); $cache->opcache_reset();
代理
CacheTool依赖于Proxies
来提供功能,默认情况下,从工厂创建CacheTool实例时,所有代理都启用(ApcuProxy
、OpcacheProxy
和PhpProxy
),您可以根据需要自定义它或扩展它,如下面的示例所示
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/FileGetContentsTest
和tests/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。