Skip to content
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 doesnt doesnt "discriminate" between args #475

Open
TC-CPHI opened this issue Nov 11, 2024 · 1 comment
Open

Cache doesnt doesnt "discriminate" between args #475

TC-CPHI opened this issue Nov 11, 2024 · 1 comment
Labels
question Further information is requested

Comments

@TC-CPHI
Copy link

TC-CPHI commented Nov 11, 2024

Hello,

Im trying to implement an in memory cache which looks at the following function. The goal is to have a cache for the same types of fromdate and todate queries that are called with the function. Yet when implementing an in memory cache as follows it always returns the same query results despite the args of the function changing to new fromdate and todate's. I've tried with custom key builders and various approaches but it seems that the cache itself is flawed?

@asynccontextmanager
async def lifespan(app: FastAPI):
    FastAPICache.init(InMemoryBackend())
    yield

app = FastAPI(lifespan=lifespan)


@app.get("/InitInt")
@cache(expire=3600)
async def InitializeInternal(api_key_fetch: str = Depends(header_scheme), fromdate : Annotated[str, Header()] = None, todate : Annotated[str, Header()] = None, offset : Annotated[int, Header()] = None):

#SOME SQL CODE
@vicchi
Copy link
Collaborator

vicchi commented Nov 11, 2024

@TC-CPHI Using an admittedly cut down example, I can't replicate what you're seeing ...

Could you provide a reproducible test case and I'll take a look ... as it stands right now, your example above has too many things undefined for me to use.

from contextlib import asynccontextmanager
from typing import Dict

from fastapi import FastAPI, Request
import uvicorn

from fastapi_cache import FastAPICache
from fastapi_cache.backends.inmemory import InMemoryBackend
from fastapi_cache.decorator import cache

@asynccontextmanager
async def lifespan(app: FastAPI):
    FastAPICache.init(InMemoryBackend())
    yield

app = FastAPI(lifespan=lifespan)


@app.get("/foo")
@cache(expire=3600)
async def foo(bar: int) -> Dict[str, int]:
    return {
        'bar': bar
    }

if __name__ == "__main__":
    uvicorn.run("cachetest:app", reload=True)
$ http get localhost:8000/foo?bar=1
HTTP/1.1 200 OK
cache-control: max-age=3600
content-length: 9
content-type: application/json
date: Mon, 11 Nov 2024 13:20:39 GMT
etag: W/-5122841640995295752
server: uvicorn
x-fastapi-cache: MISS

{
    "bar": 1
}
$ http get localhost:8000/foo?bar=1
HTTP/1.1 200 OK
cache-control: max-age=3596
content-length: 9
content-type: application/json
date: Mon, 11 Nov 2024 13:20:43 GMT
etag: W/-5122841640995295752
server: uvicorn
x-fastapi-cache: HIT

{
    "bar": 1
}

@vicchi vicchi added the question Further information is requested label Nov 11, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants