我正在尝试用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;
}
}感谢你可能会有所帮助的任何答案
发布于 2015-10-22 13:44:58
如果你使用的是GUISH5,可以使用这个缓存订户https://github.com/EmanueleMinotto/guzzle-cache-subscriber,它可以提供不同的缓存提供商,如FileSystem,Redis等,在doctrine缓存中实现。
发布于 2015-01-27 06:00:28
不确定您是否在同一条船上,但您使用的是主分支吗?
我自己设置了缓存订阅者,发现缓存的响应总是无效的,因为没有设置默认的ttl值。
我发现这是一个问题,因为我使用的是0.1.0版本,但它似乎在主(https://github.com/guzzle/cache-subscriber/blob/master/src/CacheStorage.php#L48)上得到了纠正,在那里你可以设置一个默认的ttl。
https://stackoverflow.com/questions/28003302
复制相似问题