spiritix/lada-cache
Redis-based, fully automated query cache for Laravel. Transparent with Eloquent/Query Builder, granular invalidation of affected rows/tables, scalable and cluster-ready. Includes Debugbar insights and table include/exclude controls.
composer require spiritix/lada-cache
php artisan vendor:publish --provider="Spiritix\LadaCache\LadaCacheServiceProvider"
.env:
LADA_CACHE_ACTIVE=true
LADA_CACHE_DEBUGBAR=true # Optional (for Debugbar integration)
use Spiritix\LadaCache\Database\LadaCacheTrait; to all Eloquent models.// Automatically cached
$users = User::where('active', true)->get();
Transparent Integration:
Connection subclass (registered via DB::extend()).Granular Control:
config/lada-cache.php:
'exclude_tables' => ['sensitive_data', 'logs'],
User::withoutCache()->where('id', 1)->first(); // Skips cache
Invalidation:
php artisan lada-cache:flush or Cache::flush().Multi-Connection Support:
DB::connection('mysql2')) if models specify $connection.Debugbar Integration: Visualize cache metrics (hits/misses/invalidations) in Debugbar.
// Enable in config/lada-cache.php
'debugbar' => [
'enabled' => env('LADA_CACHE_DEBUGBAR', false),
],
Custom Redis Connection: Use a non-default Redis connection:
'redis_connection' => 'custom_redis',
Base Model Approach:
// app/Models/BaseModel.php
use Spiritix\LadaCache\Database\LadaCacheTrait;
abstract class BaseModel extends Model {
use LadaCacheTrait;
}
Bulk Operations:
Lada handles insertUsing, updateFrom, and truncate invalidations automatically.
Composite Primary Keys:
Raw SQL Bypasses Cache:
DB::select(), DB::statement(), or raw PDO are not cached.Debugbar Conflicts:
'debugbar' => ['enabled' => false],
Connection Issues:
Connection subclass may conflict with third-party packages overriding DB::extend().config/app.php under providers.Locking Bypasses Cache:
lockForUpdate()/sharedLock() queries skip caching (pessimistic locks).LADA_CACHE_DEBUG_KEYS=true in .env to log generated keys (helpful for diagnosing collisions).redis-cli --scan --pattern "*:lada:*"
Custom Reflector:
Override the SQL reflector to handle edge cases (e.g., complex UNION queries):
// app/Providers/AppServiceProvider.php
use Spiritix\LadaCache\Reflector\QueryReflector;
public function register() {
$this->app->bind(QueryReflector::class, function () {
return new CustomQueryReflector();
});
}
Cache Key Customization: Extend the key generator:
// app/Providers/AppServiceProvider.php
use Spiritix\LadaCache\Cache\CacheKeyGenerator;
public function register() {
$this->app->bind(CacheKeyGenerator::class, function () {
return new CustomCacheKeyGenerator();
});
}
Post-Invalidation Hooks: Listen for invalidation events:
// app/Providers/EventServiceProvider.php
protected $listen = [
\Spiritix\LadaCache\Events\CacheInvalidated::class => [
\App\Listeners\LogInvalidation::class,
],
];
'expiration' => 60, // Default: 60 seconds
'table_expiration' => [
'sessions' => 300, // 5 minutes
],
lada-cache.redis_connection) is properly configured in config/database.php.expiration for volatile data or adjust include_tables to exclude rarely accessed tables.predis/predis or phpredis for horizontal scaling.
```markdown
### Pro Tips for Daily Use
1. **Cache Warming**:
Pre-load critical queries during deployment:
```php
// In a deploy script
User::where('is_admin', true)->cached()->get(); // Forces cache population
Tag-Based Invalidation: Manually invalidate specific tags (e.g., after bulk imports):
Cache::tags(['users'])->flush();
Temporary Disabling: Disable cache during migrations or testing:
php artisan lada-cache:disable
# ... run migrations ...
php artisan lada-cache:enable
Debugging Stale Data: Force a cache miss for a specific query:
User::withoutCache()->where('id', 1)->first();
Monitoring: Track cache efficiency with:
php artisan lada-cache:stats
(Add this to your Artisan commands if missing.)
How can I help you explore Laravel packages today?