Issue
Looking for a reliable way of sharing connection objects in django project, similarly to django database connections.
from django.db import connections
with connections['default'].cursor() as cursor:
cursor.execute(sql, params)
Not necessarily with the same API but I need a similar way of acquiring redis connection pools which initializes on django server startup, so I could reuse single connection pool from different parts of the project.
Do you have any suggestions or ideas how can I approach this issue ?
Solution
Solution which worked for me was found in django CacheHandler
In this case for building REDIS connection redis-sentinel-url is used.
settings.py
- Redis connection string definitions in
REDIS = {
"default": "redis:///",
"secondary": "redis+sentinel:///"
}
RedisPoolHandler
class implementation similar to django CacheHandler
from threading import local
import redis_sentinel_url
from django.conf import settings
from django.core.cache import InvalidCacheBackendError
class RedisClientHandler:
"""
A Redis Client Handler to manage access to Redis instances.
Ensure only one instance of each alias exists per thread.
"""
def __init__(self):
self._clients = local()
def __getitem__(self, alias):
try:
return self._redis_clients.caches[alias]
except AttributeError:
self._redis_clients.caches = {}
except KeyError:
pass
if alias not in settings.REDIS:
raise InvalidCacheBackendError(
"Could not find config for '%s' in settings.REDIS" % alias
)
value = settings.REDIS[alias]
sentinel, redis_pool = redis_sentinel_url.connect(value, client_options={"decode_responses": True})
self._redis_clients.caches[alias] = redis_pool
return redis_pool
def all(self):
return getattr(self._redis_clients, '_redis_clients', {}).values()
redis_clients = RedisPoolHandler()
Usage example below:
from path.to.classfile import redis_clients
redis_client = redis_clients['default']
Answered By - demonno
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.