Laravel cache strategies for performance

Carlos Mendez Jan 2026
4 tabs
<?php

use Illuminate\Support\Facades\Cache;

// Remember pattern - fetch from cache or execute closure
$posts = Cache::remember('posts.all', 3600, function () {
    return Post::with('author')->published()->get();
});

// Cache forever
Cache::forever('settings', $settings);

// Put value with TTL
Cache::put('key', 'value', now()->addMinutes(10));

// Get with default
$value = Cache::get('key', 'default');

// Get and delete
$value = Cache::pull('key');

// Check existence
if (Cache::has('key')) {
    // ...
}

// Increment/decrement
Cache::increment('post:views:' . $postId);
Cache::decrement('inventory:' . $productId, 5);

// Delete
Cache::forget('key');

// Clear all cache
Cache::flush();
4 files · php, blade Explain with highlit

Caching dramatically improves performance by storing expensive computations or database queries. Laravel supports multiple cache drivers—Redis, Memcached, file, database. I use Cache::remember() to fetch or compute and cache values with expiration. Cache tags group related items for bulk invalidation. The forever() method caches indefinitely until manually cleared. Model caching via packages like laravel-cacheable auto-caches queries. For query results, I cache collections with Cache::tags(['posts'])->remember(). Cache invalidation happens in model observers when data changes. The cache() helper provides a fluent API. Fragment caching in Blade with @cache caches view partials. Proper caching can reduce database load by 80%+ while keeping data fresh.