spiritix/lada-cache
Redis-backed, fully automated query cache for Laravel. Transparently accelerates Eloquent and Query Builder with granular, automatic invalidation (rows/tables), scalable for clusters, with controls to include/exclude tables and optional Debugbar insights.
Installation
composer require spiritix/lada-cache
php artisan vendor:publish --provider="Spiritix\LadaCache\LadaCacheServiceProvider"
Add LadaCacheTrait to a base model or directly to models:
use Spiritix\LadaCache\Database\LadaCacheTrait;
class BaseModel extends Model
{
use LadaCacheTrait;
}
Enable in .env
LADA_CACHE_ACTIVE=true
LADA_CACHE_DEBUGBAR=true # Optional: for Debugbar integration
First Use Case Immediately start seeing cached queries. Test with:
php artisan lada-cache:flush # Clear cache for testing
Verify via Debugbar (if enabled) or by checking Redis keys.
// Automatically cached
$users = User::where('active', true)->get();
$count = User::count();
config/lada-cache.php:
'exclude_tables' => ['sensitive_data', 'logs'],
\Spiritix\LadaCache\Facades\LadaCache::disable();
// Critical operations...
\Spiritix\LadaCache\Facades\LadaCache::enable();
php artisan lada-cache:flush # Clear all cache
php artisan lada-cache:disable # Temporarily disable
php artisan lada-cache:enable # Re-enable
DB::connection('foo')->getPdo() for non-Lada connections.$connection property work automatically.lada-cache.redis_connection in .env:
LADA_CACHE_REDIS_CONNECTION=custom_redis
insert, update, delete, or truncate.\Spiritix\LadaCache\Facades\LadaCache::invalidate(['users', 'posts']);
Composite Primary Keys
id).Raw SQL Bypasses Cache
DB::select("RAW SQL") or DB::statement().Third-Party Query Builders
Spiritix\LadaCache\Database\Query\Builder for integration.Debugbar Conflicts
fruitcake/laravel-debugbar is installed (v6.x+).composer require fruitcake/laravel-debugbar.Pessimistic Locks Bypass Cache
lockForUpdate()/sharedLock() queries are not cached.Large Payloads
redis-cli KEYS "lada:*"
LADA_CACHE_LOG_QUERIES=true
SADD).Cache::flush() uses UNLINK by default.Custom Reflector
Override Spiritix\LadaCache\Database\Reflector to handle edge-case SQL.
Event Listeners
Listen to lada.cache.invalidated or lada.cache.missed:
\Event::listen('lada.cache.invalidated', function ($tags) {
Log::debug("Invalidated tags: " . implode(', ', $tags));
});
Connection Binding
Bind custom drivers via DB::extend():
DB::extend('custom', function ($config) {
return new \Spiritix\LadaCache\Database\Connection($config);
});
'default_expiration' => 60, // 1 minute
'exclude_tables' => ['password_resets', 'failed_jobs'],
LADA_CACHE_ACTIVE=${LADA_CACHE_ACTIVE:-false}
| Issue | Solution |
|---|---|
| Stale data after invalidation | Ensure LADA_CACHE_ACTIVE=true |
| Debugbar not showing stats | Install fruitcake/laravel-debugbar |
| Row-level invalidation fails | Check for composite primary keys |
| Cache not flushing | Use php artisan lada-cache:flush |
| Slow first request | Pre-warm cache with php artisan lada-cache:warm (if available) |
```markdown
---
**Note**: Always test in a staging environment before deploying to production, especially when toggling cache settings or invalidating large datasets.
How can I help you explore Laravel packages today?