Skip to content

Query: prevent invalidated query caches from accumulating. - #254

Merged
JJJ merged 1 commit into
release/2.0.xfrom
fix/253-cache-retention-2.0
Sep 9, 2026
Merged

Query: prevent invalidated query caches from accumulating.#254
JJJ merged 1 commit into
release/2.0.xfrom
fix/253-cache-retention-2.0

Conversation

@JJJ

@JJJ JJJ commented Sep 9, 2026

Copy link
Copy Markdown
Collaborator

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:

  • WordPress 6.7.7, PHP 8.2, MariaDB 10.2, and Redis 7: 14 tests, 60 assertions passed using a test-only Redis adapter.
  • Default WordPress object cache: 14 tests, 56 assertions, one expected Redis-only skip.
  • The initial regression suite failed eight tests against unpatched 2.0.2.
  • PHP 7.4 syntax and git diff --check passed.
  • Claude reviewed the patch and found no blocking defects; the additional suggested regression cases passed.

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.

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.
Copilot AI lite review requested due to automatic review settings September 9, 2026 02:53
@JJJ
JJJ merged commit f1bb23c into release/2.0.x Sep 9, 2026
1 check passed
@JJJ
JJJ deleted the fix/253-cache-retention-2.0 branch September 9, 2026 02:53

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 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_changed alongside cached query results and ignore cached results when generations don’t match.
  • Replace the query-result cache write from cache_add() to cache_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.

Comment thread src/Database/Query.php
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.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants