Cache

Contents
Remember forever-ish
Forget keys
Tags note
Drivers note

Remember forever-ish

Remember runs the slow code once and stores the result under a key. Later calls read the stored value until the seconds run out.

Wrap reports, counters, and other slow reads the same way, with the Cache facade imported at the top.

use Illuminate\Support\Facades\Cache; $stats = Cache::remember("stats", 3600, function () { return Stats::build(); });

Forget keys

Cached values go stale when source data changes. Forget one key right after the matching update, so the next read rebuilds fresh data.

Flush wipes the whole store and fits deploys more than daily work, so prefer forget for single keys.

Cache::forget("stats"); Cache::flush();

Tags note

Tags group related keys so one call clears the whole group. Tag order stats with the orders tag, then flush only that tag after an order changes.

Not every driver supports tags, so confirm support before relying on them in code.

Cache::tags(["orders"])->remember("stats", 3600, function () { return Stats::build(); }); Cache::tags(["orders"])->flush();

Drivers note

The driver decides where cached values live. File suits small apps, array resets on every request and fits tests, while Redis and Memcached fit shared servers.

Pick the store in the env file and keep the code unchanged across drivers.

CACHE_STORE=file

Next: Sessions in Laravel

Article author: Arthur Isaev

Related articles
Development with PHP
What is Laravel and where it runs
Install Laravel
Your first route
Controllers
Blade basics
Blade advanced
Migrations
Eloquent models
Relationships
Query builder
Seeders and factories
Validation
Auth intro
Hand-rolled login
Middleware
Service container
Config and env
Artisan commands
Events and listeners
Queues
Mail
Cache
Sessions in Laravel
CSRF
Hashing
Sanctum tokens
API resources
Gates and policies
Notifications
Scheduler
Filesystem
Localization
Testing
HTTP tests
Collections
Carbon dates
Pagination
File uploads
Eloquent advanced
Transactions and locks
Deployment
Debugbar and dd
Broadcasting
Full-text search
Capstone: blog
Capstone: JSON API
Capstone: shop cart
Performance: kill N+1
Modern Laravel tour
Hero roadmap: the whole course on one page

Search this site

Channel @aofeed Chat @aofeedchat

Contacts and cooperation:
I recommend our hosting beget.com
Write to info@aredel.com if you:
1. Want to write an article for our site or translate an article into your native language.
2. Want to place thematically relevant ads on the site.
3. Ads on my site pass maximum censorship. If you see an ad block unsuitable for school-age children, shocking or misleading - please contact us by e-mail
4. Found a mistake, inaccuracy, bug, etc. on the site. ... .......
5. Articles can be shared on social media by clicking a network icon: