Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork8.3k
Caching best practices#14404
-
First Check
Commit to Help
Example CodeclassCacheManager(CacheBase):asyncdefget_or_cache(self,key:str,fetch_func:callable,to_cache_mapper:callable,from_cache_mapper:callable,ttl:int=3600):""" Generic: check cache by key, if found -> from_cache_mapper -> pydantic model if not found -> fetch_func -> to_cache_mapper -> cache -> return pydantic model :param key: cache key :param ttl: time to live in seconds :param fetch_func: async () -> model :param to_cache_mapper: model -> dict :param from_cache_mapper: dict -> model """cached=awaitself.get_cached_data(key)ifcached:logger.debug(f"{key} from cache")returnfrom_cache_mapper(cached)logger.debug(f"Fetching{key} from DB")model=awaitfetch_func()ifnotmodel:returnNonedata_to_cache=to_cache_mapper(model)# -> dictawaitself.cache_data(key,data_to_cache,ttl)logger.info(f"{key} cached")returnfrom_cache_mapper(data_to_cache)# -> pydantic# and usage likelanguages_list=awaitself._cache_manager.get_or_cache(key=self._cache_all_key,fetch_func=fetch_all,to_cache_mapper=PLanguageMapper.language_models_to_cache,from_cache_mapper=PLanguageMapper.cache_many_to_response,ttl=3600 ) DescriptionBest way to implement a clean caching layer in a mid-size fast api (up to 20 users per sec) ? I started with custom decorators and a CacheManager class, but it’s becoming hard to maintain and use... What should I do in my case? Many routes have structure like -> if cache - return, else (db query) + cache - return. Should I use aiocache[redis] instead of my custom classes? Thank you! Operating SystemWindows Operating System DetailsNo response FastAPI Version0.122.0 Pydantic Version2.12.4 Python Version3.12 Additional ContextNo response |
BetaWas this translation helpful?Give feedback.
All reactions
Replies: 1 comment
-
Hi@endingofourlife, I recommend usingfastapi-cache2 with Redis. It handles the serialization and deserialization of Pydantic models, eliminating your need for manual mappers and boilerplate Setup (in your main file) fromcontextlibimportasynccontextmanagerfromfastapiimportFastAPIfromfastapi_cacheimportFastAPICachefromfastapi_cache.backends.redisimportRedisBackendfromredisimportasyncioasaioredis@asynccontextmanagerasyncdeflifespan(app:FastAPI):# Initialize connection to Redisredis=aioredis.from_url("redis://localhost")FastAPICache.init(RedisBackend(redis),prefix="fastapi-cache")yieldapp=FastAPI(lifespan=lifespan) |
BetaWas this translation helpful?Give feedback.