Query: prevent invalidated query caches from accumulating. - #254
Merged
Conversation
Store the cache generation with each result instead of in its key, and replace results at the stable key after invalidation. Remove unset-value sentinels from the hash so identical queries share that key. Props hellofromahmed for reporting the persistent-cache growth. Props robincornett for the original unset-value cache-key correction. See #253, #184, #185.
Contributor
There was a problem hiding this comment.
🟢 Approval recommended
The change is localized to query caching, aligns with the stated goal of preventing orphaned cache-key growth, and does not introduce new API surface.
Pull request overview
This PR adjusts src/Database/Query.php’s query-result caching so repeated invalidations do not generate accumulating, permanently-stored cache keys by switching to a stable cache key and embedding cache-generation (last_changed) in the cached payload.
Changes:
- Store
last_changedalongside cached query results and ignore cached results when generations don’t match. - Replace the query-result cache write from
cache_add()tocache_set()to overwrite stale results under a stable key. - Remove per-instance “unset-value” sentinel defaults from the cache-key hash so equivalent query instances share a key.
File summaries
| File | Description |
|---|---|
| src/Database/Query.php | Makes query-result cache keys stable across invalidations and across equivalent query instances by embedding generation in the value and stripping sentinel defaults from the hashed slice. |
Review details
- Files reviewed: 1/1 changed files
- Comments generated: 1
- Review effort level: Lite
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
This was referenced Sep 9, 2026
JJJ
added a commit
that referenced
this pull request
Sep 9, 2026
Query: prevent invalidated query caches from accumulating. Port the 2.0.3 result-cache fix to the 3.0 maintenance line. Store the cache generation with each result, validate it before reuse, and replace stale results at the same key. Capture the generation before reading the database. Remove the unused private cache-key parameter. Add regression coverage for repeated mutations, missing and stale generations, empty results, and invalidation during a database read. Props hellofromahmed for reporting the persistent-cache growth. Props robincornett for the existing unset-value cache-key correction. See #253, #254, #185.
JJJ
added a commit
that referenced
this pull request
Sep 9, 2026
Query: prevent invalidated query caches from accumulating. Port the maintenance result-cache fix to the development branch. Store the generation in the cached value, replace stale results under a stable key, and include the generation in primed relationship query results. Remove the unused private cache-key parameter and add regression tests. Props hellofromahmed for reporting the persistent-cache growth. Props robincornett for the existing unset-value cache-key correction. See #253, #254, #185.
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.
Invalidating a query currently creates another persistent cache key. This maintenance fix stores the generation with the cached results and replaces stale results at a stable key, preventing successive invalidations of the same query from accumulating entries.
Remove unset-value sentinels from the query hash, following Robin Cornett's correction in #185, so equivalent query instances use the same key. The generation is captured before reading the database and checked when reading cached results.
This changes only two methods in
src/Database/Query.php, with no new API, dependencies, or minimum runtime requirements. Existing orphaned cache entries are not removed by this fix. Distinct queries still have distinct cache entries; this does not introduce expiration.Validation was performed outside the maintenance branch, with no test infrastructure added to the release:
git diff --checkpassed.Props @hellofromahmed (Ahmed Saeed) for reporting the persistent-cache growth, and @robincornett (Robin Cornett) for the original unset-value cache-key correction.
See #253, #184, #185. This PR addresses the 2.0.x maintenance line; #253 stays open for the 3.x up-port.