Repository navigation
fix(object-cache): per-request version tokens and Redis purge - #23
Merged
Merged
Conversation
The native Redis and Memcached backends read the global and the group namespace version from the backend for every key: one get cost three round trips and get_multiple(50) cost 101 instead of one. Versions are now kept in memory for the adapter's lifetime (one request in the drop-in) and updated by the adapter's own clear(). This matches the request cache the proxy already keeps: a flush from another process becomes visible to a running request only for keys it has not read yet. Versions also become random tokens instead of counters. A version key lost to eviction used to restart at 1, which made entries written before a flush valid again; it now gets a fresh token. If the version cannot be read, the call uses a throwaway token and misses instead of assuming version 1. Existing counter values keep working as tokens, so upgrading does not invalidate the cache.
flush() and flush_group() only switch the namespace version, and purge() was a no-op, so every flush left the whole previous keyspace in Redis, mostly without a TTL. With the default noeviction policy and a deploy script that flushes on every release, Redis eventually rejects writes, including those of other tenants of the instance. The scheduled purge (and `wp sympress cache purge`) now SCANs the adapter's prefix and UNLINKs entries whose global or group version is no longer current. Flush itself stays an O(1) version switch, and entries without a TTL keep living until a flush supersedes them, so cache semantics do not change. A capped TTL on every entry was rejected because it would expire entries stored with no expiration. Only phpredis-compatible clients (phpredis, Relay) are scanned; Predis and cluster connections keep the previous behaviour. Memcached needs no purge because it evicts unreachable entries through its LRU.
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.
Stacked on #21 — merge #21 first; GitHub retargets this PR to
mainwhen #21's branch is deleted (rebase ontomainafterwards).Problem
Findings #2, #4 and #6 from the 2026-11-22 re-audit of the native Redis/Memcached object-cache backends:
rawKey()read the global and the group namespace version from the backend for every key: oneget= 3 round trips,get_multiple(50)= 101 instead of 1.flush()/flush_group()only bump the version;purge()was a no-op. Every flush left the whole previous keyspace in Redis, mostly without TTL. Withnoevictionand a flush per deploy, Redis eventually rejects writes.Fix
clear(). Trade-off: a flush from another process reaches a running request only for keys it has not read yet. The proxy's request cache already has that property for every key it has read, and WordPress' own Memcached drop-in caches its flush number per request the same way. Documented in the README.bin2hex(random_bytes(6))instead of counters; flush =SETa new token (no Lua needed any more). A missing version key gets a fresh token (SET NX /add, re-read on race); an unreadable version uses a throwaway token, so the call misses instead of assuming version 1. Existing counter values ("1","2", …) are valid tokens, so the upgrade does not invalidate the cache.purge()because it keeps current semantics: flush stays an O(1) version switch, and entries stored without expiration keep living until a flush supersedes them. A TTL cap would expire entries that callers stored withexpire = 0, which changes behaviour.purge()runs from the existingsympress.framework.cache_purgecron andwp sympress cache purge. It scans<prefix>:v*(prefix glob-escaped) in batches of 1000, compares each key's global/group token with the current stored ones (request cache is reset first) and UNLINKs stale keys. Deleting is always safe for a cache. A race with a concurrent flush can only delete entries early, never keep stale ones readable.shortcut:): only phpredis-compatible clients (phpredis, Relay) are scanned. Predis and cluster/RedisArray connections keep the previous no-op, because their SCAN signatures differ and that path is untested anyway.Tests
NativeObjectCacheVersionTest(Redis + Memcached doubles):get= 1 backend read,get_multiple(50)= 1 read after warm-up. Fails on fix(object-cache): native backend and request cache correctness #21's head (3 / 101 reads), passes here.backend-smoke.yml): afterclear(),purge()on real Redis leaves only version keys under the prefix. Not run locally (no ext-redis/Redis in the local image); CI covers it.composer validate --strict,composer cs,composer static-analysis,composer testsgreen (100 tests, 3 live-cache skips).Release
chore(release): prepare 1.0.12(patch: bug fixes only; CHANGELOG only, as in the previous release commits). It covers #21, this PR and the unreleased492db26 ci: let Dependabot update GitHub Actions pins.Risk to note: version tokens are 12 hex chars instead of small integers, so raw keys get about 20 bytes longer. With the default prefix, Memcached keys stay around 195 bytes (limit 250). A very long custom
SYMPRESS_CACHE_PREFIXhas less headroom than before.Replaces #22, which GitHub closed when its stacked base branch was deleted after #21 merged.