guzzlehttp/cache-subscriber

此包已被废弃,不再维护。未建议替代包。

Guzzle HTTP 缓存订阅者

0.2.0 2019-09-16 13:44 UTC

This package is auto-updated.

Last update: 2020-08-16 21:30:45 UTC


README

重要

此仓库未针对 Guzzle 6 进行更新,仅支持 Guzzle 5。

有关一个兼容 Guzzle 6 的缓存中间件的示例,请参阅 https://github.com/Kevinrob/guzzle-cache-middleware

提供用于缓存 HTTP 响应的私有透明代理缓存。

以下是一个简单示例,说明如何使用它

use GuzzleHttp\Client;
use GuzzleHttp\Subscriber\Cache\CacheSubscriber;

$client = new Client(['defaults' => ['debug' => true]]);

// Use the helper method to attach a cache to the client.
CacheSubscriber::attach($client);

// Send the first request
$a = $client->get('http://en.wikipedia.org/wiki/Main_Page');

// Send the second request. This will find a cache hit which must be
// validated. The validation request returns a 304, which yields the original
// cached response.
$b = $client->get('http://en.wikipedia.org/wiki/Main_Page');

运行上述代码示例应输出类似以下详细的 cURL 信息

> GET /wiki/Main_Page HTTP/1.1
Host: en.wikipedia.org
User-Agent: Guzzle/4.2.1 curl/7.37.0 PHP/5.5.13
Via: 1.1 GuzzleCache/4.2.1

< HTTP/1.1 200 OK
< Server: Apache
< X-Content-Type-Options: nosniff
< Content-language: en
< X-UA-Compatible: IE=Edge
< Vary: Accept-Encoding,Cookie
< Last-Modified: Thu, 21 Aug 2014 01:51:49 GMT
< Content-Type: text/html; charset=UTF-8
< X-Varnish: 2345493325, 1998949714 1994269567
< Via: 1.1 varnish, 1.1 varnish
< Transfer-Encoding: chunked
< Date: Thu, 21 Aug 2014 02:34:12 GMT
< Age: 2541
< Connection: keep-alive
< X-Cache: cp1055 hit (1), cp1068 frontend hit (25353)
< Cache-Control: private, s-maxage=0, max-age=0, must-revalidate
< Set-Cookie: GeoIP=US:Seattle:47.6062:-122.3321:v4; Path=/; Domain=.wikipedia.org
<
* Connection #0 to host en.wikipedia.org left intact
* Re-using existing connection! (#0) with host en.wikipedia.org
> GET /wiki/Main_Page HTTP/1.1
Host: en.wikipedia.org
User-Agent: Guzzle/4.2.1 curl/7.37.0 PHP/5.5.13
Via: 1.1 GuzzleCache/4.2.1, 1.1 GuzzleCache/4.2.1
If-Modified-Since: Thu, 21 Aug 2014 01:51:49 GMT

< HTTP/1.1 304 Not Modified
< Server: Apache
< X-Content-Type-Options: nosniff
< Content-language: en
< X-UA-Compatible: IE=Edge
< Vary: Accept-Encoding,Cookie
< Last-Modified: Thu, 21 Aug 2014 01:51:49 GMT
< Content-Type: text/html; charset=UTF-8
< X-Varnish: 2345493325, 1998950450 1994269567
< Via: 1.1 varnish, 1.1 varnish
< Date: Thu, 21 Aug 2014 02:34:12 GMT
< Age: 2541
< Connection: keep-alive
< X-Cache: cp1055 hit (1), cp1068 frontend hit (25360)
< Cache-Control: private, s-maxage=0, max-age=0, must-revalidate
< Set-Cookie: GeoIP=US:Seattle:47.6062:-122.3321:v4; Path=/; Domain=.wikipedia.org
<
* Connection #0 to host en.wikipedia.org left intact

安装

将以下内容添加到您的 composer.json 中

{
    "require": {
        "guzzlehttp/cache-subscriber": "0.2.*@dev"
    }
}

$ composer require guzzlehttp/cache-subscriber

创建一个 CacheSubscriber

创建 CacheSubscriber 的最简单方法是使用 GuzzleHttp\Subscriber\Cache\CacheSubscriberattach() 辅助方法。此方法接受一个请求或客户端对象,并附加必要的订阅者,用于执行缓存查找、验证请求和自动清除资源。

attach() 方法接受以下选项

storage
一个用于存储缓存响应的 GuzzleHttp\Subscriber\Cache\CacheStorageInterface 对象。如果不提供值,将使用内存数组缓存。
validate
一个布尔值,用于确定缓存响应是否始终与原始服务器进行验证。此设置默认为 true,但可以通过传递 false 禁用。
purge
一个布尔值,用于确定在向其 URI 发送非幂等请求时是否清除缓存响应。此设置默认为 true,但可以通过传递 false 禁用。
can_cache
一个可选的可调用对象,用于确定请求是否可以缓存。该可调用对象接受一个 GuzzleHttp\Message\RequestInterface,并返回一个布尔值。如果不提供值,则使用默认行为。

警告

这是 Guzzle 5+ 的 WIP 更新。它尚未经过测试,正在积极开发中。请预期会有错误和中断。