Per-record model caching via HasPermanentCache trait#20
Open
mhortulanus wants to merge 2 commits into
Open
Conversation
Add a new way to cache Eloquent models per record. Models opt in by using the `HasPermanentCache` trait and implementing `cachedResult(): mixed`. Records are registered in `AppServiceProvider::boot()` with `PermanentCache::models([...])`, which auto-attaches saved/deleted/restored listeners to keep the per-record cache in sync. - `HasPermanentCache` trait: cached(), refreshCache(), forgetCache(), isCached(), cacheUpdatedAt(). `$permanentCacheStore` selects the cache connection only; the key slug is `<model>:<getKey()>` and a record without a primary key throws. - Optional per-model knobs: `$permanentCacheExpression` (cron), `$permanentCacheQueue` (+ connection/queue name). - `RefreshModelCacheJob` queues refreshes when the model opts in. - `permanent-cache:warm` command iterates registered models in chunks; `--queue`, `--sync`, `--filter`, `--chunk` flags supported. - `permanent-cache:update` extended to also warm registered models. - Service provider schedules `permanent-cache:warm --filter=<basename>` per model that defines `$permanentCacheExpression`. - Events `PermanentCacheUpdating`/`Updated` now also accept Eloquent models. - Tests cover key shape, save/delete invalidation, missing PK guard, registration validation, and warm command end-to-end.
PHP's unserialize returned __PHP_Incomplete_Class for the (object) cast payload in some opcache/serializer scenarios, breaking ->value access. Storing a plain array sidesteps it entirely; cacheUpdatedAt() now parses an ISO-8601 string back into Carbon on read.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Adds per-record permanent caching for Eloquent models alongside the existing class-level
Cachedflow.HasPermanentCachetrait:cached(),refreshCache(),forgetCache(),isCached(),cacheUpdatedAt().$permanentCacheStoreselects the cache connection only; the cache key is always<model_basename>:<getKey()>. Records without a primary key throw aLogicException.$permanentCacheExpression(cron),$permanentCacheQueue(+ connection/name).```php
PermanentCache::models([User::class, Order::class]);
```
Auto-attaches `saved` → refresh, `deleted` → forget, `restored` → refresh.
Test plan