peroks / guzzle-file-cache
为Guzzle 7+实现PSR-16缓存接口的轻量级文件缓存
1.0.2
2022-10-03 18:46 UTC
Requires
- php: ^7.4 || ^8.0
- guzzlehttp/guzzle: ^7.0
- psr/simple-cache: ^1.0
README
为Guzzle 7+实现PSR-16缓存接口的轻量级文件缓存
如何使用
<?php
use GuzzleHttp\Client;
use GuzzleHttp\HandlerStack;
use Peroks\GuzzleFileCache\Cache;
use Peroks\GuzzleFileCache\FileStorage;
function getGuzzleClient( array $options = [] ): Client {
$stack = HandlerStack::create();
$store = new FileStorage( 'your/cache/directory' );
$cache = new Cache( $store );
$stack->push( $cache );
$options['handler'] = $stack;
return new Client( $options );
}
您可以使用Peroks\GuzzleFileCache\Cache
类的实例将文件缓存作为处理程序(中间件)添加到Guzzle HTTP客户端。
Peroks\GuzzleFileCache\FileStorage
执行实际工作,实现了PSR-16: 缓存库通用接口。
在您向Guzzle HTTP客户端添加缓存后,会出现一个新的缓存选项:ttl
(存活时间)。ttl
值以秒为单位。
您可以为此客户端发送的每个请求设置此选项。如果您省略此选项或设置ttl
为0
,则请求不会被缓存。
$response = $client->send( $request, [ 'ttl' => 3600 ] ); // 1 hour caching.
安装
您需要composer
来下载和安装peroks/guzzle-file-cache。只需在项目中运行composer require peroks/guzzle-file-cache
即可。