Nyx*_*nyx 5 python database-connection redis python-3.x redis-py
应该两个不同的模块如何foo.py和bar.py获得从Redis的连接池中的连接?换句话说,我们应该如何构建应用程序?
我相信目标是让所有模块只有一个连接池来获取连接。
Q1:在我的例子中,两个模块是否从同一个连接池中获得连接?
Q2:在 中创建 RedisClient 实例RedisClient.py,然后将实例导入其他 2 个模块是否可以?或者,还有更好的方法?
Q3:conn实例变量的延迟加载真的有用吗?
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()
Run Code Online (Sandbox Code Playgroud)
文件
from RedisClient import redisClient
species = 'lion'
key = 'zoo:{0}'.format(species)
data = redisClient.conn.hmget(key, 'age', 'weight')
print(data)
Run Code Online (Sandbox Code Playgroud)
酒吧.py
from RedisClient import redisClient
print(redisClient.conn.ping())
Run Code Online (Sandbox Code Playgroud)
或者这样更好?
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()
Run Code Online (Sandbox Code Playgroud)
文件
from RedisClient import redisClient
species = 'lion'
key = 'zoo:{0}'.format(species)
data = redisClient.getConnection().hmget(key, 'age', 'weight')
print(data)
Run Code Online (Sandbox Code Playgroud)
酒吧.py
from RedisClient import redisClient
print(redisClient.getConnection().ping())
Run Code Online (Sandbox Code Playgroud)
A1:是的,它们使用相同的连接池。
A2:这不是一个好习惯。因为您无法控制此实例的初始化。另一种方法是使用单例。
import redis
class Singleton(type):
"""
An metaclass for singleton purpose. Every singleton class should inherit from this class by 'metaclass=Singleton'.
"""
_instances = {}
def __call__(cls, *args, **kwargs):
if cls not in cls._instances:
cls._instances[cls] = super(Singleton, cls).__call__(*args, **kwargs)
return cls._instances[cls]
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)
Run Code Online (Sandbox Code Playgroud)
然后RedisClient将是一个单例类。不管你调用多少次client = RedisClient(),你都会得到相同的对象。
所以你可以像这样使用它:
from RedisClient import RedisClient
client = RedisClient()
species = 'lion'
key = 'zoo:{0}'.format(species)
data = client.conn.hmget(key, 'age', 'weight')
print(data)
Run Code Online (Sandbox Code Playgroud)
第一次调用client = RedisClient()时实际上会初始化这个实例。
或者您可能希望根据不同的参数获得不同的实例:
class Singleton(type):
"""
An metaclass for singleton purpose. Every singleton class should inherit from this class by 'metaclass=Singleton'.
"""
_instances = {}
def __call__(cls, *args, **kwargs):
key = (args, tuple(sorted(kwargs.items())))
if cls not in cls._instances:
cls._instances[cls] = {}
if key not in cls._instances[cls]:
cls._instances[cls][key] = super(Singleton, cls).__call__(*args, **kwargs)
return cls._instances[cls][key]
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6790 次 |
| 最近记录: |