Skip to content

gregurco/GuzzleBundleCachePlugin

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

12 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Guzzle Bundle Cache Plugin

Build Status Coverage Status SensioLabsInsight

This plugin integrates cache functionality into Guzzle Bundle, a bundle for building RESTful web service clients.

Requirements

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

# app/AppKernel.php

new EightPoints\Bundle\GuzzleBundle\EightPointsGuzzleBundle([
    new Gregurco\Bundle\GuzzleBundleCachePlugin\GuzzleBundleCachePlugin(),
])

Basic configuration

# app/config/config.yml

eight_points_guzzle:
    clients:
        api_payment:
            base_url: "https://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