- How do I install and enable query caching for Eloquent in Laravel?
- Install via Composer with `composer require vigstudio/laravel-eloquent-query-cache`, then enable it globally in `AppServiceProvider` using `QueryCache::enable()`. For selective use, apply the `QueryCacheable` trait to specific models or use middleware to toggle caching per request.
- Does this package support Laravel 10 or only older versions?
- The package is designed for Laravel 8+ and should work with Laravel 10, but test thoroughly as Eloquent APIs evolve. Check the GitHub repo for compatibility notes or open an issue if you encounter version-specific issues.
- How does cache invalidation work if data changes?
- Invalidation relies on Laravel’s cache system (e.g., `Cache::forget()`). For automatic invalidation, listen to Eloquent events like `saved`, `deleted`, or `updated` and manually clear the cache keys. Example: `User::saved(fn() => Cache::forget('eloquent:User:all'))`.
- Can I cache complex queries with joins or dynamic scopes?
- The package works best with simple queries (e.g., `Model::all()`, `Model::where('active', true)`). Complex queries (e.g., raw joins, dynamic scopes) may require custom cache keys or exclusion logic. Test edge cases to avoid stale or inconsistent data.
- What cache drivers does this package support?
- It integrates with Laravel’s built-in cache drivers (Redis, Memcached, file, database, etc.). Configure your preferred driver in `config/cache.php`—the package will use the default or specified store unless overridden.
- How do I bypass caching for specific queries or tests?
- Use `QueryCache::disable()` to globally disable caching or apply the `->withoutCache()` method to individual queries. For testing, mock the cache facade or use `Cache::shouldReceive('get')->andReturn([])` in PHPUnit.
- Will caching slow down my application if the cache misses frequently?
- Cache misses add overhead, but the package is optimized for read-heavy workloads. Monitor cache hit/miss ratios in production (e.g., via Laravel Debugbar or custom logging) and adjust TTL or cache keys to improve efficiency.
- Are there alternatives to this package for Eloquent caching?
- Alternatives include Laravel’s built-in `Cache::remember()` (manual caching), `spatie/laravel-query-builder` (for query caching), or database-level caching (e.g., Redis with `SELECT` caching). This package offers a more seamless, model-level integration.
- How do I structure cache keys to avoid collisions?
- The package uses a default key format like `eloquent:{model}:{method}:{query_hash}`. Customize this in the config or override the `getCacheKey()` method in your models. Avoid dynamic keys for queries with identical parameters but different results.
- Is this package actively maintained? What’s the upgrade path for Laravel updates?
- The package is maintained by Renoki Co., with the last update in 2023. Monitor the GitHub repo for Laravel compatibility notes. If Eloquent APIs change, the package may require updates—check the changelog or contribute fixes if needed.