2021-10-20 23:29:31 +02:00
|
|
|
import os
|
2021-10-22 15:33:05 +02:00
|
|
|
from typing import Union
|
2021-10-20 23:29:31 +02:00
|
|
|
|
|
|
|
import dotenv
|
2021-10-22 15:33:05 +02:00
|
|
|
import aioredis
|
2021-08-31 03:08:07 +02:00
|
|
|
import redis
|
2021-10-22 15:33:05 +02:00
|
|
|
from redis_om.unasync_util import ASYNC_MODE
|
2021-08-31 03:08:07 +02:00
|
|
|
|
2021-10-20 23:29:31 +02:00
|
|
|
dotenv.load_dotenv()
|
|
|
|
|
|
|
|
URL = os.environ.get("REDIS_OM_URL", None)
|
2021-10-22 15:33:05 +02:00
|
|
|
if ASYNC_MODE:
|
|
|
|
client = aioredis.Redis
|
|
|
|
else:
|
|
|
|
client = redis.Redis
|
2021-10-20 23:29:31 +02:00
|
|
|
|
|
|
|
|
2021-10-22 15:33:05 +02:00
|
|
|
def get_redis_connection(**kwargs) -> Union[aioredis.Redis, redis.Redis]:
|
2021-10-20 23:29:31 +02:00
|
|
|
# If someone passed in a 'url' parameter, or specified a REDIS_OM_URL
|
|
|
|
# environment variable, we'll create the Redis client from the URL.
|
|
|
|
url = kwargs.pop("url", URL)
|
|
|
|
if url:
|
2021-10-22 15:33:05 +02:00
|
|
|
return client.from_url(url, **kwargs)
|
2021-10-20 23:29:31 +02:00
|
|
|
|
|
|
|
# Decode from UTF-8 by default
|
|
|
|
if "decode_responses" not in kwargs:
|
|
|
|
kwargs["decode_responses"] = True
|
2021-10-22 15:33:05 +02:00
|
|
|
return client(**kwargs)
|