- How do I install and enable caching for Eloquent models in Laravel?
- Run `composer require mikebronner/laravel-model-caching`, then add the `Cachable` trait to your Eloquent models. No additional configuration is needed for basic usage—queries and relationships are automatically cached. Configure cache stores (Redis, DynamoDB, etc.) via the `config/model-caching.php` file.
- Which Laravel versions are officially supported?
- The package supports Laravel 11, 12, and 13. It leverages modern Eloquent features and is tested against these versions. Older Laravel versions (e.g., 10) are not supported due to breaking changes in the query builder.
- Does this package work with multi-tenant applications?
- Yes, it supports multi-tenancy via the `$cachePrefix` property in your models. Set this to a tenant-specific value (e.g., `tenant_{$tenantId}`) to isolate cache entries. DynamoDB and Redis both support this pattern natively.
- How does cache invalidation work when models are updated?
- Invalidation is automatic and triggered by Eloquent events (created, updated, deleted). The package flushes all cached entries for the affected model and its relationships. For high-churn models (e.g., comments), use `cacheCooldownSeconds` to delay invalidation and reduce database load.
- Can I use DynamoDB for caching in serverless environments?
- Yes, DynamoDB is fully supported. The package handles logical invalidation via namespace versioning, but be aware of eventual consistency. Enable `MODEL_CACHE_FALLBACK_TO_DB` in `config/model-caching.php` for resilience during DynamoDB throttling or outages.
- What happens if I disable caching for specific models or queries?
- Use the `disableCache()` method on a query or model to opt out of caching. This is useful for admin panels, real-time updates, or queries with dynamic `select()` clauses. The package respects this globally without requiring configuration.
- How do I monitor cache hit/miss ratios and performance?
- The package logs cache events (hits/misses/invalidations) to Laravel’s log channel. For deeper insights, integrate with tools like Laravel Debugbar or Prometheus to track metrics. DynamoDB and Redis provide native monitoring dashboards for cache store health.
- Are there alternatives to this package for Laravel model caching?
- Alternatives include `spatie/laravel-query-builder` (for manual caching) or `stitcherhq/laravel-model-caching` (similar trait-based approach). However, this package stands out for its automatic invalidation, multi-store support, and granular relationship caching without manual key management.
- How should I test cache behavior in CI/CD pipelines?
- Mock the cache store in tests using Laravel’s `Cache::shouldReceive()` or `Mockery`. For DynamoDB, use localstack or a test container. Disable caching in staging (`MODEL_CACHE_ENABLED=false`) to verify no cached data leaks into production.
- What are the risks of using fallback-to-database mode?
- Fallback-to-database (`MODEL_CACHE_FALLBACK_TO_DB`) adds resilience but can mask cache-related bugs during development. Enable it only in production if cache availability is critical. Monitor query logs for unexpected database falls back, which may indicate misconfigured TTLs or invalidation issues.