laravel-redis-model-cache maintained by sm-me
Laravel Redis Model Cache
Deterministic Redis caching for high-read Eloquent tables whose small set of filter fields is known up front. It replaces repeated database reads with indexed Redis lookups; it is not a general-purpose Eloquent query cache.
It does not support !=, LIKE, arbitrary WHERE clauses,
orderBy/groupBy/join, or all(). See
the complete query limitations table.
What this does not do automatically
Use it when a model is read frequently, its common filters can be declared as indexes, and predictable Redis-backed reads are worth the synchronous cache maintenance on writes.
Do not use it for ad-hoc reporting queries, full-table scans, joins, or query patterns that change constantly. The cache does not automatically turn arbitrary Eloquent queries into Redis queries.
Performance model
Model cache writes are synchronous by default. A save() or delete() on a
model using HasRedisModelCache updates Redis during the model event and adds
Redis latency to the request. See the
write-path details.
60-second quick start
composer require sm-me/laravel-redis-model-cache
use Illuminate\Database\Eloquent\Model;
use SMDev\RedisModelCache\Concerns\HasRedisModelCache;
class User extends Model
{
use HasRedisModelCache;
protected static function redisModelCacheConfig(): array
{
return ['indexes' => ['status']];
}
}
$activeUsers = app(\SMDev\RedisModelCache\RedisModelService::class, [
'model_class' => User::class,
'indexes' => ['status'],
])->where(['status' => 'active']);
The trait keeps cached models and indexes synchronized on saved, deleted,
and forceDeleted events. The example uses one indexed where() lookup; add
only fields your application queries regularly.
Full documentation
- Architecture and performance model
- Supported and unsupported queries
- Configuration
- Features, stampede protection, and SWR
- Invalidation lifecycle
- Performance and capacity planning
- Observability and debugging
- Upgrade guide
- Namespace stability commitment
- Static-analysis policy
- Full historical usage reference
Requirements
- PHP 8.3 or 8.4
- Laravel 11, 12, or 13
- Redis with phpredis or Predis
License and support
MIT. See LICENSE, CONTRIBUTING.md, and SECURITY.md.