gregurco / guzzle-bundle-cache-plugin
Cache Plugin for Guzzle Bundle, a PHP HTTP client library and framework for building RESTful web service clients
Installs: 265 038
Dependents: 1
Suggesters: 0
Security: 0
Stars: 11
Watchers: 3
Forks: 8
Open Issues: 0
Requires
- php: >=7.2
- eightpoints/guzzle-bundle: ~8.0
- guzzlehttp/guzzle: ^6.5.8|^7.4.5
- kevinrob/guzzle-cache-middleware: ~4.0|~5.0
- symfony/config: ~5.0|~6.0|~7.0
- symfony/dependency-injection: ~5.0|~6.0|~7.0
- symfony/expression-language: ~5.0|~6.0|~7.0
- symfony/http-kernel: ~5.0|~6.0|~7.0
Requires (Dev)
- php-coveralls/php-coveralls: ~2.2
- symfony/phpunit-bridge: ~5.0|~6.0|~7.0
README
This plugin integrates cache functionality into Guzzle Bundle, a bundle for building RESTful web service clients.
Requirements
- PHP 7.2 or above
- Guzzle Bundle
- Guzzle Cache middleware
Installation
Using composer:
composer.json
{ "require": { "gregurco/guzzle-bundle-cache-plugin": "dev-master" } }
command line
$ composer require gregurco/guzzle-bundle-cache-plugin
Usage
Enable bundle
Symfony 2.x and 3.x
Plugin will be activated/connected through bundle constructor in app/AppKernel.php
, like this:
new EightPoints\Bundle\GuzzleBundle\EightPointsGuzzleBundle([ new Gregurco\Bundle\GuzzleBundleCachePlugin\GuzzleBundleCachePlugin(), ])
Symfony 4
The registration of bundles was changed in Symfony 4 and now you have to change src/Kernel.php
to achieve the same functionality.
Find next lines:
foreach ($contents as $class => $envs) { if (isset($envs['all']) || isset($envs[$this->environment])) { yield new $class(); } }
and replace them by:
foreach ($contents as $class => $envs) { if (isset($envs['all']) || isset($envs[$this->environment])) { if ($class === \EightPoints\Bundle\GuzzleBundle\EightPointsGuzzleBundle::class) { yield new $class([ new \Gregurco\Bundle\GuzzleBundleCachePlugin\GuzzleBundleCachePlugin(), ]); } else { yield new $class(); } } }
Basic configuration
# app/config/config.yml eight_points_guzzle: clients: api_payment: base_url: "http://api.domain.tld" # define headers, options # plugin settings plugin: cache: enabled: true
Configuration with specific cache strategy
# app/config/services.yml services: acme.filesystem_cache: class: Doctrine\Common\Cache\FilesystemCache arguments: ['/tmp/'] public: false acme.doctrine_cache_storage: class: Kevinrob\GuzzleCache\Storage\DoctrineCacheStorage arguments: ['@acme.filesystem_cache'] public: false acme.private_cache_strategy: class: Kevinrob\GuzzleCache\Strategy\PrivateCacheStrategy arguments: ['@acme.doctrine_cache_storage'] public: false
# app/config/config.yml eight_points_guzzle: clients: api_payment: plugin: cache: enabled: true strategy: "acme.private_cache_strategy"
More information about cache strategies can be found here: Kevinrob/guzzle-cache-middleware
Invalidate cache
# get client $apiPaymentClient = $this->get('eight_points_guzzle.client.api_payment'); # do a request $apiPaymentClient->request('GET', 'ping'); # invalidate cache $event = new InvalidateRequestEvent($apiPaymentClient, 'GET', 'ping'); $this->get('event_dispatcher')->dispatch(GuzzleBundleCacheEvents::INVALIDATE, $event);
License
This middleware is licensed under the MIT License - see the LICENSE file for details