PHPackages                             spatie/laravel-flexible-cache-polyfill - 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. [Caching](/categories/caching)
4. /
5. spatie/laravel-flexible-cache-polyfill

ActiveLibrary[Caching](/categories/caching)

spatie/laravel-flexible-cache-polyfill
======================================

Polyfill for Cache::flexible() in Laravel 10

0.1.0(7mo ago)1197↓92.2%1MITPHPPHP ^8.1CI passing

Since Dec 2Pushed 7mo agoCompare

[ Source](https://github.com/spatie/laravel-flexible-cache-polyfill)[ Packagist](https://packagist.org/packages/spatie/laravel-flexible-cache-polyfill)[ Docs](https://github.com/spatie/laravel-flexible-cache-polyfill)[ GitHub Sponsors](https://github.com/Spatie)[ RSS](/packages/spatie-laravel-flexible-cache-polyfill/feed)WikiDiscussions main Synced today

READMEChangelog (1)Dependencies (13)Versions (2)Used By (0)

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

[](#polyfill-for-cacheflexible-in-laravel-10-and-11)

[![Latest Version on Packagist](https://camo.githubusercontent.com/735ef75c5bf199f5b907ccd6f7e472ea9e74f0c886336279ee40b2e58fa68775/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f7370617469652f6c61726176656c2d666c657869626c652d63616368652d706f6c7966696c6c2e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/spatie/laravel-flexible-cache-polyfill)[![GitHub Tests Action Status](https://camo.githubusercontent.com/528b599da55fb693856f6872ea2959696fee64d508b7ec3b67710537037ef72d/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f7370617469652f6c61726176656c2d666c657869626c652d63616368652d706f6c7966696c6c2f72756e2d74657374732e796d6c3f6272616e63683d6d61696e266c6162656c3d7465737473267374796c653d666c61742d737175617265)](https://github.com/spatie/laravel-flexible-cache-polyfill/actions?query=workflow%3Arun-tests+branch%3Amain)[![GitHub Code Style Action Status](https://camo.githubusercontent.com/81d2c6e2cfed25d88198fbc81c251933c7719e989df7a12db610a128c47c340e/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f616374696f6e732f776f726b666c6f772f7374617475732f7370617469652f6c61726176656c2d666c657869626c652d63616368652d706f6c7966696c6c2f6669782d7068702d636f64652d7374796c652d6973737565732e796d6c3f6272616e63683d6d61696e266c6162656c3d636f64652532307374796c65267374796c653d666c61742d737175617265)](https://github.com/spatie/laravel-flexible-cache-polyfill/actions?query=workflow%3A%22Fix+PHP+code+style+issues%22+branch%3Amain)[![Total Downloads](https://camo.githubusercontent.com/2646dd5d35ce13cc06b6362764136d8f1eb057c8de55bede07968928d083e2ea/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f7370617469652f6c61726176656c2d666c657869626c652d63616368652d706f6c7966696c6c2e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/spatie/laravel-flexible-cache-polyfill)

This package provides a polyfill for the `Cache::flexible()` method, which was introduced in Laravel 11. It brings the [stale while revalidating](https://laravel.com/docs/master/cache#swr) 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
----------

[](#support-us)

[![](https://camo.githubusercontent.com/933d24862e373a59f38ce18a40d7160ad25a10da09d1e568e312fed5b9ccf3c9/68747470733a2f2f6769746875622d6164732e73332e65752d63656e7472616c2d312e616d617a6f6e6177732e636f6d2f466c657869626c654361636865506f6c7966696c6c2e6a70673f743d31)](https://spatie.be/github-ad-click/FlexibleCachePolyfill)

We invest a lot of resources into creating [best in class open source packages](https://spatie.be/open-source). You can support us by [buying one of our paid products](https://spatie.be/open-source/support-us).

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](https://spatie.be/about-us). We publish all received postcards on [our virtual postcard wall](https://spatie.be/open-source/postcards).

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

[](#installation)

You can install the package via composer:

```
composer require spatie/laravel-flexible-cache-polyfill
```

Usage
-----

[](#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
-------

[](#testing)

```
composer test
```

Changelog
---------

[](#changelog)

Please see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently.

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

[](#contributing)

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

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

[](#security-vulnerabilities)

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

Credits
-------

[](#credits)

- [Alex Vanderbist](https://github.com/alexvanderbist)
- [All Contributors](../../contributors)

License
-------

[](#license)

The MIT License (MIT). Please see [License File](LICENSE.md) for more information.

###  Health Score

33

—

LowBetter than 72% of packages

Maintenance65

Regular maintenance activity

Popularity17

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity34

Early-stage or recently created project

 Bus Factor1

Top contributor holds 100% 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

Unknown

Total

1

Last Release

214d ago

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/7535935?v=4)[Spatie](/maintainers/spatie)[@spatie](https://github.com/spatie)

---

Top Contributors

[![AlexVanderbist](https://avatars.githubusercontent.com/u/6287961?v=4)](https://github.com/AlexVanderbist "AlexVanderbist (5 commits)")

---

Tags

polyfillspatielaravelcachestale-while-revalidateFlexible

###  Code Quality

TestsPest

Static AnalysisPHPStan

Code StyleLaravel Pint

### Embed Badge

![Health badge](/badges/spatie-laravel-flexible-cache-polyfill/health.svg)

```
[![Health](https://phpackages.com/badges/spatie-laravel-flexible-cache-polyfill/health.svg)](https://phpackages.com/packages/spatie-laravel-flexible-cache-polyfill)
```

###  Alternatives

[psalm/plugin-laravel

Psalm plugin for Laravel

3355.3M346](/packages/psalm-plugin-laravel)[laravel/horizon

Dashboard and code-driven configuration for Laravel queues.

4.2k95.4M307](/packages/laravel-horizon)[laravel/sail

Docker files for running a basic Laravel application.

1.9k205.7M1.3k](/packages/laravel-sail)[spatie/laravel-responsecache

Speed up a Laravel application by caching the entire response

2.8k9.0M69](/packages/spatie-laravel-responsecache)[illuminate/database

The Illuminate Database package.

2.8k54.9M11.7k](/packages/illuminate-database)[laravel/ai

The official AI SDK for Laravel.

1.0k3.2M203](/packages/laravel-ai)

PHPackages © 2026

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