微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

python redis包中的StrictRedis 和Redis的区别

redis-py提供两个类Redis和StrictRedis用于实现Redis的命令,StrictRedis用于实现大部分官方的命令,并使用官方的语法和命令(比如,SET命令对应与StrictRedis.set方法)。Redis是StrictRedis子类,用于向后兼容旧版本的redis-py。 简单说,官方推荐使用StrictRedis方法

不推荐Redis类,原因是他和咱们在redis-cli操作有些不一样,主要不一样是下面这三个方面。

·LREM:参数‘num’和‘value’的顺序交换了一下,cli是 lrem queueName 0 ‘string’ 。 这里的0时所有的意思。 但是Redis这个类,把控制和string调换了。

·ZADD:实现时score和value的顺序不小心弄反了,后来有人用了,就这样了

·SETEX:time和value的顺序反了

.Pool:连接池

Redis的连接池的方法:

pool = redis.ConnectionPool(host='localhost', port=6379, db=0)

r = redis.Redis(connection_pool=pool)

StrictRedis的连接池的实现方式:

In [4]: pool = redis.ConnectionPool(host='127.0.0.1', port=6379)

In [5]: r = redis.StrictRedis(connection_pool=pool)

官方的创建redis的时候,都可以添加什么参数。

class redis.StrictRedis(host='localhost', port=6379, db=0, password=None, socket_timeout=None, connection_pool=None, charset='utf-8', errors='strict', decode_responses=False, unix_socket_path=None)

Implementation of the Redis protocol.

This abstract class provides a Python interface to all Redis commands and an implementation of the Redis protocol.

Connection and Pipeline derive from this, implementing how the commands are sent and received to the Redis server

redis的对于有些编码入库的问题,redis的连接附加的参数里面,认编码是utf-8,但是如果你非要用GBK那就需要指明你的chardet和decode_responses为True 。

转载于:https://www.cnblogs.com/sundahua/p/10247812.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐