{"id":20470911,"url":"https://github.com/brightfish-be/caching-guzzle","last_synced_at":"2025-04-13T10:57:05.980Z","repository":{"id":56951332,"uuid":"175029173","full_name":"brightfish-be/caching-guzzle","owner":"brightfish-be","description":"Cache HTTP responses through Guzzle middleware","archived":false,"fork":false,"pushed_at":"2024-05-09T09:17:38.000Z","size":88,"stargazers_count":10,"open_issues_count":1,"forks_count":4,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-03-27T02:11:34.586Z","etag":null,"topics":["cache","guzzle","http-cache","laravel","middleware"],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"gpl-3.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/brightfish-be.png","metadata":{"files":{"readme":"readme.md","changelog":null,"contributing":".github/CONTRIBUTING.md","funding":null,"license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":".github/SECURITY.md","support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2019-03-11T15:27:27.000Z","updated_at":"2024-05-31T01:18:56.000Z","dependencies_parsed_at":"2024-11-19T01:44:01.058Z","dependency_job_id":null,"html_url":"https://github.com/brightfish-be/caching-guzzle","commit_stats":{"total_commits":37,"total_committers":4,"mean_commits":9.25,"dds":0.2702702702702703,"last_synced_commit":"ff3d880a0a40a84472a4b856732f9f112f8d7a34"},"previous_names":[],"tags_count":15,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/brightfish-be%2Fcaching-guzzle","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/brightfish-be%2Fcaching-guzzle/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/brightfish-be%2Fcaching-guzzle/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/brightfish-be%2Fcaching-guzzle/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/brightfish-be","download_url":"https://codeload.github.com/brightfish-be/caching-guzzle/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248703195,"owners_count":21148117,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["cache","guzzle","http-cache","laravel","middleware"],"created_at":"2024-11-15T14:14:20.480Z","updated_at":"2025-04-13T10:57:05.959Z","avatar_url":"https://github.com/brightfish-be.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# HTTP caching middleware for Guzzle\n\n[![Tests](https://github.com/brightfish-be/caching-guzzle/workflows/Tests/badge.svg?event=push\u0026style=flat-square)](https://github.com/brightfish-be/caching-guzzle/actions)\n[![Latest Version on Packagist](https://img.shields.io/packagist/v/brightfish/caching-guzzle.svg?style=flat-square\u0026label=Version)](https://packagist.org/packages/brightfish/caching-guzzle)\n[![Downloads](https://img.shields.io/packagist/dt/brightfish/caching-guzzle?label=Downloads\u0026style=flat-square)](https://packagist.org/packages/brightfish/caching-guzzle)\n\nSimple and transparent HTTP response caching middleware for [Guzzle](https://github.com/guzzle/guzzle/), \nworks well with [Laravel](https://github.com/laravel) or with any caching library \nimplementing the [PSR-16 caching interface](https://www.php-fig.org/psr/psr-16/).  \n\n## Installation\nYou can install the library with Composer: ```composer require brightfish/caching-guzzle```\n\n## How to use\nThe registration of the middleware follows [Guzzle's documentation](http://docs.guzzlephp.org/en/stable/handlers-and-middleware.html#handlers):\n\n```php\n/** @var \\Psr\\SimpleCache\\CacheInterface $cache */\n$cache = app('cache')-\u003estore('database'); // Laravel example, but any PSR-16 cache will do\n\n$middleware = new \\Brightfish\\CachingGuzzle\\Middleware($cache);\n\n$stack = \\GuzzleHttp\\HandlerStack::create();\n\n$stack-\u003epush($middleware);\n\n$client = new \\GuzzleHttp\\Client([\n    'handler' =\u003e $stack,\n    'base_uri' =\u003e 'https://example.org/api/'\n]);\n```\n\n### Instantiation parameters\nNext to a PSR-16 compliant cache, the middleware takes two optional parameters: \n- `$ttl`, the default cache duration, which can be overridden by each request\n- `$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)).\n\n```php\n$middleware = new \\Brightfish\\CachingGuzzle\\Middleware($store, $ttl = 60, $log = true);\n```\n\n## Making requests\n\n**Available options:**   \n\n| Option | Type | Default | Description |\n|:-------|------|---------|:------------|\n|`cache` | bool | `true` | Completely disable the cache for this request |\n|`cache_anew` | bool | `false` | Bypass the cache and replace it with the new response |\n|`cache_ttl` | int | `60` | Cache duration in seconds, use `-1` to cache forever |\n|`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)) |\n\n### Example: cache the response for 90s (default: 60)\n```php\n$response_1 = $guzzleClient-\u003eget('resource', [\n    'cache_ttl' =\u003e 90\n]);\n```\n### Example: request anew and update the cache\n```php\n$response_3 = $guzzleClient-\u003epost('resource/84', [\n    'cache_anew' =\u003e true\n]);\n```\n### Example: disable caching\n```php\n$response_2 = $guzzleClient-\u003epost('resource/84', [\n    'cache' =\u003e false\n]);\n```\n### Example: cache forever with a custom key\n```php\n$response_4 = $guzzleClient-\u003epost('resource/84', [\n    'cache_key' =\u003e 'my-key',\n    'cache_ttl' =\u003e -1\n]);\n```\nIf `cache_ttl` is set to `0` the response will not be cached, but, contrary to `'cache' =\u003e false`, it may be retrieved from it.\n\n## Example: cache retrieval\n```php\n# Get response_1 from cache.\n$cached_response_1 = $cache-\u003eget('//example.org/api/resource');\n\n# Get response_4 from cache.\n$cached_response_4 = $cache-\u003eget('my-key');\n```\n\n## Using the wrapper\nInstead 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.\n\n```php\nuse Brightfish\\CachingGuzzle\\Client;\n\n/** @var \\Psr\\SimpleCache\\CacheInterface $store */\n$psrCompatibleCache = new Cache();\n\n$client = new Client($psrCompatibleCache, [\n    'cache_ttl' =\u003e 60,\t   // default cache duration\n    'cache_log' =\u003e false,  // log the cache hits\n    // Guzzle options:\n    'base_uri' =\u003e 'https://example.org/api/'\n    // ...\n]);\n```\n\n## Contributing\n\nPlease see [CONTRIBUTING](.github/CONTRIBUTING.md) for details.\n\n## Security Vulnerabilities\n\nPlease review [our security policy](../../security/policy) on how to report security vulnerabilities.\n\n## Credits\n\n- [dotburo](https://github.com/dotburo)\n- [Peter Forret](https://github.com/pforret)\n- [All Contributors](../../contributors)\n\n## License\n\nGNU General Public License (GPL). Please see the [license file](LICENSE.md) for more information.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbrightfish-be%2Fcaching-guzzle","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fbrightfish-be%2Fcaching-guzzle","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fbrightfish-be%2Fcaching-guzzle/lists"}