sm-me / laravel-redis-model-cache
Optimized Redis model and hash-set caching service for Laravel Eloquent.
Package info
github.com/sm-me-dev/laravel-redis-model-cache
pkg:composer/sm-me/laravel-redis-model-cache
Requires
- php: ^8.3 || ^8.4
- illuminate/console: ^11.0 || ^12.0 || ^13.0
- illuminate/database: ^11.0 || ^12.0 || ^13.0
- illuminate/redis: ^11.0 || ^12.0 || ^13.0
- illuminate/support: ^11.0 || ^12.0 || ^13.0
Requires (Dev)
- larastan/larastan: ^3.10
- laravel/boost: ^2.4
- laravel/pint: ^1.29
- orchestra/testbench: ^9.0 || ^10.11 || ^11.0
- pestphp/pest: ^3.8
Suggests
- ext-lz4: For LZ4 compression support (https://github.com/kjdev/php-ext-lz4)
- ext-zstd: For Zstandard compression support (https://github.com/kjdev/php-ext-zstd)
Provides
None
Conflicts
None
Replaces
None
README
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.