GithubHelp home page GithubHelp logo

Comments (5)

AndreyKuchko avatar AndreyKuchko commented on June 21, 2024 1

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.

Krukov avatar Krukov commented on June 21, 2024 1

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.

Krukov avatar Krukov commented on June 21, 2024

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.

sergey-burkov-trailstone avatar sergey-burkov-trailstone commented on June 21, 2024

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.

Krukov avatar Krukov commented on June 21, 2024

Fixed with 7.1.0 version. Please reopen the issue if it still exist

from cashews.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.