Python redis.ttl 返回 None
2017-12-07 12:02
711 查看
今天在 Python 中使用 redis 遇到一个奇怪的问题。
怎么会是 None 呢?去 redis 里面看看
ttl 有值啊,这就奇怪了。
Google 后找到应该这样使用:
那么问题来了
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 类。
一. 问题描述
>>> 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
那么问题来了
Redis和
StrictRedis究竟有什么区别?
二. 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 类。
相关文章推荐
- Python3基础 Python的函数都有返回值 无指定返回值的函数 返回NONE对象
- 关于Python的列表append()返回None对象
- python return 返回None
- Python xml.dom.minidom nodeValue 始终返回none
- Python3基础 filter()第一个参数为NONE时 结果只返回为True的对象
- redis学习(1)python连接redis
- python根据日期返回星期几的方法
- 【13】python列表去重/寻找一个文件夹中的所有文件/函数返回多个值
- lepus监控redis执行python check_redis.py报错
- Python菜鸟之路:Python基础-操作缓存memcache、redis
- python is not None
- python对缓存(memcached,redis)的操作
- python代码 if not x: 和 if x is not None: 和 if not x is None:使用介绍
- Python之 is None VS == None
- python 解接口返回的json字符串
- python3 MySQL 查询返回字典类型
- Python标准库:内置函数eval(expression, globals=None, locals=None)
- python中字典不自动排序/删除指定类型文件/执行可执行文件的返回值
- Python之redis(三)
- python 函数返回多个值