Comments (5)
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 CacheRequestControlMiddleware
stores decompressed response body with headers from compressed response(it contains Content-Encoding: gzip
) as a cached page to the memory. On the next request GZipMiddleware
ignores compressing the response body, because according to Content-Encoding
it assumes that it is already done.
If I use redis
backed for the cache instead of mem
, the same code works without any problems. As I see in this case it stores correct(without Content-Encoding: gzip
) response headers together with decompressed response body as a cache page.
I hope this information will help with investigation.
from cashews.
Thanks for explanation. And sure that we need to store a copy of object - I thought that it so ) , I gonna fix it
from cashews.
Sorry for a delay
I spend some time trying to reproduce it but can't
Can you help me with a code?
I have a next snippet but it works as expected
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)
from cashews.
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.
from cashews.
Fixed with 7.1.0 version. Please reopen the issue if it still exist
from cashews.
Related Issues (20)
- CacheEtagMiddleware breaks with redis client_side cache HOT 1
- Counter in Cache condition
- cache_control_ttl is undocumented HOT 1
- Documenting with sphinx? HOT 1
- ValueError: Duplicated timeseries in CollectorRegistry HOT 2
- early_ttl not working in combination with CacheEtagMiddleware HOT 5
- Gracefully close a connection pool with redis > 5.0.1
- Cannot close Redis connection - AttributeError: 'Redis' object has no attribute '_client' HOT 4
- JSON serializer HOT 5
- Early cache with sqlalchemy & fastapi depend (sqlalchemy.exc.IllegalStateChangeError: Method 'close()' can't be called here; method '_connection_for_bind()' is already in progress and this would cause an unexpected state change to <SessionTransactionState.CLOSED: 5>) HOT 1
- Formatter Called with Formatted Value HOT 6
- CacheRequestControlMiddleware: Backend not found exception with non-default cache prefix HOT 3
- cache_control_ttl can't be used directly with cache api HOT 1
- thunder_protection with multiple event loops breaks HOT 3
- re.error: bad character in group name when using custom formatter in a tag HOT 3
- Unhandled exception when there are only backends with prefixes HOT 1
- Feature: Get or set
- _empty and _empty_in_redis marker objects leak HOT 1
- Redis Client Side Connection Timeout HOT 3
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from cashews.