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

Overview

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

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

You might also like...
Simple cache abstraction layer implementing PSR-16

sabre/cache This repository is a simple abstraction layer for key-value caches. It implements PSR-16. If you need a super-simple way to support PSR-16

PSR-6 cache implementation adapting a given PSR-16 instance

PSR-6 cache implementation adapting PSR-16 This package provides a PSR-6 cache instance when you only have a PSR-16 cache at hand. As PSR-6 is more fe

More Than Just a Cache: Redis Data Structures
More Than Just a Cache: Redis Data Structures

More Than Just a Cache: Redis Data Structures Redis is a popular key-value store, commonly used as a cache or message broker service. However, it can

Simple cache

Simple cache

Elephant - a highly performant PHP Cache Driver for Kirby 3

🐘 Kirby3 PHP Cache-Driver Elephant - a highly performant PHP Cache Driver for Kirby 3 Commerical Usage Support open source! This plugin is free but i

An improved helper for working with cache

Laravel Cache Installation To get the latest version of Laravel Cache, simply require the project using Composer: $ composer require dragon-code/larav

Zend Framework cache backend for MongoDB

Zend_Cache_Backend_Mongo Author: Anton Stöckl About Zend_Cache_Backend_Mongo is a Zend Framework Cache Backend for MongoDB. It supports tags and autoc

PHP local cache

__ ____ _________ ______/ /_ ___ / __ \/ ___/ __ `/ ___/ __ \/ _ \ / /_/ / /__/ /_/ / /__/ / / / __/ / ._

A fast, lock-free, shared memory user data cache for PHP

Yac is a shared and lockless memory user data cache for PHP.

Comments
  • Add PHP8 Support

    Add PHP8 Support

    This PR will allow PHP 8 compatiblity. Additionally, the deprecated/removed PhpUnit method assertInternalType was fixed and Psr7\Utils::uriFor() is used instead of the deprecated/removed Psr7\uri_for().

    opened by dsentker24 5
  • Refactor invalidate request

    Refactor invalidate request

    What I did:

    • use default event_dispatcher
    • replace logic from event to subscriber
    • only one subscriber per all client
    • add example in readme

    Todo:

    • [x] tests

    @Neirda24 please review changes and write your opinion please. I also added an example in readme.

    enhancement 
    opened by gregurco 3
  • Support for eightpoints/guzzle-bundle 8.x

    Support for eightpoints/guzzle-bundle 8.x

    This bundle requires eightpoints/guzzle-bundle ~7.0. As of Jan 20th 2020 version 8.0 of eightpoints/guzzle-bundle came out.

    Full error from composer when trying to bump to eightpoints/guzzle-bundle 8.x:

    gregurco/guzzle-bundle-cache-plugin v1.0.4 requires eightpoints/guzzle-bundle ~7.0 -> satisfiable by eightpoints/guzzle-bundle[7.3.0, v7.0.0, v7.1.0, v7.1.1, v7.2.0, v7.2.1, v7.3.1, v7.4.0, v7.4.1, v7.5.0, v7.6.0, v7.6.1, v7.x-dev] but these conflict with your requirements or minimum-stability.

    opened by petski 2
  • how to set cache expire time

    how to set cache expire time

    If I only want the cache to be valid within 5 minutes, and I need to request it again for the rest of the time, how can I set the effective time of the cache?Hope for help, thanks

    opened by licolas94 2
Releases(v2.1.0)
Owner
Vlad Gregurco
Software Architect and Open Source Contributor
Vlad Gregurco
A thin PSR-6 cache wrapper with a generic interface to various caching backends emphasising cache tagging and indexing.

Apix Cache, cache-tagging for PHP Apix Cache is a generic and thin cache wrapper with a PSR-6 interface to various caching backends and emphasising ca

Apix 111 Nov 26, 2022
Symfony2 Bundle for Doctrine Cache

DoctrineCacheBundle Symfony Bundle for Doctrine Cache. Master: Master: Deprecation warning This bundle is abandoned and will no longer be updated. If

Doctrine 2.8k Dec 9, 2022
A high-performance backend cache system. It is intended for use in speeding up dynamic web applications by alleviating database load.

A high-performance backend cache system. It is intended for use in speeding up dynamic web applications by alleviating database load. Well implemented, it can drops the database load to almost nothing, yielding faster page load times for users, better resource utilization. It is simple yet powerful.

PHPSocialNetwork 2.3k Dec 30, 2022
The place to keep your cache.

Stash - A PHP Caching Library Stash makes it easy to speed up your code by caching the results of expensive functions or code. Certain actions, like d

Tedious Developments 944 Jan 4, 2023
PHP cache library, with adapters for e.g. Memcached, Redis, Couchbase, APC(u), SQL and additional capabilities (e.g. transactions, stampede protection) built on top.

Donate/Support: Documentation: https://www.scrapbook.cash - API reference: https://docs.scrapbook.cash Table of contents Installation & usage Adapters

Matthias Mullie 295 Nov 28, 2022
Cache slam defense using a semaphore to prevent dogpile effect.

metaphore PHP cache slam defense using a semaphore to prevent dogpile effect (aka clobbering updates, stampending herd or Slashdot effect). Problem: t

Przemek Sobstel 102 Sep 28, 2022
:zap: Simple Cache Abstraction Layer for PHP

⚡ Simple Cache Class This is a simple Cache Abstraction Layer for PHP >= 7.0 that provides a simple interaction with your cache-server. You can define

Lars Moelleken 27 Dec 8, 2022
Doctrine Cache component

Doctrine Cache Cache component extracted from the Doctrine Common project. Documentation This library is deprecated and will no longer receive bug fix

Doctrine 7.6k Jan 3, 2023
LRU Cache implementation in PHP

PHP LRU Cache implementation Intro WTF is a LRU Cache? LRU stands for Least Recently Used. It's a type of cache that usually has a fixed capacity and

Rogério Vicente 61 Jun 23, 2022