Skip to content

Instantly share code, notes, and snippets.

@holabene
Last active January 23, 2020 15:57
Show Gist options
  • Save holabene/6471423 to your computer and use it in GitHub Desktop.
Save holabene/6471423 to your computer and use it in GitHub Desktop.
Guzzle cache that actually works..
<?php
/**
* Caching to filesystem using ZendCache
* Cache as much as possible, very convenient for dev (default ttl = 3600)
*/
use Guzzle\Http\Client;
use Guzzle\Plugin\Cache\CachePlugin;
use Guzzle\Plugin\Cache\SkipRevalidation;
use Guzzle\Plugin\Cache\DefaultCacheStorage;
use Guzzle\Cache\CacheAdapterFactory;
use Zend\Cache\Storage\Adapter\Filesystem as FilesystemCache;
$client = new Client('http://api.example.com');
$storage = new FilesystemCache(['cacheDir' => 'some_path']);
$cachePlugin = new CachePlugin([
'storage' => new DefaultCacheStorage(CacheAdapterFactory::fromCache($storage)),
'revalidation' => new SkipRevalidation,
]);
$client->addSubscriber($cachePlugin);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment