How laravel-model-caching works
masterThe package provides automatic caching for Eloquent model queries and eager-loaded relationships. By adding a specific trait or extending a base class, you enable a layer that caches query results and automatically flushes relevant cache entries when models are created, updated, or deleted. This eliminates the need for manual cache key management and manual invalidation logic.
What is cached:
- Model queries:
get,first,find,all,paginate,pluck,value,exists. - Aggregations:
count,sum,avg,min,max. - Eager-loaded relationships (using
with()).
What is NOT cached:
- Lazy-loaded relationships: Only relationships loaded via
with()are cached. You must usewith()to benefit from caching. - Queries with
select()clauses: Custom column selections bypass the cache. - Queries inside transactions: Cache is not automatically flushed when a transaction commits. You must call
flushCache()manually if needed. inRandomOrder()queries: Caching is automatically disabled for these queries to ensure varied results.