-
Notifications
You must be signed in to change notification settings - Fork 27
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
cache_key should be dependent on chosen_algorithm #53
Comments
Thanks for digging into this. I think this can (should?) also be solved by more carefully setting the def get_cache_key(request):
# EDIT: the cache key mush always be a string
return f"{request.url};{request.headers.get('Accept-Encoding')}" The I agree the current situation needs to be improved, either by merging your PR, or updating the docs with better examples and warn about this pitfall. |
Setting the cache key based on Accept-Encoding may cause redundancy in the cache, since different values of Accept-Encoding may lead to the same chosen_algorithm. This is not a huge problem, but I will argue that my PR is the more technically correct. |
I am inclined to agree. Note that in practice, there are not many |
Hope I'm not dogpiling, but I 100% agree that the solution that @xmcp provided is more elegant. I've implemented the cache key trick mentioned above and while it works, the new fix is highly desirable for those of us using both flask-caching and flask-compress in production. Huge kudos. |
After more testing of this, it turns out releasing this would have broken quite a lot of things (for starters, pretty much all my projects 😉). Flask-Caching relies on cachelib. If you look at the documentation of cache.get, you will see that the key must be of It does not show in the example above, as the default The trouble begins when you use another cache type, personally I rely often on The error I got when testing this was not obvious and did not match the
I was a bit unlucky here, as the clear error in So the next steps:
In hindsight I should have required a test before merging, even though on |
Oops sorry for that. I only tested it with |
@xmcp are you planning to open another PR? |
I am busy with other things in the following days. I am okay with minor changes like casting the type to |
Fixed by e3aeec8 |
For the record release 1.16 shipped with that fix. |
Currently the
after_request
function finds the cached response throughkey = self.cache_key(request)
.However, this key does not consider
chosen_algorithm
, so if a request wants brotli, the brotli response body will be cached, and following gzip requests will be incorrectly served with brotli body.Step to reproduce:
The text was updated successfully, but these errors were encountered: