文章内容
2023/2/13 11:23:05,作 者: 黄兵
redis.Redis 和 redis.StrictRedis 的区别
最近在优化代码的时候,看到连接 Redis 数据库优如下写法:
class RedisOperation(object):
def __new__(cls):
if not hasattr(cls, 'instance'):
get_env_config = os.getenv('FLASK_CONFIG') or 'default'
get_config = app.config.get(get_env_config)
pool = redis.ConnectionPool(host=get_config.REDIS_HOST,
port=int(get_config.REDIS_PORT),
password=get_config.REDIS_PASSWORD,
decode_responses=get_config.DECODE_RESPONSES)
cls.instance = redis.StrictRedis(connection_pool=pool)
return cls.instance上面使用了单例模式来连接数据库,最后使用 redis.StrictRedis() 我看了一下也可以使用 redis.Redis() 的方式连接数据库。
两者有什么区别呢?
通过查询其他人对此问题的回答,总结如下:
他们两者是等价的。
redis-py 3.0 放弃了对遗留“Redis”客户端类的支持。“StrictRedis”已更名为“Redis”,并提供一个名为“StrictRedis”的别名,以便之前使用“StrictRedis”的用户可以继续运行不变。
当然更多的细节,可以参考下面的参考资料。
参考资料:
1、redis-py : What's the difference between StrictRedis() and Redis()?
黄兵个人博客原创。
评论列表