Laravel Cache Cheat Sheet: Quick Reference for Common Operations
This article provides a concise cheat sheet for Laravel's Cache system, detailing facade methods, helper functions, tagging, sections, store selection, and related events with ready-to-use code snippets, while noting version‑specific nuances such as the cache duration unit change in Laravel 5.8.
Overview
Laravel includes a powerful caching helper class that simplifies storing and retrieving data. The cheat sheet presents the most frequently used Cache facade methods and helper functions, making it easy for developers to implement caching quickly in their applications.
Basic Cache Facade Methods
cache(); // get cache instance, equivalent to Cache
Cache::put('key', 'value', $seconds); // store value for given seconds (seconds in Laravel 5.8, minutes in earlier versions)
Cache::put('key', 'value'); // store permanently (no expiration)
Cache::add('key', 'value', $seconds); // store only if key does not exist
Cache::forever('key', 'value'); // store forever
Cache::sear('key', function () { return 'value'; }); // retrieve or execute closure and store result
Cache::remember('key', $seconds, function () { return 'value'; }); // retrieve or execute closure and store result
Cache::rememberForever('key', function () { return 'value'; }); // retrieve or execute closure and store forever
Cache::forget('key'); // delete a specific key
Cache::has('key'); // check existence
Cache::get('key'); // retrieve value
Cache::get('key', 'default'); // retrieve with default fallback
Cache::get('key', function () { return 'default'; }); // retrieve with closure fallback
Cache::pull('key'); // retrieve and delete
Cache::flush(); // clear all cache entries
Cache::increment('key'); // increment numeric value
Cache::increment('key', $amount); // increment by amount
Cache::decrement('key'); // decrement numeric value
Cache::decrement('key', $amount); // decrement by amountTagging Support
Cache::tags('my-tag')->put('key', 'value', $seconds);
Cache::tags('my-tag')->has('key');
Cache::tags('my-tag')->get('key');
Cache::tags(['people', 'artists'])->put('John', $john, $seconds);
Cache::tags('my-tag')->forget('key');
Cache::tags('my-tag')->flush();
Cache::tags(['people', 'authors'])->flush();Cache Sections (Laravel 8+)
Cache::section('group')->put('key', $value);
Cache::section('group')->get('key');
Cache::section('group')->flush();Helper Functions
cache('key'); // retrieve value
cache(['key' => 'value'], $seconds); // store value
cache(['key' => 'value'], now()->addMinutes(10)); // store with Carbon expiration
cache()->remember('users', $seconds, function () { return User::all(); }); // remember pattern using helperSelecting a Specific Store
Cache::store('file')->get('foo'); // use file driver
Cache::store('redis')->put('name', 'Jack', 600); // store for 10 minutes in RedisCache Events
'Illuminate\Cache\Events\CacheHit' => ['App\Listeners\LogCacheHit'],
'Illuminate\Cache\Events\CacheMissed' => ['App\Listeners\LogCacheMissed'],
'Illuminate\Cache\Events\KeyForgotten' => ['App\Listeners\LogKeyForgotten'],
'Illuminate\Cache\Events\KeyWritten' => ['App\Listeners\LogKeyWritten'],The cheat sheet is intended as a quick‑reference guide; developers should still consult the official Laravel documentation for advanced usage and configuration details.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Laravel Tech Community
Specializing in Laravel development, we continuously publish fresh content and grow alongside the elegant, stable Laravel framework.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
