All Projects → Kevinrob → Guzzle Cache Middleware

Kevinrob / Guzzle Cache Middleware

Licence: mit
A HTTP Cache for Guzzle 6. It's a simple Middleware to be added in the HandlerStack.

Projects that are alternatives of or similar to Guzzle Cache Middleware

Guzzle Advanced Throttle
A Guzzle middleware that can throttle requests according to (multiple) defined rules. It is also possible to define a caching strategy, e.g. get the response from cache when the rate limit is exceeded or always get a cached value to spare your rate limits. Using wildcards in host names is also supported.
Stars: ✭ 120 (-63.08%)
Mutual labels:  middleware, cache, guzzle
Strapi Middleware Cache
🔌 A cache middleware for https://strapi.io
Stars: ✭ 146 (-55.08%)
Mutual labels:  middleware, cache
Gin
Gin is a HTTP web framework written in Go (Golang). It features a Martini-like API with much better performance -- up to 40 times faster. If you need smashing performance, get yourself some Gin.
Stars: ✭ 53,971 (+16506.46%)
Mutual labels:  middleware, performance
Rayo.js
Micro framework for Node.js
Stars: ✭ 170 (-47.69%)
Mutual labels:  middleware, performance
Cetus
Cetus is a high performance middleware that provides transparent routing between your application and any backend MySQL Servers.
Stars: ✭ 1,199 (+268.92%)
Mutual labels:  middleware, performance
Guzzle retry middleware
Middleware for Guzzle v6+ that automatically retries HTTP requests on 429, 503 responses.
Stars: ✭ 90 (-72.31%)
Mutual labels:  middleware, guzzle
Foxify
The fast, easy to use & typescript ready web framework for Node.js
Stars: ✭ 138 (-57.54%)
Mutual labels:  middleware, performance
Home
Project Glimpse: Node Edition - Spend less time debugging and more time developing.
Stars: ✭ 260 (-20%)
Mutual labels:  middleware, performance
express-view-cache
Unobtrusive solution to express framework - cache rendered page, without database requests and rendering.
Stars: ✭ 20 (-93.85%)
Mutual labels:  middleware, cache
AkamaiOPEN-edgegrid-php-client
PHP client library for Akamai {OPEN} EdgeGrid Authentication scheme (based on Guzzle)
Stars: ✭ 38 (-88.31%)
Mutual labels:  middleware, guzzle
Guzzle Cache Middleware
A Guzzle Cache middleware
Stars: ✭ 50 (-84.62%)
Mutual labels:  middleware, cache
Ristretto
A high performance memory-bound Go cache
Stars: ✭ 3,584 (+1002.77%)
Mutual labels:  cache, performance
Htmlcache
Laravel middleware to cache the rendered html
Stars: ✭ 35 (-89.23%)
Mutual labels:  middleware, cache
Dotweb
Simple and easy go web micro framework
Stars: ✭ 1,354 (+316.62%)
Mutual labels:  middleware, cache
Apicache
Simple API-caching middleware for Express/Node.
Stars: ✭ 957 (+194.46%)
Mutual labels:  middleware, cache
Outputcache
Cache api responses using Redis, Memcached or any cache provider for NodeJS
Stars: ✭ 9 (-97.23%)
Mutual labels:  middleware, cache
Miox
Modern infrastructure of complex SPA
Stars: ✭ 374 (+15.08%)
Mutual labels:  middleware, cache
Iris
The fastest HTTP/2 Go Web Framework. AWS Lambda, gRPC, MVC, Unique Router, Websockets, Sessions, Test suite, Dependency Injection and more. A true successor of expressjs and laravel | 谢谢 https://github.com/kataras/iris/issues/1329 |
Stars: ✭ 21,587 (+6542.15%)
Mutual labels:  middleware, performance
Http Cache
High performance Golang HTTP middleware for server-side application layer caching, ideal for REST APIs
Stars: ✭ 184 (-43.38%)
Mutual labels:  middleware, cache
Doctrinecachebundle
Symfony2 Bundle for Doctrine Cache
Stars: ✭ 2,813 (+765.54%)
Mutual labels:  doctrine, cache

guzzle-cache-middleware

Latest Stable Version Total Downloads License Tests Scrutinizer Code Quality SensioLabsInsight Code Coverage

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 Guzzle6.

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 by PublicCacheStrategy
  • All requests to twitter.com will be handled by PrivateCacheStrategy
  • All other requests won't be cached.

Drupal

See Guzzle Cache module.

Links that talk about the project

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].