Download the PHP package iksaku/laravel-swr-cache without Composer
On this page you can find all versions of the php package iksaku/laravel-swr-cache. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download iksaku/laravel-swr-cache
More information about iksaku/laravel-swr-cache
Files in iksaku/laravel-swr-cache
Package laravel-swr-cache
Short Description This is my package laravel-swr-cache
License MIT
Homepage https://github.com/iksaku/laravel-swr-cache
Informations about the package laravel-swr-cache
Laravel Cache Stale-While-Revalidate
There are applications out there that rely heavily on cache to improve performance,
and thanks to Laravel's cache()->remember()
method, we can easily cache the result
of a callback for a given Time-To-Live (TTL
).
However, there are cases when the callback may take a long time to execute, and we don't want to wait for it to finish before giving a response back to the user.
This is where the Stale-While-Revalidate pattern comes in handy. It allows us to return a cached result immediately, and then execute the callback in the background to update the cache for the next request.
How does SWR works under the hood?
Installation
You can install the package via composer:
Usage
The swr()
method is a wrapper around cache()->remember()
that adds support for
the Stale-While-Revalidate pattern using a new Time-To-Stale argument (TTS
).
You can access this method using the cache()
helper function:
Or using the Cache
facade:
Like the remember()
method, if the value is not available in cache,
the callback will be executed and the result will be cached for the
given Time-To-Live and the corresponding Time-To-Stale will also be stored.
If the value is available and the Time-To-Stale has not passed, the value is considered fresh and will be returned immediately. The callback will not be executed.
If the Time-To-Stale has passed, the value is considered stale, it will be returned immediately, and the callback will be executed after the response is sent to the user.
[!TIP] Mohamed Said has a great post on this. Check it out: Running a task after the response is sent.
Queueing the callback execution
If you prefer to queue the callback execution instead of running it after the
response is sent, you can use the queue
argument:
And, if you want to further customize the queued job, you can pass on a closure
that accepts a parameter of type Illuminate\Foundation\Bus\PendingClosureDispatch
:
Testing
Changelog
Please see CHANGELOG for more information on what has changed recently.
Contributing
Please see CONTRIBUTING for details.
Security Vulnerabilities
Please review our security policy on how to report security vulnerabilities.
Credits
- Jorge González
- All Contributors
License
The MIT License (MIT). Please see License File for more information.
All versions of laravel-swr-cache with dependencies
spatie/laravel-package-tools Version ^1.14.0
illuminate/contracts Version ^9.0|^10.0|^11.0