Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Laravel Flexible Cache Polyfill Laravel Package

spatie/laravel-flexible-cache-polyfill

Polyfill for Laravel 10 that adds Cache::flexible() (introduced in Laravel 11). Implements stale-while-revalidate caching so expired values can be served immediately while recalculation runs in the background, avoiding slow responses.

View on GitHub
Deep Wiki
Context7

Polyfill for Cache::flexible() in Laravel 10 and 11

Latest Version on Packagist GitHub Tests Action Status GitHub Code Style Action Status Total Downloads

This package provides a polyfill for the Cache::flexible() method, which was introduced in Laravel 11. It brings the stale while revalidating cache pattern to Laravel 10.

When using Cache::remember, some users may experience slow response times if the cached value has expired. The flexible method allows stale data to be served while the cached value is recalculated in the background after the response is sent, preventing slow response times for your users.

Support us

We invest a lot of resources into creating best in class open source packages. You can support us by buying one of our paid products.

We highly appreciate you sending us a postcard from your hometown, mentioning which of our package(s) you are using. You'll find our address on our contact page. We publish all received postcards on our virtual postcard wall.

Installation

You can install the package via composer:

composer require spatie/laravel-flexible-cache-polyfill

Usage

Using the Cache::flexible() facade:

$value = Cache::flexible('users', [5, 10], function () {
    return DB::table('users')->get();
});

The first value (5) is the number of seconds the cache is considered "fresh". The second value (10) defines how long it can be served as stale data before recalculation is necessary.

  • 0-5 seconds: Returns cached value immediately
  • 5-10 seconds: Returns stale value, refreshes in background after response is sent
  • After 10 seconds: Cache expired, recalculates immediately

Support for all stores is available:

$value = Cache::store('redis-2')->flexible(...);

Testing

composer test

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

License

The MIT License (MIT). Please see License File for more information.

Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
davejamesmiller/laravel-breadcrumbs
artisanry/parsedown
christhompsontldr/phpsdk
enqueue/dsn
bunny/bunny
enqueue/test
enqueue/null
enqueue/amqp-tools
milesj/emojibase
bower-asset/punycode
bower-asset/inputmask
bower-asset/jquery
bower-asset/yii2-pjax
laravel/nova
spatie/laravel-mailcoach
spatie/laravel-superseeder
laravel/liferaft
nst/json-test-suite
danielmiessler/sec-lists
jackalope/jackalope-transport