blob: fdf1d6fe85250430a84e99fedadae79c69027726 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
"""Connections for Redis."""
import logging
import traceback
import contextlib
from typing import Iterator
from redis import Redis, RedisError, ConnectionError as RedisConnectionError
@contextlib.contextmanager
def connection(redis_uri) -> Iterator[Redis]:
"""Connection to redis"""
rconn = Redis.from_url(redis_uri, decode_responses=True)
try:
if not rconn.ping():
raise RedisConnectionError("Could not connect to Redis.")
yield rconn
except RedisError as _rerr:
logging.debug(traceback.format_exc())
raise
finally:
rconn.disconnect()
rconn.close()
|