fastapi-cache
fastapi-cache copied to clipboard
Is there a lock when cached data is preparing?
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?
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