我试图在Redis中存储自定义的,可序列化的python对象,但遇到了一些奇怪的行为。 set
方法似乎起作用,但get
方法仅返回对象的__repr__
方法的值。例如...在Redis中存储自定义Python对象
import redis
# initialize the redis connection pool
rs = redis.Redis(host='localhost', port=6379)
# define a custom class
class SomeCustomObject(object):
pass
当我尝试设置SomeCustomObject
作为一种价值,它似乎工作:
>>> rs.set('c', SomeCustomObject())
True
然而,当我get
值回,它只是__repr__
字符串:
>>> rs.get('c')
'<__main__.SomeCustomObject object at 0x102496710>'
如何存储/取回实例?我没有太多的运气在the documentation找到这方面的任何信息,但我肯定不是第一个遇到这个问题的人?
你在哪里序列化对象? –