Mastering Laravel Cache Optimization for Blazing-Fast Performance
What Is Caching?
Caching stores frequently used data temporarily so your app doesnโt hit the database every time.
Laravel makes this effortless using a unified Cache API.
// Store data in cache
Cache::put('site_name', 'SuriSnippet', 3600); // 1 hour
// Retrieve cached data
$site = Cache::get('site_name', 'DefaultSite');
Supported Cache Drivers
Laravel supports multiple cache backends. Configure yours in .env:
CACHE_DRIVER=file
# Other options: redis, memcached, database, array
Code Example (config/cache.php excerpt):
'default' => env('CACHE_DRIVER', 'file'),
Cache Query Results
You can store query results to reduce repeated database hits.
$users = Cache::remember('active_users', 3600, function () {
return User::where('status', 'active')->get();
});
Clear Query Cache
If data updates, clear the cache to avoid showing old results.
Cache::forget('active_users');
Cache Tags
Cache tags allow grouping of related cache items โ clear them all at once.
Cache::tags(['users', 'authors'])->put('author_1', $author, 3600);
Cache::tags(['users'])->flush(); // Clears all user-related cache
Cache Forever (No Expiration)
Sometimes you want to cache static data permanently until manual clearing.
Cache::forever('country_list', Country::all());
Conditional Cache Refresh
Use conditionals to refresh cache only when needed.
if (!Cache::has('site_settings')) {
Cache::put('site_settings', SiteSetting::all(), 86400);
}
View Cache
Compiled Blade templates are automatically cached. You can manually clear them:
php artisan view:clear
php artisan view:cache
Route Cache
Cache route definitions for faster route registration.
php artisan route:cache
php artisan route:clear
Config Cache
Cache your configuration files to avoid re-reading them on each request.
php artisan config:cache
php artisan config:clear
Why Redis?
Redis is an in-memory key-value store that makes cache operations lightning-fast.
.env Example:
CACHE_DRIVER=redis
REDIS_HOST=127.0.0.1
REDIS_PASSWORD=null
REDIS_PORT=6379
Usage:
Cache::store('redis')->put('snippet_count', 120, 600);
Flush Redis Cache
Explanation:
Clear Redis cache using Artisan or Redis CLI.
php artisan cache:clear
# or
redis-cli FLUSHALL
Cache Scheduling
Use Laravelโs Scheduler to refresh cache daily.
Code Example (app/Console/Kernel.php):
protected function schedule(Schedule $schedule)
{
$schedule->command('cache:clear')->dailyAt('03:00');
}
Deployment Cache Clearing
Always clear caches after deployment to avoid mismatched config or routes.
php artisan optimize:clear
Cache Wisely
Donโt cache everything โ only data that changes infrequently and is expensive to query.
Monitor Cache Size
Large cache files can slow down performance.
Use Cache::flush() occasionally or automate cleanup.
โ Result:
After completing this snippet, your Laravel app will load 2โ5ร faster, your database will stay light, and your users will enjoy instant page responses โ the professional standard Envato reviewers look for.
English
Dutch