Reference / Laravel
Cache::flexible()
Stale-While-Revalidate caching pattern introduced in Laravel 11. Serves stale cached values immediately while refreshing data in background.
Syntax
Cache::flexible(string $key, array $durations, Closure $callback): mixedParameters
| Name | Type | Description |
|---|---|---|
| key | string | Cache key. |
| durations | array | [freshSeconds, staleSeconds] window. |
| callback | Closure | Fresh value calculation closure. |
Examples
Non-blocking cache refresh
$stats = Cache::flexible('leaderboard:global', [300, 3600], function () {
return DB::table('users')->orderByDesc('points')->limit(50)->get();
});Prevents cache stampede / dog-piling on high-traffic endpoints.