fix(static-cache): count each outcome once per request, for real - #3
Merged
Merged
Conversation
The guard's comment promised "count each outcome once per request", but RECORDED_KEY was a single last-value slot, so it only suppressed immediate repeats. The very sequence the comment describes — a middleware probe misses, the rendered page is handed to the cacher, nocache probes again — went miss -> write -> miss and counted the miss twice. The application cacher only reports a page once its prepared response is snapshotted, so that second probe really does miss. One page serve counted as two misses is a hit ratio that lies. Remember every outcome already counted instead of the last one. The repeat probe now also leaves the span's statamic.static_cache attribute on `write` rather than reopening it as a miss. Covered by a test that drives the sequence through the real cacher: it fails with `2.0 is identical to 1.0` before the fix.
sylvesterdamgaard
force-pushed
the
fix/static-cache-outcome-count
branch
from
September 23, 2026 17:35
27c8891 to
bb04666
Compare
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.
The guard in
StaticCacheTelemetry::recordOutcome()promised "count each outcome once per request", butRECORDED_KEYwas a single last-value slot:That suppresses an immediate repeat and nothing else. The very sequence the comment was written for goes
miss → write → miss, and the write in the middle clears the way for the second miss to be counted.It is real, not theoretical
The application cacher only reports a page once its prepared response has been snapshotted, so a probe after
cachePage()genuinely misses again. Driving the sequence through the real cacher onmain:One page serve, two misses.
statamic.static_cache.operationsis what a hit-ratio panel divides by, so this doesn't just add noise — it biases the ratio toward misses, and only for pages that were actually rendered and cached.The fix
The promise was right; the implementation was wrong. The request attribute now holds the set of outcomes already counted rather than the last one.
A second effect worth naming: the repeat probe used to overwrite the span's
statamic.static_cacheattribute back tomissafter the write. It now stayswrite, which is whatrecordWrite()'s own docblock says should happen ("the span attribute is overwritten so the final value tells the fuller story"). The existing span assertion already expectedwrite— it passed only because nothing exercised the repeat.Test
tests/Feature/StaticCacheTest.phpdrives probe → cache → probe through the resolved cacher and asserts the vocabulary plus the per-series totals, using 2.4's metric inspection:Before the fix it fails with
Failed asserting that 2.0 is identical to 1.0. Note thatassertLabelValuespasses either way — the vocabulary was never wrong, only the counts — which is why this needed the totals and not just an exact label set.pint, PHPStan and 78 tests green.
Note for whoever merges
Touches
tests/Feature/StaticCacheTest.php, as does #2 (additively, at the end of the file). Whichever lands second wants a trivial rebase.