Skip to content

Cache

Overview

The Cache system provides auto-invalidating Redis caching for Eloquent models via the Cacheable trait. Any model using this trait automatically flushes its tagged cache on create, update, or delete — ensuring data consistency without manual invalidation.

How It Works

php
use App\Traits\Redis\Cacheable;

class Setting extends Model
{
    use Cacheable;

    // Optional: override TTL (default 5 minutes)
    protected int $redisTtl = 60;
}

// Cache a query under this model's tag:
Setting::cacheRemember('val:core:general:name', fn () => 
    Setting::query()->where('module', 'core')->first()
);

Key Methods

MethodDescription
cacheRemember(key, callback)Cache the callback result under the model's tag
cacheFlush()Flush all cache keys under this model's tag
bootCacheable()Auto-registers flush on created, updated, deleted events

Key Files

app/Traits/Redis/
├── Cacheable.php            # Main trait — auto-invalidation on events
├── CacheKey.php             # Cache key generation
└── CacheOperations.php      # Low-level Redis operations