refactor: early-return writeResponseToCache on cache hit#32
Merged
Conversation
Skip the parent invocation when shouldWriteResponseToCache() is false. Behavior is unchanged - the parent already short-circuits via its own guard - but the structure is clearer and avoids walking into the parent when no write will happen. Adds tests covering cache hit (refresh marker untouched), fresh fetch (marker written), and setWriteCache(false) (marker skipped even on a fresh fetch). Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
aandrews-di
approved these changes
May 1, 2026
jwadhams
approved these changes
May 1, 2026
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
Restructure
AbstractUseStaleRequest::writeResponseToCache()to early-return whenshouldWriteResponseToCache()is false, instead of wrapping only the refresh-marker write and then falling through toparent::writeResponseToCache().Why
The previous shape was correct in net effect — the parent already short-circuits on cache hit via its own guard — but it had two drawbacks:
parent::writeResponseToCache(), doing duplicate guard work that produced no side effects.writeResponseToCache()got an unclear contract: callingparent::looked like it might do something, but actually never did on a cache hit.The new shape makes the no-op explicit. If a subclass calls
parent::writeResponseToCache()and adds side effects after, the parent's behavior is now unambiguous: it either fully wrote (marker + response) or it didn't run at all.This is motivated by debugging an AIS-side bug (MR-7089) where a subclass of
AbstractUseStaleRequestaddedCache::tags(...)->flush()afterparent::writeResponseToCache(). The flush fired on every cache hit because nothing in the chain signalled "no write happened." Cleaner framework semantics make that class of mistake easier to spot.What changed
app/AbstractUseStaleRequest.php—writeResponseToCache()now returns at the top when the guard is false.tests/Feature/AbstractUseStaleRequestTest.php— three new tests covering the three branches:testCacheHitDoesNotWriteRefreshMarker— seeds a sentinel marker, callssync(), asserts the sentinel survives.testFreshFetchWritesRefreshMarker— asserts the marker is written with value'refresh after'after a live fetch.testWriteCacheDisabledSkipsRefreshMarker— assertssetWriteCache(false)blocks both the marker write and the response write.Behavior change
None at the framework boundary — all existing tests should still pass, including
testCacheBehaviorUnderHeavyLoadwhich exercises the full lifecycle. The parent method simply isn't invoked on a cache hit anymore (it would have no-op'd anyway).Test plan
vendor/bin/phpunit tests/Feature/AbstractUseStaleRequestTest.phppasses locally