$ lexprog.com

// notes from an old coder -- php, databases, and the occasional rant

[December 19, 2025] Laravel

Laravel Caching Strategies

Laravel Caching: Tips & Tricks

────────────────────────────────────────────────────────

Laravel Caching: Tips & Tricks

Tip: Use remember() for the Cache-Aside Pattern

$users = Cache::remember('all-users', 3600, function () {
    return User::all();
});

Checks cache first, runs the closure if missing, stores the result.

Gotcha: Cache Tags Require Redis or Memcached

The file and database drivers don't support tags.

Cache::tags(['users', 'admins'])->put('user-1', $user, 3600);
Cache::tags(['users'])->flush(); // Clears all user caches

Tip: Cache Locks for Race Conditions

$lock = Cache::lock('process-order', 10);

if ($lock->get()) {
    try {
        // Process order
    } finally {
        $lock->release();
    }
}

Prevents duplicate processing.

Gotcha: forever() is Not Really Forever

It uses a very long TTL (5 years). It can still be evicted by Redis memory policies.

Tip: Use Cache::pull() for One-Time Values

$token = Cache::pull('reset-token-' . $userId);

Gets the value AND deletes it in one operation.

Tip: Atomic Locks with block()

Cache::lock('sync', 10)->block(5, function () {
    // Waits up to 5 seconds for the lock
});

Gotcha: Serialization Overhead

Caching large Eloquent collections serializes all attributes. Use ->toBase() to cache only the data you need.

Tip: Cache Busting with Version Keys

$version = Cache::get('users-list-version', 1);
$users = Cache::remember("users-list-v{$version}", 3600, fn() => User::all());

// To invalidate:
Cache::increment('users-list-version');

Tip: Use route:cache Carefully

php artisan route:cache is fast, but it doesn't work with closure-based routes. Every time you cache routes, Laravel serializes them. If you have Route::redirect() or closure callbacks, the cache breaks. Stick to controller-based routes in production.

Tip: Model APP_KEY Rotation

Rotating APP_KEY invalidates all encrypted data — cookies, encrypted DB columns, and password reset tokens. If you must rotate (e.g., after a leak), plan a migration that re-encrypts existing data with the new key.

Gotcha: Local Scope Leaks

Global scopes defined in booted() apply to ALL queries on that model — including relationships. An innocent User::all() in admin panel might exclude soft-deleted users if a global scope is active.

Senior Insight

Caching is a performance multiplier, but misconfigured cache invalidation is a data integrity bomb. I've debugged incidents where stale cached query results served incorrect data for hours because the cache tags didn't match the actual invalidation keys. My advice: always use cache tags when invalidating related data, and never cache for longer than your business can tolerate stale data. A 5-minute TTL with proper invalidation beats a 24-hour TTL with buggy invalidation every time.

Source: Laravel News (https://laravel-news.com/), Freek.dev (https://freek.dev/tags/laravel), Spatie Blog (https://spatie.be/blog)

────────────────────────────────────────────────────────
<-- back to posts