Download the PHP package eg-mohamed/laravel-http-client-cache without Composer
On this page you can find all versions of the php package eg-mohamed/laravel-http-client-cache. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download eg-mohamed/laravel-http-client-cache
More information about eg-mohamed/laravel-http-client-cache
Files in eg-mohamed/laravel-http-client-cache
Package laravel-http-client-cache
Short Description Opt-in response caching for Laravel's HTTP client.
License MIT
Homepage https://github.com/eg-mohamed/laravel-http-client-cache
Informations about the package laravel-http-client-cache
Opt-in response caching for Laravel's HTTP client. Cache outbound API responses with a single
fluent call — without replacing the Http facade or touching Laravel core.
The cache layer wraps the normal PendingRequest, so everything you already use keeps working:
Http::fake(), headers, query params, JSON payloads, retries, throwing, and the full
Response API (body(), json(), status(), header(), successful(), …).
Installation
The service provider is auto-discovered. Publish the config file if you want to tweak defaults:
Basic usage
Pass a cache key and a TTL (in seconds) to Http::cache(), then chain any normal HTTP client
method:
By default only successful GET requests are cached. The first request hits the network;
matching requests within the TTL are served from cache without sending another request.
Flexible TTL (stale-while-revalidate)
Pass an array [$fresh, $stale] to use Laravel's Cache::flexible(). Values are served fresh
for $fresh seconds, then served stale (and refreshed in the background) up to $stale:
A plain integer, DateInterval, or DateTimeInterface uses normal Cache::remember() behavior.
Caching read-only POST APIs
Some APIs expose read-only data over POST (reports, search, GraphQL). Opt those methods in
explicitly — string, array, and enum values are all accepted and normalized to uppercase:
Non-allowed methods simply bypass the cache and perform a normal request.
Checking fromCache()
Every response carries a fromCache() flag:
Cache store
Route the cached payload to a specific store:
Tags
Tags are applied only when the underlying store supports them (e.g. Redis). With a store that does not support tagging, tags are silently ignored instead of throwing:
Conditional caching
Decide per-response whether the result should be cached:
Status-based caching
Limit caching to specific status codes:
Disabling cache
Skip the cache for a single request with dontCache():
Or disable the package globally via config (enabled => false) — every request passes straight
through to the network.
Full fluent API
| Method | Purpose |
|---|---|
cache($key, $ttl = null, $methods = null) |
Start a cached request. |
cacheStore(?string $store) |
Choose the cache store. |
cacheTags(array\|string $tags) |
Tag entries (tag-capable stores only). |
cacheKeyPrefix(?string $prefix) |
Override the cache key prefix. |
cacheWhen(Closure $callback) |
Cache only when the callback returns true. |
cacheStatuses(array\|int\|null $statuses) |
Cache only these status codes. |
cacheMethods(array\|string\|UnitEnum $methods) |
Override cacheable methods. |
dontCache() |
Bypass the cache for this request. |
fromCache() (on the response) |
Whether the response was served from cache. |
Configuration
IDE autocompletion
Http::cache() and $response->fromCache() are registered as macros, so editors don't
autocomplete them out of the box. This package ships an ide-helper.php stub at its root that
PhpStorm indexes automatically from vendor/ — completion for Http::cache(...),
Factory/PendingRequest chaining, and Response::fromCache() works with no setup.
If you use barryvdh/laravel-ide-helper, the
macros are registered at boot and picked up automatically when you regenerate the helper:
Testing notes
Http::cache() wraps the real pending request, so Http::fake() keeps working. In tests, use
the array cache store and remember that a cache hit does not send another request:
Behavior summary
- Only
GETis cached by default; opt in to other methods explicitly. - Only successful (2xx) responses are cached by default; failed responses are not.
- Method names are normalized to uppercase; string, array, and enum values are accepted.
- Array TTL uses
Cache::flexible(); everything else usesCache::remember(). - Only a small serializable payload (body, status, reason, headers) is stored — never the full
Responseobject. - Tags are honored only on stores that support them.
- A cache hit never sends a second HTTP request.
License
The MIT License (MIT). Please see License File for more information.
All versions of laravel-http-client-cache with dependencies
spatie/laravel-package-tools Version ^1.16
illuminate/contracts Version ^11.0||^12.0||^13.0