feat(cache): add opt-in sort_query to merge reordered query strings - #353
Merged
Merged
Conversation
With @cache(sort_query=True) the default key builder stable-sorts the query parameters by decoded name, so ?a=1&b=2 and ?b=2&a=1 share one entry while repeated values of one name keep their order. Names and values are encoded as in the unsorted key; the default False leaves every key unchanged. build_cache_key() and invalidate() take the same keyword, and combining the flag with a custom key_builder raises CacheXError.
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 #267
What
@cache(sort_query=True)makes?limit=1&q=widand?q=wid&limit=1share one cache entry. The defaultFalseleaves every existing key byte-for-byte unchanged.How the query is sorted
str(request.query_params): Starlette parses the query (blank values kept, empty&&segments dropped, names and values percent-decoded) and re-encodes the pairs in the order sent.sort_querystable-sorts those same decoded(name, value)pairs by name and encodes them with the sameurlencode, so only the order changes. A query that is already in order gets the identical key with or without the flag.%20against+), and would split requests that the default key already treats as equal.%61asa. Sorting by the raw name would put%61=1apart from the otheravalues even though the key shows them as the same name.key=itemgetter(0)), so?tag=b&tag=astays distinct from?tag=a&tag=b: a handler readingtag: list[str]sees a different order.Validation and other entry points
sort_querymust be abool. Combining it with a customkey_builderraisesCacheXErrorwhen the decorator is applied (the same style as the other decoration-time checks), and the message points tobuild_cache_key(request, sort_query=True).build_cache_keygains that keyword-only parameter, so a custom builder can opt in itself.invalidate()gains a keyword-onlysort_query, validated the same way. Likekey_builderandvary, it cannot read the route's settings, so the caller passes the route's value. This is documented, and a test pins that omitting it misses the sorted entry.clear_path()matches on the path component only, so it is unaffected. A test covers this.Docs
docs/HTTP_CACHING.md: the "query order matters" note now describessort_query, and theinvalidate()section is updated.@cache,build_cache_keyandinvalidatedocstrings are updated.changelog.d/267.added.md.Tests
tests/test_cache_sort_query.pycovers:&,&&, a segment without=, blank values;key_buildercombination and non-bool values being rejected;invalidate()with a reordered query;clear_path().Mutation checks: