-
Notifications
You must be signed in to change notification settings - Fork 22
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 doesn't work with GZIPMiddleware #210
Comments
I have the same problem with following(simplified) middlewares setup in fastapi application: app.add_middleware(gzip.GZipMiddleware, minimum_size=100)
app.add_middleware(CacheRequestControlMiddleware)
cache.setup(settings_url="mem://?check_interval=10&size=10000") $ python --version
Python 3.9.18
$ pip freeze
...
cashews==7.0.2
fastapi==0.110.0
starlette==0.36.3
uvicorn==0.28.0
... After quick debug I see that I hope this information will help with investigation. |
Sorry for a delay I spend some time trying to reproduce it but can't from fastapi import FastAPI, Response
from starlette.middleware import gzip
from cashews import cache
from cashews.contrib.fastapi import (
CacheDeleteMiddleware,
CacheEtagMiddleware,
CacheRequestControlMiddleware,
)
app = FastAPI()
app.add_middleware(gzip.GZipMiddleware, minimum_size=100)
app.add_middleware(CacheDeleteMiddleware)
app.add_middleware(CacheEtagMiddleware)
app.add_middleware(CacheRequestControlMiddleware)
cache.setup("mem://")
@app.get("/")
@cache(ttl="5m")
async def root():
return Response(status_code=200)
if __name__ == "__main__":
import uvicorn
uvicorn.run(app, host="0.0.0.0", port=8000) |
Hey, the underlying issue happens only when caching RAW FastAPI response objects. As they are getting modified later in the chain by GZipMiddleware - additional headers are added. And as the cache is only maintaining references of the objects, they would get modified by it. One approach would be to make a copy of object placed into the cache. |
Thanks for explanation. And sure that we need to store a copy of object - I thought that it so ) , I gonna fix it |
Fixed with 7.1.0 version. Please reopen the issue if it still exist |
When cache is HIT, the response is 200 OK with empty body, in FASTAPI logs RuntimeExceptions:
The text was updated successfully, but these errors were encountered: