wddyousuf/eloquent-autocache
Automatic, self-invalidating Eloquent query caching. Add one trait and reads are cached transparently while any write flushes the model's cache. Uses cache tags or per-model versioning, so it works on every store.
lockForUpdate() / sharedLock() /
lock() queries were eligible for query-level caching — and on grammars that
compile the lock clause away (SQLite), a locked canonical find() could even
be served from the row cache. A warm entry then satisfied the read without
touching the database, so no row lock was acquired and check-then-write
sections relying on it ran unserialized. Locked queries now bypass the cache
in both directions: never served from it, never stored into it.Supersedes 0.2.3, which was withdrawn: a premature
v0.2.3tag was published to Packagist against an earlier commit, and Packagist blocks moving a released version to a new commit. 0.2.4 is the same code cut cleanly frommain.
cache_in_transactions, default true).
Reads issued while a transaction is open can observe a not-yet-committed write
and cache it; a later ROLLBACK then leaves that value stale until its TTL. The
default preserves the previous behavior (so caching keeps working under
RefreshDatabase). Set false — globally or per-model via $cacheInTransactions
— and reads bypass the cache entirely while a transaction is open, so a
rollback can never poison it. Writes still flush.$post->tags()->sync()/->attach()/->detach()) is a bare pivot statement
that never reaches a cacheable model's builder, so cached relation reads went
stale until TTL — unsafe when the pivot gates authorization. Configure
autocache.pivot_invalidation.map (pivot table => [cacheable models]) and
AutoCache watches the query stream, flushing those models on any write to a
listed pivot, wherever it originates. Opt-in: the listener is only registered
when the map is non-empty, so there is zero query-stream overhead otherwise.cache.serializable_classes => false, which forbids unserializing any class
(including stdClass and Eloquent models) out of a serializing cache store.
Cached query rows are now stored as plain arrays and re-objectified on read,
and canonical find() lookups cache the raw attribute array instead of the
hydrated model. Previously, on a default Laravel 13 app any ->paginate()
threw in getCountForPagination() (#4), find() returned an unusable
__PHP_Incomplete_Class, and cached get() results silently leaked a junk
__PHP_Incomplete_Class_Name attribute. No configuration change is required.CachedBuilder ([@template](https://github.com/template) +
[@extends](https://github.com/extends) Builder<TModelClass>) and annotated the trait's builder-returning
methods as CachedBuilder<static>. Static analysers (PHPStan/Larastan) no
longer widen queries on a Cacheable model back to a bare Model, so
Post::query()->first() resolves to Post again. Docblock-only; no runtime
change (#2).illuminate/database, illuminate/support, and
illuminate/cache now allow ^13.0 (#3).orchestra/testbench ^11.0 and
phpunit/phpunit ^12.0.Initial release.
Cacheable trait: transparent, self-invalidating query caching for Eloquent
models via a single trait.find($id) lookups are cached under a stable
per-row key and survive writes to other rows (version-counter stores).mode => 'opt-in') with ->cache() / Model::cache(),
plus a per-model $cacheMode property overriding the global mode.swr) via Cache::flexible() on Laravel 11+
(skipped for models with a max_rows cap so the size guard always applies).AutoCache::fake() test double with assertFlushed, assertNotFlushed,
assertNothingFlushed, assertHit, and assertMissed.runSelect), so get,
first, find, pluck, value, aggregates (count/sum/avg/min/max),
exists, and pagination counts are all cached uniformly.insert/upsert/insertOrIgnore/insertUsing/
insertOrIgnoreUsing/updateFrom, increment/decrement, truncate, and
event-suppressing "quiet" writes.$flushRelated property.lock_for).ttl_jitter) to avoid synchronized expiry.max_rows) and volatile-query skipping.withoutCache(), cacheFor(), cacheKey(), plus the
static Model::withoutCache() / Model::cacheFor() entry points.CacheHit, CacheMissed, and CacheFlushed events.stats).AutoCache facade and autocache:flush, autocache:clear,
autocache:warm (with --all), and autocache:stats (with --reset)
Artisan commands.How can I help you explore Laravel packages today?