Laravel & Backend6 min read

Laravel Caching Strategies to Improve Application Speed

Md Shahed Alam
Md Shahed Alam
March 20, 2025
Laravel Caching Strategies to Improve Application Speed

I once looked at a client's app and found it was running the same query — "get all product categories" — on every single page load. The categories never changed. The query ran thousands of times a day for no reason.

One line of caching cut their database load by 40%.

Caching is storing the result of something expensive so you do not have to do it again. It is one of the fastest wins you can get in a Laravel app.

The simplest cache operation

php
// Store something for 60 minutes
Cache::put('featured-products', $products, 3600);

// Get it back
$products = Cache::get('featured-products');

// The pattern I use most often:
$categories = Cache::remember('all-categories', 3600, function () {
    return Category::with('children')->orderBy('name')->get();
});

Cache::remember is the one you will use constantly. It checks if the key exists. If yes, it returns the cached value. If no, it runs the callback, stores the result, and returns it. Clean and simple.

Which driver to use

For development, the file driver is fine. For production, use Redis. It is much faster because it stores everything in memory.

text
CACHE_DRIVER=redis

Caching in production

Beyond query caching, Laravel can cache its own configuration, routes, and views:

bash
php artisan config:cache
php artisan route:cache
php artisan view:cache

Run these on every deployment. They make a noticeable difference.

The hard part: knowing when to clear cache

This is where people get into trouble. If you cache product data and then update a product, users will see the old data until the cache expires.

The fix is to clear the cache when data changes:

php
// In your Product model
protected static function booted(): void {
    static::saved(function () {
        Cache::forget('featured-products');
        Cache::forget('all-products');
    });
    
    static::deleted(function () {
        Cache::forget('featured-products');
        Cache::forget('all-products');
    });
}

Cache tags (Redis only)

If you have many related cache keys, tags let you clear them all at once:

php
Cache::tags(['products', 'homepage'])->put('featured', $data, 3600);

// Later, when products change:
Cache::tags('products')->flush(); // clears all product-tagged cache

What should you cache?

Good candidates: data that is read often but changes rarely. Navigation menus, product categories, site settings, popular posts.

Bad candidates: user-specific data (unless you include the user ID in the cache key), data that changes every few seconds, anything that must always be fresh.

Start with your slowest queries. Measure before and after. The results will motivate you to cache more.

LaravelCachingRedisPerformance

Ready to build something great?

Let's talk about your project. We will give you honest advice, a clear plan, and a fair price. No pressure, no sales pitch.

Free consultation
No commitment required
Response within 24 hours