本以为这是一件再简单不过的事情
>>> import redis
>>> r = redis.Redis(host='localhost', port=6379, db=0)
>>> r.set('foo', 'bar')
True
>>> r.get('foo')
'bar'
后来发现,这只能取到 String 类型 的值, 对于 list , set, zset, 以及 hash 类型需要采用各自的方法:
import redis
redis = redis.Redis(host='localhost', port=6379, db=0)
redis.set('foo', 'bar')
keys = redis.keys()
for key in keys:
type = redis.type(key)
if type == b'string':
vals = redis.get(key)
elif type == b'list':
vals = redis.lrange(key, 0, -1)
# print(vals)
elif type == b'set':
vals = redis.smembers(key);
elif type == b'zset':
vals = redis.zrange(key, 0, -1)
elif type == b"hash":
vals = redis.hgetall(key)
else:
print(type, key)
print(vals)
[图片上传中...(_storage_emulated_0_tencent_MicroMsg_dcf316107e88b00dcebabf2f627777f4_image2_1d_fe_1dfe40e73e307ac9337fa42369732560_recompress.jpg-8a298b-1552902030420-0)]