async def __call__( self, config: Config = Depends(config_dependency)) -> Redis: """Creates the Redis pool if necessary and returns it.""" if not self.redis: password = config.redis_password self.redis = Redis.from_url(config.redis_url, password=password) assert self.redis return self.redis
def from_url(url, **kwargs): """ Returns an active Redis client generated from the given database URL. Will attempt to extract the database id from the path url fragment, if none is provided. """ from aioredis.client import Redis return Redis.from_url(url, **kwargs)
async def on_startup(): """Creates a ClientSession to be used app-wide.""" from api.services import redis, http if http.session is None or http.session.closed: http.session = ClientSession() log.info("Created HTTP ClientSession.") if redis.pool is None or redis.pool.connection is None: if (redis_uri := config.redis_uri()) is not None: redis.pool = Redis.from_url(redis_uri) log.info("Connected to redis server: " + str(redis.pool)) else: redis.pool = FakeRedis() log.warning( "\n" " > Created FakeRedis server, using a real redis server is suggested.\n" " > You can launch a local one using `docker compose up redis` and providing the url in env." )
import sys from string import ascii_uppercase from aioredis import Redis from loguru import logger from tenacity import retry, stop_after_delay, wait_exponential from . import config redis = Redis.from_url(config.REDIS_URL) async def startup() -> None: ''' Initialize resources such as Redis and Database connections ''' setup_logger() if config.DEBUG: show_config() await _init_redis() logger.info('started...') async def shutdown() -> None: ''' Release resources ''' await _stop_redis() logger.info('...shut down')