Python redis.ttl 返回 None

今天在 Python 中使用 redis 遇到一个奇怪的问题。

一. 问题描述

>>> import redis
>>> >>> r = redis.Redis(host='localhost', port=6379)
>>> print r.get('foo')
bar
>>> print r.ttl('foo')
None

怎么会是 None 呢?去 redis 里面看看

127.0.0.1:6379> get foo
"bar"
127.0.0.1:6379> ttl foo
(integer) -1

ttl 有值啊,这就奇怪了。

Google 后找到应该这样使用:

>>> r = redis.StrictRedis(host='localhost', port=6379, db=0)
>>> print r.ttl('foo')
-1

那么问题来了RedisStrictRedis 究竟有什么区别?

二. Redis 和 StrictRedis 的联系与区别

class StrictRedis(builtin.object)

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

StrictRedis 类实现了 Redis 协议的全部命令。

我们再来看看 Redis 类:

class Redis(StrictRedis)

Provides backwards compatibility with older versions of redis-py that changed arguments to some commands to be more Pythonic, sane, or by accident.

Redis 类继承自 StrictRedis,提供了向后兼容。

If you’re looking to use the standard syntax, consider using the StrictRedis class.

如果要使用标准语法的话,官方推荐 StrictRedis 类。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值