caching - Guzzle 缓存订阅者

标签 caching guzzle

我正在尝试使用 guzzle 设置缓存,但似乎无法让它工作,也没有找到任何相关文档。我正在使用 guzzle 5 和描述服务来调用 Web API。缓存订阅者似乎已附加到客户端,但重新加载网页时,我的查询似乎没有发生缓存。

这是我用来构建客户端的 ClientFactory 类:

use Doctrine\Common\Cache\ArrayCache;
use Doctrine\Common\Cache\FilesystemCache;
use Guzzle\Plugin\Cache\CachePlugin;
use GuzzleHttp\Client;
use GuzzleHttp\Command\Guzzle\Description;
use GuzzleHttp\Command\Guzzle\GuzzleClient;
use GuzzleHttp\Subscriber\Cache\CacheStorage;
use GuzzleHttp\Subscriber\Cache\CacheSubscriber;

class ClientFactory
{
    public static function factory(array $config = [])
    {
    $defaultConfig = [
        'defaults' => [
            'headers' => ['User-Agent' => 'MyWebApi/1.0.0 +https://mywebsite.com'],
            'auth' => 'oauth'
        ],
    ];



    $client = new Client(self::mergeRecursive($defaultConfig, $config));
    $client->setDefaultOption('verify', false);

    $array = CacheSubscriber::attach($client, [ "storage" =>  new CacheStorage(new FilesystemCache("cache/"))]);
    $service = include __DIR__ . '/../../resources/service.php';
    $description = new Description($service);

    return new GuzzleClient($client, $description);
}

public static function &mergeRecursive(array &$array1, &$array2 = null)
{
    $merged = $array1;

    if (is_array($array2)) {
        foreach ($array2 as $key => $val) {
            if (is_array($array2[$key])) {
                $merged[$key] = is_array($merged[$key]) ? self::mergeRecursive($merged[$key], $array2[$key]) : $array2[$key];
            } else {
                $merged[$key] = $val;
            }
        }
    }

    return $merged;
}
}

感谢您的任何可能有帮助的回答

最佳答案

不确定您是否在同一条船上,但是您使用的是主分支吗?

我自己设置缓存订阅者,发现缓存的响应总是会变得无效,因为没有设置默认的 ttl 值。

我发现这是一个问题,因为我使用的是 0.1.0 版本,但它似乎在 master ( https://github.com/guzzle/cache-subscriber/blob/master/src/CacheStorage.php#L48 ) 上得到了纠正,您可以在其中设置默认 ttl。

关于caching - Guzzle 缓存订阅者,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28003302/

相关文章:

iphone - iOS 模拟器上的假互联网连接

php - 我可以将 Guzzle 用于 GraphQL API 消费吗?

php - 无法使用 try/catch 处理异常

php - 如何在 Guzzle 中设置默认标题?

laravel - Guzzle 不向 Laravel Passport 发送授权类型

java - Spring @Cacheable 双重检查锁定(infinispan 后端)

asp.net - 如何为客户端和服务器缓存设置不同的缓存过期时间

asp.net - HTTP缓存: Cache-Control

caching - EJB 单例作为共享内存?

php - 无法设置 Guzzle 内容类型