Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message
Laravel Model Caching

Laravel Model Caching Laravel Package

mikebronner/laravel-model-caching

Speeds up Eloquent by automatically caching model queries and relationships, cutting repetitive database hits. Drop-in package with cache tagging support, configurable cache stores and TTLs, and easy invalidation on model updates—ideal for high-traffic Laravel apps.

View on GitHub
Deep Wiki
Context7

Eloquent model-caching made easy.

Frequently asked questions about Laravel Model Caching
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.
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
codifyo/ts-generator-bundle
andydefer/laravel-cluster
testo/fiber
mintobit/jobqueue
a4sex/maintenance-bundle
a4sex/entity-date-update
a4sex/client-identifier
a4sex/base-utilites
a4sex/key-value-storage
a4sex/micro-status
chilldev/dependency-injection-extra
datinglibre/datinglibre-app-api
biberltd/corebundle
bricre/symfony-bundle-test
biberltd/logbundle
dominium/http-adapter-bundle
dominium/google-analytics
a4sex/auto-clean-entity
christhompsontldr/laravel-inky
spatie/mailcoach-vapor