⚡️ Use Redis to cache REST responses
This commit is contained in:
@@ -1,10 +1,12 @@
|
||||
from os import environ
|
||||
|
||||
from fastapi_cache.backends.redis import RedisBackend
|
||||
from redis import asyncio as aioredis
|
||||
from yaml import safe_load
|
||||
|
||||
from backend.db import db
|
||||
from backend.idfm_interface import IdfmInterface
|
||||
from backend.settings import Settings
|
||||
from backend.settings import CacheSettings, Settings
|
||||
|
||||
|
||||
CONFIG_PATH = environ.get("CONFIG_PATH", "./config.sample.yaml")
|
||||
@@ -20,3 +22,17 @@ def load_settings(path: str) -> Settings:
|
||||
settings = load_settings(CONFIG_PATH)
|
||||
|
||||
idfm_interface = IdfmInterface(settings.idfm_api_key.get_secret_value(), db)
|
||||
|
||||
|
||||
def init_redis_backend(settings: CacheSettings) -> RedisBackend:
|
||||
login = f"{settings.user}:{settings.password}@" if settings.user is not None else ""
|
||||
|
||||
url = f"redis://{login}{settings.host}:{settings.port}"
|
||||
|
||||
redis_connections_pool = aioredis.from_url(
|
||||
url, encoding="utf8", decode_responses=True
|
||||
)
|
||||
return RedisBackend(redis_connections_pool)
|
||||
|
||||
|
||||
redis_backend = init_redis_backend(settings.cache)
|
||||
|
Reference in New Issue
Block a user