fastapi-cache icon indicating copy to clipboard operation
fastapi-cache copied to clipboard

Is there a lock when cached data is preparing?

Open PaleNeutron opened this issue 1 year ago • 2 comments

for example, if function A is decorated and will take 10s and 1 core CPU to return, what happens the function is called 10 times in in 1s?

Does the function actually run 10 times? Is there a lock when cached data is preparing?

PaleNeutron avatar Apr 03 '24 04:04 PaleNeutron

Does the function actually run 10 times?

Yes, you can try it for yourself:

import asyncio

from contextlib import asynccontextmanager
from fastapi import FastAPI

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


@asynccontextmanager
async def lifespan(_):
    FastAPICache.init(InMemoryBackend(), prefix="fastapi-cache")
    yield


app = FastAPI(lifespan=lifespan)

@app.get("/cached-route")
@cache(expire=60)
async def cached_route():
    print('Function invoked')
    await asyncio.sleep(10)
    return dict(hello="world")

If you call this 10 times before the result is cached, you will see Function invoked 10 times in the logs

0xTiger avatar Jul 26 '24 17:07 0xTiger