PHPackages                             brightfish/caching-guzzle - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [HTTP &amp; Networking](/categories/http)
4. /
5. brightfish/caching-guzzle

ActiveLibrary[HTTP &amp; Networking](/categories/http)

brightfish/caching-guzzle
=========================

Cache HTTP responses through Guzzle middleware

2.0.1(6mo ago)1031.5k↑25%4[1 issues](https://github.com/brightfish-be/caching-guzzle/issues)GPL-3.0-onlyPHPPHP ^8.0

Since Mar 11Pushed 6mo ago2 watchersCompare

[ Source](https://github.com/brightfish-be/caching-guzzle)[ Packagist](https://packagist.org/packages/brightfish/caching-guzzle)[ Docs](https://github.com/brightfish-be/caching-guzzle#readme)[ RSS](/packages/brightfish-caching-guzzle/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (5)Dependencies (4)Versions (15)Used By (0)

HTTP caching middleware for Guzzle
==================================

[](#http-caching-middleware-for-guzzle)

[![Tests](https://github.com/brightfish-be/caching-guzzle/workflows/Tests/badge.svg?event=push&style=flat-square)](https://github.com/brightfish-be/caching-guzzle/actions)[![Latest Version on Packagist](https://camo.githubusercontent.com/4fcca7a178bb20989479999c1267ff04f408cfe9c44428cc792b38f113577026/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f627269676874666973682f63616368696e672d67757a7a6c652e7376673f7374796c653d666c61742d737175617265266c6162656c3d56657273696f6e)](https://packagist.org/packages/brightfish/caching-guzzle)[![Downloads](https://camo.githubusercontent.com/3ca69aecfe937b821e8dcf45cc115313524cec902ebf35fc95bb2707d6db50b1/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f627269676874666973682f63616368696e672d67757a7a6c653f6c6162656c3d446f776e6c6f616473267374796c653d666c61742d737175617265)](https://packagist.org/packages/brightfish/caching-guzzle)

Simple and transparent HTTP response caching middleware for [Guzzle](https://github.com/guzzle/guzzle/), works well with [Laravel](https://github.com/laravel) or with any caching library implementing the [PSR-16 caching interface](https://www.php-fig.org/psr/psr-16/).

Installation
------------

[](#installation)

You can install the library with Composer: `composer require brightfish/caching-guzzle`

How to use
----------

[](#how-to-use)

The registration of the middleware follows [Guzzle's documentation](http://docs.guzzlephp.org/en/stable/handlers-and-middleware.html#handlers):

```
/** @var \Psr\SimpleCache\CacheInterface $cache */
$cache = app('cache')->store('database'); // Laravel example, but any PSR-16 cache will do

$middleware = new \Brightfish\CachingGuzzle\Middleware($cache);

$stack = \GuzzleHttp\HandlerStack::create();

$stack->push($middleware);

$client = new \GuzzleHttp\Client([
    'handler' => $stack,
    'base_uri' => 'https://example.org/api/'
]);
```

### Instantiation parameters

[](#instantiation-parameters)

Next to a PSR-16 compliant cache, the middleware takes two optional parameters:

- `$ttl`, the default cache duration, which can be overridden by each request
- `$log`, instructs the package to log cache hits Laravel's log or PHP's default `error_log` (see [source](https://github.com/brightfish-be/caching-guzzle/blob/c0e96ae157b4e17363eb76ee5996995fbf0bd4a5/src/Middleware.php#L168)).

```
$middleware = new \Brightfish\CachingGuzzle\Middleware($store, $ttl = 60, $log = true);
```

Making requests
---------------

[](#making-requests)

**Available options:**

OptionTypeDefaultDescription`cache`bool`true`Completely disable the cache for this request`cache_anew`bool`false`Bypass the cache and replace it with the new response`cache_ttl`int`60`Cache duration in seconds, use `-1` to cache forever`cache_key`string`true`Cache key to override the default one based on the request URI (see [Cache retrieval](https://github.com/brightfish-be/caching-guzzle#cache-retrieval))### Example: cache the response for 90s (default: 60)

[](#example-cache-the-response-for-90s-default-60)

```
$response_1 = $guzzleClient->get('resource', [
    'cache_ttl' => 90
]);
```

### Example: request anew and update the cache

[](#example-request-anew-and-update-the-cache)

```
$response_3 = $guzzleClient->post('resource/84', [
    'cache_anew' => true
]);
```

### Example: disable caching

[](#example-disable-caching)

```
$response_2 = $guzzleClient->post('resource/84', [
    'cache' => false
]);
```

### Example: cache forever with a custom key

[](#example-cache-forever-with-a-custom-key)

```
$response_4 = $guzzleClient->post('resource/84', [
    'cache_key' => 'my-key',
    'cache_ttl' => -1
]);
```

If `cache_ttl` is set to `0` the response will not be cached, but, contrary to `'cache' => false`, it may be retrieved from it.

Example: cache retrieval
------------------------

[](#example-cache-retrieval)

```
# Get response_1 from cache.
$cached_response_1 = $cache->get('//example.org/api/resource');

# Get response_4 from cache.
$cached_response_4 = $cache->get('my-key');
```

Using the wrapper
-----------------

[](#using-the-wrapper)

Instead of manually configuring Guzzle's client and the caching middleware, it is also possible to instantiate the `Client` class provided by this package. This way, the binding of the middleware is done for you.

```
use Brightfish\CachingGuzzle\Client;

/** @var \Psr\SimpleCache\CacheInterface $store */
$psrCompatibleCache = new Cache();

$client = new Client($psrCompatibleCache, [
    'cache_ttl' => 60,	   // default cache duration
    'cache_log' => false,  // log the cache hits
    // Guzzle options:
    'base_uri' => 'https://example.org/api/'
    // ...
]);
```

Contributing
------------

[](#contributing)

Please see [CONTRIBUTING](.github/CONTRIBUTING.md) for details.

Security Vulnerabilities
------------------------

[](#security-vulnerabilities)

Please review [our security policy](../../security/policy) on how to report security vulnerabilities.

Credits
-------

[](#credits)

- [dotburo](https://github.com/dotburo)
- [Peter Forret](https://github.com/pforret)
- [All Contributors](../../contributors)

License
-------

[](#license)

GNU General Public License (GPL). Please see the [license file](LICENSE.md) for more information.

###  Health Score

51

—

FairBetter than 96% of packages

Maintenance66

Regular maintenance activity

Popularity35

Limited adoption so far

Community16

Small or concentrated contributor base

Maturity73

Established project with proven stability

 Bus Factor1

Top contributor holds 66.7% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~187 days

Recently: every ~413 days

Total

14

Last Release

186d ago

Major Versions

1.3.3 → 2.0.02025-10-17

PHP version history (4 changes)1.0.0PHP &gt;=7.2

1.0.3PHP &gt;=7.1.2

1.3.1PHP ^7.1|^8.0

2.0.0PHP ^8.0

### Community

Maintainers

![](https://www.gravatar.com/avatar/4b8ed2843e909fe0239c457f77f851ae07cfe022efe7e48c50e3166bf70da4fd?d=identicon)[pecuchet](/maintainers/pecuchet)

![](https://www.gravatar.com/avatar/337ba8a7b95f42ae856defba4b85180291c8d12c33eb99011ccb9d58294e2be7?d=identicon)[brightfish](/maintainers/brightfish)

---

Top Contributors

[![pecuchet](https://avatars.githubusercontent.com/u/10818207?v=4)](https://github.com/pecuchet "pecuchet (20 commits)")[![pforret](https://avatars.githubusercontent.com/u/474312?v=4)](https://github.com/pforret "pforret (6 commits)")[![mitinsany](https://avatars.githubusercontent.com/u/9968346?v=4)](https://github.com/mitinsany "mitinsany (3 commits)")[![DennizSvens](https://avatars.githubusercontent.com/u/34778022?v=4)](https://github.com/DennizSvens "DennizSvens (1 commits)")

---

Tags

cacheguzzlehttp-cachelaravelmiddlewaremiddlewarelaravelGuzzlecachehttp-cache

###  Code Quality

TestsPHPUnit

Code StylePHP CS Fixer

### Embed Badge

![Health badge](/badges/brightfish-caching-guzzle/health.svg)

```
[![Health](https://phpackages.com/badges/brightfish-caching-guzzle/health.svg)](https://phpackages.com/packages/brightfish-caching-guzzle)
```

###  Alternatives

[kevinrob/guzzle-cache-middleware

A HTTP/1.1 Cache for Guzzle 6. It's a simple Middleware to be added in the HandlerStack. (RFC 7234)

43417.4M104](/packages/kevinrob-guzzle-cache-middleware)[hamburgscleanest/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 response from cache when rate limit is exceeded or always get cached value to spare your rate limits.

13033.4k1](/packages/hamburgscleanest-guzzle-advanced-throttle)[eljam/guzzle-jwt-middleware

A jwt authentication middleware for guzzle 6

28722.5k3](/packages/eljam-guzzle-jwt-middleware)[hamburgscleanest/laravel-guzzle-throttle

A Laravel wrapper for https://github.com/hamburgscleanest/guzzle-advanced-throttle.

7914.3k](/packages/hamburgscleanest-laravel-guzzle-throttle)[behamin/service-proxy

for proxy or sending requests to other services with useful utilities

102.2k](/packages/behamin-service-proxy)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
