- What does affordablemobiles/laravel-eloquent-query-cache do?
- It is a Laravel package that caches the results of Eloquent queries so repeated identical reads can be served from your cache store instead of hitting the database. You install it with Composer and follow the package setup to apply caching to the queries you choose. It is intended to reduce database load in read-heavy Laravel applications.
- How do I install laravel-eloquent-query-cache in a Laravel app?
- Install it with Composer using composer require affordablemobiles/laravel-eloquent-query-cache. Then follow the package README for any provider registration, configuration publishing, or migration steps required by your Laravel version. Laravel package auto-discovery usually handles provider registration for supported versions.
- Which Laravel versions are supported by this query cache package?
- Supported Laravel versions depend on the specific package release, so check the composer.json constraints and README before upgrading. Match the package version to your Laravel major version and run your test suite after any upgrade. If you are on an older Laravel release, verify compatibility or look for a tagged version that supports it.
- How does cache invalidation work for cached Eloquent queries?
- Caching query results creates a risk of stale data, so the package is designed to work with Laravel's cache and model events to clear affected entries when underlying records change. Review the README for the exact invalidation behavior and any configuration needed for your models. For critical data, test invalidation paths or bypass the cache on reads that must always be fresh.
- Can I use this package with Redis, Memcached, or the database cache driver?
- It should work with any cache store Laravel supports because it builds on Laravel's cache repository. Configure the store in config/cache.php and make sure it suits your traffic and TTL needs. Redis or Memcached are common choices for fast query caching, while the database driver is simpler but can add load.
- How do I enable or disable caching for specific Eloquent queries?
- The package is intended to let you opt in on the queries you want cached rather than caching everything automatically. Follow the README for the supported methods or query builder calls. Keep a way to bypass the cache for admin screens, debugging, or data that changes constantly.
- Does it cache eager-loaded relationships and nested query results?
- Caching applies to the Eloquent query results you mark for caching, and eager-loaded relationships are part of that result set depending on how you structure the call. Verify the behavior in your tests, especially for nested relationships. If relationship data changes often, consider caching at a narrower level or using a shorter TTL.
- How should I test code that uses the Eloquent query cache?
- Use Laravel's testing helpers and configure a fast, isolated cache store such as the array driver for tests. Clear or flush the cache between tests so results do not leak across cases. Write tests that assert both cached and uncached behavior, including invalidation when records change.
- What production concerns should I consider before enabling query caching?
- Choose a TTL that balances freshness with database savings, and make sure your cache store has enough memory and an appropriate eviction policy for your query volume. Monitor hit rates and database load after enabling it. For critical reads, consider shorter TTLs or explicit cache clearing when data changes.
- Is this package maintained, and what are the alternatives?
- Check Packagist and the repository for the latest release date, open issues, and compatibility with your Laravel version before adopting it. Alternatives include Laravel's own cache tags and manual Cache::remember calls, or other query-cache packages. For simple cases, wrapping hot queries with Cache::remember is often enough; a package helps when you want consistent behavior across many queries.