我正在尝试将一个自定义的、可序列化的Python对象存储在Redis中,但遇到了一些奇怪的行为。 set
方法似乎可以工作,但 get
方法只返回对象的 __repr__
方法的值。例如...
import redis
# initialize the redis connection pool
rs = redis.Redis(host='localhost', port=6379)
# define a custom class
class SomeCustomObject(object):
pass
当我尝试将 SomeCustomObject 设置为值时,它似乎有效:
<code>>>> rs.set('c', SomeCustomObject())
True
</code>
然而,当我获取值时,它只是__repr__
字符串:
>>> rs.get('c')
'<__main__.SomeCustomObject object at 0x102496710>'
我应该如何存储/获取实例?在文档中,我没有找到有关此问题的信息,但我肯定不是第一个遇到这个问题的人吧?