Performance: Caching strategies
HIGH IMPACT
Caching strategies improve page load speed by reducing server processing and network delays for repeated requests.
from fastapi import FastAPI from fastapi_cache import FastAPICache from fastapi_cache.backends.inmemory import InMemoryBackend from fastapi_cache.decorator import cache app = FastAPI() @app.on_event('startup') async def startup(): FastAPICache.init(InMemoryBackend()) @app.get('/data') @cache(expire=60) async def get_data(): data = await fetch_data_from_db() return data
from fastapi import FastAPI app = FastAPI() @app.get('/data') async def get_data(): # Fetch fresh data every request data = await fetch_data_from_db() return data
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| No caching, fetch fresh data every request | Minimal DOM nodes | Multiple reflows due to slow response | High paint cost due to delayed content | [X] Bad |
| Cache API responses for repeated requests | Minimal DOM nodes | Single reflow with fast response | Low paint cost with quick content display | [OK] Good |