How should two different modules foo.py
and bar.py
get a connection from a Redis connection pool? In other words, how should we structure the app?
I believe the goal is to have just a single connection pool for all modules to get a connection from.
Q1: In my example, does both modules get a connection from the same connection pool?
Q2: Is it Ok to create the RedisClient instance in RedisClient.py
, then import the instance into the other 2 modules? Or is there a better way?
Q3: Is lazy loading of the conn
instance variable actually useful?
RedisClient.py
import redis
class RedisClient(object):
def __init__(self):
self.pool = redis.ConnectionPool(host = HOST, port = PORT, password = PASSWORD)
@property
def conn(self):
if not hasattr(self, '_conn'):
self.getConnection()
return self._conn
def getConnection(self):
self._conn = redis.Redis(connection_pool = self.pool)
redisClient = RedisClient()
foo.py
from RedisClient import redisClient
species = 'lion'
key = 'zoo:{0}'.format(species)
data = redisClient.conn.hmget(key, 'age', 'weight')
print(data)
bar.py
from RedisClient import redisClient
print(redisClient.conn.ping())
Or is this better?
RedisClient.py
import redis
class RedisClient(object):
def __init__(self):
self.pool = redis.ConnectionPool(host = HOST, port = PORT, password = PASSWORD)
def getConnection(self):
return redis.Redis(connection_pool = self.pool)
redisClient = RedisClient()
foo.py
from RedisClient import redisClient
species = 'lion'
key = 'zoo:{0}'.format(species)
data = redisClient.getConnection().hmget(key, 'age', 'weight')
print(data)
bar.py
from RedisClient import redisClient
print(redisClient.getConnection().ping())