kevinrob / guzzle-cache-middleware
A HTTP/1.1 Cache for Guzzle 6. It's a simple Middleware to be added in the HandlerStack. (RFC 7234)
Installs: 12 535 422
Dependents: 119
Suggesters: 7
Security: 0
Stars: 413
Watchers: 11
Forks: 77
Open Issues: 31
Requires
- php: >=8.1
- guzzlehttp/guzzle: ^7.9.2
- guzzlehttp/promises: ^2.0.3
- guzzlehttp/psr7: ^2.7.0
Requires (Dev)
- cache/array-adapter: ^0.4 || ^0.5 || ^1.0
- cache/simple-cache-bridge: ^0.1 || ^1.0
- doctrine/cache: ^1.10
- illuminate/cache: ^5.0
- league/flysystem: ^2.5
- phpunit/phpunit: ^9.6.21
- psr/cache: ^1.0
- symfony/cache: ^4.4 || ^5.0
- symfony/phpunit-bridge: ^7.1.4
Suggests
- doctrine/cache: This library has a lot of ready-to-use cache storage (to be used with Kevinrob\GuzzleCache\Storage\DoctrineCacheStorage). Use only versions >=1.4.0 < 2.0.0
- guzzlehttp/guzzle: For using this library. It was created for Guzzle6 (but you can use it with any PSR-7 HTTP client).
- laravel/framework: To be used with Kevinrob\GuzzleCache\Storage\LaravelCacheStorage
- league/flysystem: To be used with Kevinrob\GuzzleCache\Storage\FlysystemStorage
- psr/cache: To be used with Kevinrob\GuzzleCache\Storage\Psr6CacheStorage
- psr/simple-cache: To be used with Kevinrob\GuzzleCache\Storage\Psr16CacheStorage
- dev-master
- 6.0.0
- v5.1.0
- v5.0.0
- v4.1.2
- v4.1.1
- v4.1.0
- v4.0.2
- v4.0.1
- v4.0.0
- v3.5.0
- v3.4.1
- v3.4.0
- v3.3.1
- v3.3.0
- v3.2.1
- v3.2.0
- v3.1.0
- v3.0.0
- v2.1.1
- v2.1
- v2.0.2
- v2.0.1
- v2.0
- v1.5.2
- v1.5.1
- v1.5
- v1.4.3
- v1.4.2
- v1.4.1
- v1.4
- v1.3
- v1.2.2
- v1.2.1
- v1.2
- v1.1.2
- v1.1.1
- v1.1
- v1.0.1
- v1.0
- v0.8.1
- v0.8
- v0.7.1
- v0.7
- v0.6.2
- v0.6.1
- v0.6
- v0.5.3
- v0.5.2
- v0.5.1
- v0.5
- v0.4.2
- v0.4.1
- v0.4
- v0.3
- v0.2
- v0.1
This package is auto-updated.
Last update: 2024-11-28 09:55:04 UTC
README
A HTTP Cache for Guzzle 6+. It's a simple Middleware to be added in the HandlerStack.
Goals
- RFC 7234 compliance
- Performance and transparency
- Assured compatibility with PSR-7
Built-in storage interfaces
Installation
composer require kevinrob/guzzle-cache-middleware
or add it the your composer.json
and run composer update kevinrob/guzzle-cache-middleware
.
Why?
Performance. It's very common to do some HTTP calls to an API for rendering a page and it takes times to do it.
How?
With a simple Middleware added at the top of the HandlerStack
of Guzzle.
use GuzzleHttp\Client; use GuzzleHttp\HandlerStack; use Kevinrob\GuzzleCache\CacheMiddleware; // Create default HandlerStack $stack = HandlerStack::create(); // Add this middleware to the top with `push` $stack->push(new CacheMiddleware(), 'cache'); // Initialize the client with the handler option $client = new Client(['handler' => $stack]);
Examples
Doctrine/Cache
You can use a cache from Doctrine/Cache
:
[...] use Doctrine\Common\Cache\FilesystemCache; use Kevinrob\GuzzleCache\Strategy\PrivateCacheStrategy; use Kevinrob\GuzzleCache\Storage\DoctrineCacheStorage; [...] $stack->push( new CacheMiddleware( new PrivateCacheStrategy( new DoctrineCacheStorage( new FilesystemCache('/tmp/') ) ) ), 'cache' );
You can use ChainCache
for using multiple CacheProvider
instances. With that provider, you have to sort the different caches from the faster to the slower. Like that, you can have a very fast cache.
[...] use Doctrine\Common\Cache\ChainCache; use Doctrine\Common\Cache\ArrayCache; use Doctrine\Common\Cache\FilesystemCache; use Kevinrob\GuzzleCache\Strategy\PrivateCacheStrategy; use Kevinrob\GuzzleCache\Storage\DoctrineCacheStorage; [...] $stack->push(new CacheMiddleware( new PrivateCacheStrategy( new DoctrineCacheStorage( new ChainCache([ new ArrayCache(), new FilesystemCache('/tmp/'), ]) ) ) ), 'cache');
Laravel cache
You can use a cache with Laravel, e.g. Redis, Memcache etc.:
[...] use Illuminate\Support\Facades\Cache; use Kevinrob\GuzzleCache\Strategy\PrivateCacheStrategy; use Kevinrob\GuzzleCache\Storage\LaravelCacheStorage; [...] $stack->push( new CacheMiddleware( new PrivateCacheStrategy( new LaravelCacheStorage( Cache::store('redis') ) ) ), 'cache' );
Flysystem
[...] use League\Flysystem\Adapter\Local; use Kevinrob\GuzzleCache\Strategy\PrivateCacheStrategy; use Kevinrob\GuzzleCache\Storage\FlysystemStorage; [...] $stack->push( new CacheMiddleware( new PrivateCacheStrategy( new FlysystemStorage( new Local('/path/to/cache') ) ) ), 'cache' );
WordPress Object Cache
[...] use Kevinrob\GuzzleCache\Strategy\PrivateCacheStrategy; use Kevinrob\GuzzleCache\Storage\WordPressObjectCacheStorage; [...] $stack->push( new CacheMiddleware( new PrivateCacheStrategy( new WordPressObjectCacheStorage() ) ), 'cache' );
Public and shared
It's possible to add a public shared cache to the stack:
[...] use Doctrine\Common\Cache\FilesystemCache; use Doctrine\Common\Cache\PredisCache; use Kevinrob\GuzzleCache\Strategy\PrivateCacheStrategy; use Kevinrob\GuzzleCache\Strategy\PublicCacheStrategy; use Kevinrob\GuzzleCache\Storage\DoctrineCacheStorage; [...] // Private caching $stack->push( new CacheMiddleware( new PrivateCacheStrategy( new DoctrineCacheStorage( new FilesystemCache('/tmp/') ) ) ), 'private-cache' ); // Public caching $stack->push( new CacheMiddleware( new PublicCacheStrategy( new DoctrineCacheStorage( new PredisCache( new Predis\Client('tcp://10.0.0.1:6379') ) ) ) ), 'shared-cache' );
Greedy caching
In some cases servers might send insufficient or no caching headers at all. Using the greedy caching strategy allows defining an expiry TTL on your own while disregarding any possibly present caching headers:
[...] use Kevinrob\GuzzleCache\KeyValueHttpHeader; use Kevinrob\GuzzleCache\Strategy\GreedyCacheStrategy; use Kevinrob\GuzzleCache\Storage\DoctrineCacheStorage; use Doctrine\Common\Cache\FilesystemCache; [...] // Greedy caching $stack->push( new CacheMiddleware( new GreedyCacheStrategy( new DoctrineCacheStorage( new FilesystemCache('/tmp/') ), 1800, // the TTL in seconds new KeyValueHttpHeader(['Authorization']) // Optional - specify the headers that can change the cache key ) ), 'greedy-cache' );
Delegate caching
Because your client may call different apps, on different domains, you may need to define which strategy is suitable to your requests.
To solve this, all you have to do is to define a default cache strategy, and override it by implementing your own Request Matchers.
Here's an example:
namespace App\RequestMatcher; use Kevinrob\GuzzleCache\Strategy\Delegate\RequestMatcherInterface; use Psr\Http\Message\RequestInterface; class ExampleOrgRequestMatcher implements RequestMatcherInterface { /** * @inheritDoc */ public function matches(RequestInterface $request) { return false !== strpos($request->getUri()->getHost(), 'example.org'); } }
namespace App\RequestMatcher; use Kevinrob\GuzzleCache\Strategy\Delegate\RequestMatcherInterface; use Psr\Http\Message\RequestInterface; class TwitterRequestMatcher implements RequestMatcherInterface { /** * @inheritDoc */ public function matches(RequestInterface $request) { return false !== strpos($request->getUri()->getHost(), 'twitter.com'); } }
require_once __DIR__ . '/vendor/autoload.php'; use App\RequestMatcher\ExampleOrgRequestMatcher; use App\RequestMatcher\TwitterRequestMatcher; use GuzzleHttp\Client; use GuzzleHttp\HandlerStack; use Kevinrob\GuzzleCache\CacheMiddleware; use Kevinrob\GuzzleCache\Strategy; $strategy = new Strategy\Delegate\DelegatingCacheStrategy($defaultStrategy = new Strategy\NullCacheStrategy()); $strategy->registerRequestMatcher(new ExampleOrgRequestMatcher(), new Strategy\PublicCacheStrategy()); $strategy->registerRequestMatcher(new TwitterRequestMatcher(), new Strategy\PrivateCacheStrategy()); $stack = HandlerStack::create(); $stack->push(new CacheMiddleware($strategy)); $guzzle = new Client(['handler' => $stack]);
With this example:
- All requests to
example.org
will be handled byPublicCacheStrategy
- All requests to
twitter.com
will be handled byPrivateCacheStrategy
- All other requests won't be cached.
Drupal
See Guzzle Cache module.
Links that talk about the project
Buy me a coffee
If you like this project, you can buy me a coffee! (or a beer 😉)
Development
Docker quick start
Initialization
make init
Running test
make test
Entering container shell
make shell