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 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.

Frequently asked questions about Laravel Flexible Cache Polyfill
Does this package work with Laravel 10.x, or is it only for Laravel 11?
This package is specifically designed for Laravel 10.x. It polyfills the `Cache::flexible()` method, which was introduced in Laravel 11, allowing you to use stale-while-revalidate (SWR) caching patterns in Laravel 10. Laravel 11+ already has native support, so this package is redundant there.
How do I install and set up this package in my Laravel 10 project?
Installation is straightforward: run `composer require spatie/laravel-flexible-cache-polyfill`. The package is auto-discovered in Laravel 10, so no additional configuration is needed unless you’re using a custom service provider setup. After installation, you can immediately use `Cache::flexible()` in your code.
What cache drivers does this package support?
The package supports all Laravel cache drivers, including Redis, Memcached, database, and file drivers. However, the file driver may have edge cases due to locking mechanisms. Always test with your primary cache driver to ensure expected behavior, especially for high-concurrency scenarios.
How does `Cache::flexible()` differ from `Cache::remember()` in Laravel 10?
`Cache::flexible()` implements the stale-while-revalidate (SWR) pattern, serving stale data immediately while regenerating it in the background. This avoids slow response times when the cache expires. In contrast, `Cache::remember()` waits for the cache to regenerate before returning, which can cause delays if the operation is slow.
Can I use this package alongside existing `Cache::remember()` calls without breaking anything?
Yes, this package is fully backward-compatible. You can use `Cache::flexible()` for new SWR patterns while keeping existing `Cache::remember()` calls unchanged. There are no breaking changes or conflicts with other cache methods.
What happens if my Laravel 10 application reaches EOL in September 2025? Will this package stop working?
The package includes a deprecation warning to remind you of Laravel 10’s EOL. After September 2025, you should migrate to Laravel 11+ for continued security and feature support. The polyfill will remain functional until then, but Spatie may discontinue updates post-EOL.
Is there a performance overhead when using `Cache::flexible()` compared to `Cache::remember()`?
Yes, `Cache::flexible()` adds slight overhead because it serves stale data while regenerating in the background. Benchmark your use case to compare response times. For non-critical reads (e.g., dashboards), the performance gain from avoiding slow regenerations often outweighs the overhead.
Can I control how stale the data can be before regeneration completes?
Yes, you can specify a `ttl` (time-to-live) parameter for the stale data window. For example, `Cache::flexible('key', 10, function () { ... }, now()->addSeconds(5))` serves stale data for 5 seconds while regenerating. Adjust this based on your tolerance for stale data.
Are there any known issues with race conditions when using `Cache::flexible()`?
Race conditions can occur if multiple requests trigger regeneration simultaneously. Test under high concurrency to validate behavior. For critical applications, consider adding explicit locking or using a shorter stale TTL to minimize inconsistencies.
What alternatives exist if I don’t want to use this polyfill?
Alternatives include custom cache tagging systems, Redis Lua scripts for atomic SWR logic, or upgrading to Laravel 11+ for native `Cache::flexible()` support. However, these require more setup and may not integrate as seamlessly with Laravel’s cache facade.
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