Reference

RedisHandler

Source code in quart_redis/__init__.py
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
class RedisHandler:
    _connection: Optional[Redis] = None
    conn_key = "REDIS_URI"
    conn_attempts_key = "REDIS_CONN_ATTEMPTS"

    def __init__(self, app: Optional[Quart] = None, **kwargs):
        """
        calls init_app() automatically,
        with any given kwargs if app is given.

            :param app: the quart app, defaults to None
            :param **kwargs: pass arguments to init_app()
        """
        if app is not None:
            self.init_app(app, **kwargs)

    @staticmethod
    async def __attempt_to_connect(conn: Redis, attempts: int):
        for attempt in range(1, attempts + 1):
            try:
                await conn.ping()
                logger.debug("Redis connection established")
                break
            except RedisError:
                logger.warning(
                    f"Redis connection attempt {attempt} of {attempts} failed"
                )
                if attempt == attempts:
                    logger.critical("Redis connection failed")
                    raise
                await asyncio.sleep(2)

    def init_app(self, app: Quart, **kwargs):
        """
        get config from quart app
        then setup connection handlers.

            :param app: the quart app
            :param **kwargs: pass any further arguments to redis
        """
        conn_uri = app.config[self.conn_key]
        conn_attempts = app.config.get(self.conn_attempts_key, 3)

        @app.before_serving
        async def init_redis():
            RedisHandler._connection = from_url(
                conn_uri,
                **kwargs
            )
            if conn_attempts >= 0:
                await self.__attempt_to_connect(
                    RedisHandler._connection,
                    conn_attempts,
                )
            logger.info("Redis started")

        @app.after_serving
        async def close_redis():
            if RedisHandler._connection is not None:
                await RedisHandler._connection.close()
                logger.info("Redis shutdown")

    @classmethod
    def get_connection(cls) -> Redis:
        """
        get the shared redis connection

            :raises RedisNotInitialized: if redis has not been initialized
        """
        if cls._connection is None:
            raise RedisNotInitialized("Redis has not been initialized")
        return cls._connection

__init__(app=None, **kwargs)

calls init_app() automatically, with any given kwargs if app is given.

:param app: the quart app, defaults to None
:param **kwargs: pass arguments to init_app()
Source code in quart_redis/__init__.py
23
24
25
26
27
28
29
30
31
32
def __init__(self, app: Optional[Quart] = None, **kwargs):
    """
    calls init_app() automatically,
    with any given kwargs if app is given.

        :param app: the quart app, defaults to None
        :param **kwargs: pass arguments to init_app()
    """
    if app is not None:
        self.init_app(app, **kwargs)

get_connection() classmethod

get the shared redis connection

:raises RedisNotInitialized: if redis has not been initialized
Source code in quart_redis/__init__.py
80
81
82
83
84
85
86
87
88
89
@classmethod
def get_connection(cls) -> Redis:
    """
    get the shared redis connection

        :raises RedisNotInitialized: if redis has not been initialized
    """
    if cls._connection is None:
        raise RedisNotInitialized("Redis has not been initialized")
    return cls._connection

init_app(app, **kwargs)

get config from quart app then setup connection handlers.

:param app: the quart app
:param **kwargs: pass any further arguments to redis
Source code in quart_redis/__init__.py
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
def init_app(self, app: Quart, **kwargs):
    """
    get config from quart app
    then setup connection handlers.

        :param app: the quart app
        :param **kwargs: pass any further arguments to redis
    """
    conn_uri = app.config[self.conn_key]
    conn_attempts = app.config.get(self.conn_attempts_key, 3)

    @app.before_serving
    async def init_redis():
        RedisHandler._connection = from_url(
            conn_uri,
            **kwargs
        )
        if conn_attempts >= 0:
            await self.__attempt_to_connect(
                RedisHandler._connection,
                conn_attempts,
            )
        logger.info("Redis started")

    @app.after_serving
    async def close_redis():
        if RedisHandler._connection is not None:
            await RedisHandler._connection.close()
            logger.info("Redis shutdown")

get_redis()

get the shared redis connection

:raises RedisNotInitialized: if redis has not been initialized
Source code in quart_redis/__init__.py
92
93
94
95
96
97
98
def get_redis() -> Redis:
    """
    get the shared redis connection

        :raises RedisNotInitialized: if redis has not been initialized
    """
    return RedisHandler.get_connection()