Caching
Some pieces of work are expensive to redo on every single request — a slow, complex database query, a call out to a third-party API, a calculation that crunches a lot of data. Caching is simply the practice of running that expensive work once, storing its result somewhere fast, and reusing the stored result for subsequent requests instead of paying the same cost again — until it's time to refresh it.
Cache Drivers
Laravel doesn't hardcode where cached data actually lives — that's controlled by a
driver, configured in config/cache.php, and every driver is used
through the exact same Cache facade API, so switching drivers later means changing
configuration, not application code:
| Driver | Where it stores cached data |
|---|---|
file | Plain files on disk — simple, zero setup, and Laravel's default |
database | A dedicated table in your existing database |
redis / memcached | A fast, in-memory data store — the typical choice for production, since memory is far faster than disk or a database round-trip |
The Cache Facade: put, get, forget
The most basic operations are a small, easy-to-remember set:
<?php
use Illuminate\Support\Facades\Cache;
Cache::put('homepage-visits', 1204, 600); // store for 600 seconds
$visits = Cache::get('homepage-visits'); // read it back, or null if expired/missing
Cache::forget('homepage-visits'); // remove it before it would naturally expire
The remember() Pattern
Manually checking whether a value is cached, and only computing it if not, is such a common pattern
that Laravel wraps it up in a single method: Cache::remember(). It checks the cache
first — if the key already has a value, that value is returned immediately and the closure never
runs at all; only if the key is not already cached does it run the closure, store whatever
it returns under that key, and then return it:
<?php
$popular = Cache::remember('popular-articles', 3600, function () {
return Article::where('published', true)
->orderBy('views', 'desc')
->limit(10)
->get();
});
Here, the query to find the ten most-viewed published articles only actually runs against the
database once every 3,600 seconds (one hour) — the second argument. Every request in between reuses
the cached Collection straight from the cache store, at a fraction of the cost of
re-running the query. This one method — check, compute-if-missing, store, return — covers the vast
majority of real-world caching needs.
Cache Tags: Grouping Related Entries
Sometimes several cache keys are logically related, and you want to clear all of them together rather than remembering every individual key name. Cache tags let you group entries under one or more labels and flush a whole group in one call:
<?php
Cache::tags(['articles'])->put('popular-articles', $articles, 3600);
// later, clear everything tagged "articles" without knowing every key name
Cache::tags(['articles'])->flush();
file and database drivers do not. Keep this in mind before designing around tags in a project not yet running Redis in production.When Not to Cache
Caching is not a universal win, and reaching for it everywhere causes its own class of bugs. Data that changes frequently — a live stock ticker, an inbox unread count, anything the user expects to reflect their most recent action instantly — is a poor caching candidate, since a stale cached value can actively mislead or confuse whoever is looking at it. A safer default is to cache things that are genuinely expensive to compute and where being a little out of date for a few minutes is truly harmless — a "popular articles" list is a good fit; a user's own account balance is not. When in doubt, it's worth asking plainly: would this user be upset to learn the number on their screen right now isn't quite current?