https://github.com/gregurco/guzzlebundlecacheplugin
Cache Plugin for EightPointsGuzzleBundle
https://github.com/gregurco/guzzlebundlecacheplugin
bundle cache guzzle guzzle-bundle-plugin guzzle-middleware php symfony symfony-bundle
Last synced: 17 days ago
JSON representation
Cache Plugin for EightPointsGuzzleBundle
- Host: GitHub
- URL: https://github.com/gregurco/guzzlebundlecacheplugin
- Owner: gregurco
- License: mit
- Created: 2017-10-24T09:40:12.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2024-03-30T19:36:47.000Z (about 1 year ago)
- Last Synced: 2025-04-13T05:08:35.260Z (17 days ago)
- Topics: bundle, cache, guzzle, guzzle-bundle-plugin, guzzle-middleware, php, symfony, symfony-bundle
- Language: PHP
- Homepage:
- Size: 35.2 KB
- Stars: 11
- Watchers: 2
- Forks: 7
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Guzzle Bundle Cache Plugin
[](https://travis-ci.org/gregurco/GuzzleBundleCachePlugin)
[](https://coveralls.io/r/gregurco/GuzzleBundleCachePlugin)
[](https://insight.sensiolabs.com/projects/08400bb3-540d-4616-b0b3-f694a73a72cf)This plugin integrates cache functionality into Guzzle Bundle, a bundle for building RESTful web service clients.
## Requirements
- PHP 7.2 or above
- [Guzzle Bundle][1]
- [Guzzle Cache middleware][2]
## Installation
Using [composer][3]:##### composer.json
``` json
{
"require": {
"gregurco/guzzle-bundle-cache-plugin": "dev-master"
}
}
```##### command line
``` bash
$ 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:``` php
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:```php
foreach ($contents as $class => $envs) {
if (isset($envs['all']) || isset($envs[$this->environment])) {
yield new $class();
}
}
```and replace them by:
```php
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
``` yaml
# app/config/config.ymleight_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
``` yaml
# app/config/services.ymlservices:
acme.filesystem_cache:
class: Doctrine\Common\Cache\FilesystemCache
arguments: ['/tmp/']
public: falseacme.doctrine_cache_storage:
class: Kevinrob\GuzzleCache\Storage\DoctrineCacheStorage
arguments: ['@acme.filesystem_cache']
public: falseacme.private_cache_strategy:
class: Kevinrob\GuzzleCache\Strategy\PrivateCacheStrategy
arguments: ['@acme.doctrine_cache_storage']
public: false```
``` yaml
# app/config/config.ymleight_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][2]
### Invalidate cache
```php
# 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[1]: https://github.com/8p/EightPointsGuzzleBundle
[2]: https://github.com/Kevinrob/guzzle-cache-middleware
[3]: https://getcomposer.org/