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.
Cache::flexible() (introduced in Laravel 11), which enables stale-while-revalidate (SWR) patterns. This is critical for performance-sensitive applications where stale data is acceptable during regeneration (e.g., dashboards, analytics, or non-critical reads).Cache facade, making it a low-risk addition to existing Laravel 10 applications.composer require and a service provider registration (if not auto-discovered). No database migrations or schema changes.Cache::remember()/Cache::get() calls without breaking changes.| Risk Area | Assessment | Mitigation Strategy |
|---|---|---|
| Cache Driver Quirks | SWR may behave differently across drivers (e.g., Redis vs. database). | Test with primary cache driver; document driver-specific behaviors in release notes. |
| Race Conditions | Stale data served during regeneration could expose inconsistencies. | Validate with high-concurrency workloads; consider adding a ttl parameter for control. |
| Laravel 10 EOL | Laravel 10 reaches EOL in September 2025 (per Laravel Release Cycle). | Deprecation warning in package; recommend migration to Laravel 11+ post-EOL. |
| Performance Impact | SWR adds overhead for stale reads. | Benchmark against Cache::remember; provide guidance on when to use each method. |
Why Laravel 10?
Cache Strategy
Adoption Scope
Long-Term Plan
Cache::flexible() exists. Avoid duplication.Pre-Integration:
Cache::remember() calls to identify high-impact candidates for SWR (e.g., slow-to-generate data).Implementation:
composer require spatie/laravel-flexible-cache-polyfill
config/app.php.$data = Cache::flexible('key', 10, function () {
return expensiveOperation();
}, now()->addSeconds(5)); // Stale for 5s while regenerating
Phased Rollout:
Cache::flexible() calls in a feature flag for gradual enablement.slowlog).maxmemory-policy allkeys-lru or database cache with connection pooling.| Scenario | Impact | Mitigation |
|---|---|---|
| Cache driver failure | Stale data served indefinitely. | Fallback to Cache::remember with longer TTLs. |
| Race condition in regeneration | Inconsistent data. | Use atomic operations (e.g., Redis transactions) if critical. |
| Laravel 10 upgrade delay | Security/feature debt. | Prioritize upgrade; use polyfill as temporary crutch. |
| High cache miss ratio | Increased DB/regeneration load. | Optimize TTLs; consider pre-warming cache. |
Cache::flexible() usage for non-idempotent, slow operations.Cache::remember for key endpoints.How can I help you explore Laravel packages today?