long2ice / fastapi-cache

fastapi-cache is a tool to cache fastapi response and function result, with backends support redis and memcached.
https://github.com/long2ice/fastapi-cache
Apache License 2.0
1.37k stars 167 forks source link

Cache doesnt doesnt "discriminate" between args #475

Open TC-CPHI opened 1 week ago

TC-CPHI commented 1 week ago

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 commented 1 week ago

@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
}