Repository navigation
fix(cache)!: store and replay repeated response headers line by line - #411
Merged
Merged
Conversation
CacheEntry.headers becomes a tuple of (name, value) pairs in the order sent instead of dict[str, str] | None, so a header the handler sends more than once (several Link lines, a custom header read with getlist()) comes back from the cache as sent, and a 304 repeats every Vary line. Values are no longer joined, so one holding a comma stays one line. The constructor still accepts a dict, any iterable of pairs, or None, and rejects anything but (str, str) pairs. Redis and Memcached write the headers as a JSON list of [name, value] and still decode the 0.3.x object; malformed headers make the entry a miss. A hit sets its own Content-Type, Content-Length, ETag, Cache-Control and Age, even when an entry built outside @cache carries them. add_vary() puts the missing names on a new Vary line: Starlette's add_vary_header rewrote the first line and dropped the others, which lost a Vary line whenever @cache(vary=...) or the session middleware added a name. BREAKING CHANGE: CacheEntry.headers is a tuple of (name, value) pairs; code that reads it as a dict must iterate the pairs. 0.3.x cannot serve an entry 0.4.0 wrote with headers, which matters only for keys both versions share (a custom key_builder that does not call build_cache_key()). Closes #105
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.
Closes #105, following the revised design in the issue.
What changes
CacheEntry.headerstype. It is now a tuple of(name, value)pairs in the order sent, instead ofdict[str, str] | None; no headers is().dict, any iterable of pairs, orNone, and converts it.TypeErrorfor anything but(str, str)pairs.@cachestores and replays. It stores every header line. A hit and all three 304 paths (stored entry,no_cache, bypass/private) replay them in order.Linklines, or a custom header read withgetlist(), come back as sent.@cachecan therefore not duplicate framing headers or override the validators.[[name, value], ...]and still decodes the 0.3.x object. Any other shape is a miss.add_vary(). It puts the missing names on a newVaryline. Starlette'sadd_vary_headerrewrote the first line and dropped the others. As a result,@cache(vary=...)and the session middleware lost a handler's secondVaryline; that was already the case on master.Migration
MIGRATING_0_4.md#cache-entry-headersnow says which setups are affected in a rolling deploy.build_cache_key(), a rolling deploy needs nothing: the key format changed in the same release, so 0.3.x and 0.4.0 never read each other's HTTP entries.key_builderthat builds the key itself gives the same key in both versions. 0.3.9 answers500on an entry 0.4.0 wrote with headers. For such a route, keep 0.3.x off the shared backend during the rollout, or move the builder tobuild_cache_key()first.No version marker is needed for this.
Docs
CACHE_FLOW,HTTP_CACHINGandMIGRATING_0_4are updated in English and zh-TW.changelog.d/105.changed.md.Tests
tests/test_cache_repeated_headers.pycovers:Varylines on all three paths, with and withoutvary=;CacheEntryconversion and rejection;add_varywhen every name is already present.headers is Noneto(). The codec round-trips now use repeatedlinklines.add_vary;dict;Varyline.