- How do I cache an Eloquent query result for 10 minutes using eusonlito/laravel-database-cache?
- Use the `cache()` method on your Eloquent query chain. For example: `User::where('active', true)->cache(600)->get();` This stores the result in the database cache table with a 10-minute TTL. The package automatically handles key generation and invalidation via Laravel events.
- Does this package work with Laravel 11 when it releases?
- The package is designed for Laravel 10.x+ and should work with Laravel 11 if no breaking changes are introduced to the query builder or Eloquent. Always check the package’s `composer.json` for Laravel version constraints after upgrading. If issues arise, the GitHub issues tab is the best place to track compatibility.
- Can I use this alongside Redis for hybrid caching?
- Yes, this package is designed to coexist with other caching strategies. Use Redis for hot, frequently accessed data and fall back to database caching for less critical or offline-capable queries. Configure TTLs per query to balance performance and freshness.
- How does cache invalidation work if I update a record via a raw SQL query?
- The package relies on Laravel’s event system (e.g., `eloquent.updated`, `eloquent.deleted`) for invalidation. Raw SQL updates won’t trigger these events by default. For critical cases, manually clear the cache using `DB::table('laravel_cache')->where('key', 'like', 'user%')->delete();` or implement custom observers for your models.
- Will caching queries with this package slow down my database?
- Database caching adds minimal overhead (~1–10ms per query) but can increase write load due to INSERT/UPDATE operations on the cache table. Monitor the cache table size and query performance. For high-volume apps, consider batching cache writes or optimizing the cache table with indexes (e.g., on `key` and `expires_at`).
- How do I configure the cache table name and TTL globally?
- Publish the config file with `php artisan vendor:publish --tag=database-cache-config`, then set `DB_CACHE_TABLE` and `DB_CACHE_TTL` in your `.env` file. Example: `DB_CACHE_TABLE=app_query_cache` and `DB_CACHE_TTL=3600` for a 1-hour default TTL. You can also override these per query using the `cache()` method.
- Does this package support caching complex queries with joins or subqueries?
- Basic joins and subqueries are supported, but dynamic WHERE clauses or highly complex queries may not cache as expected. The package generates cache keys based on the query string, so identical queries will reuse the same cache entry. For edge cases, manually specify a cache key using `cache($key, $ttl)` instead of relying on auto-generation.
- What’s the best way to monitor cache hits and misses?
- The package doesn’t include built-in monitoring, but you can extend it or use Laravel Debugbar to log cache operations. Add middleware or a service provider to track hits/misses by checking the cache table before and after queries. Alternatively, wrap the `cache()` method in a custom trait for observability.
- How do I manually invalidate the cache for a specific model?
- Use the `CacheObserver` pattern. Register an observer for your model (e.g., `User::observe(UserCacheObserver::class)`) and implement `handleCacheInvalidation` to clear stale cache entries. Example: `public function handleCacheInvalidation($model) { DB::table('laravel_cache')->where('key', 'like', 'user%')->delete(); }`
- Is this package suitable for a high-traffic Laravel API with 10,000+ requests per minute?
- This package is best suited for read-heavy workloads, but at scale, database caching may become a bottleneck due to write operations. Test under load and consider alternatives like Redis for high-throughput APIs. If you proceed, monitor database performance closely and optimize the cache table (e.g., add indexes, use batch deletes for invalidation).