我试图在Redis中存储一个自定义的、可序列化的python对象,但是遇到了一些奇怪的行为。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
设置为一个值时,它似乎可以工作:
但是,当我get
返回值时,它只是__repr__
字符串:
>>> rs.get('c')
'<__main__.SomeCustomObject object at 0x102496710>'
如何存储/获取实例?我在the documentation中没有找到任何关于这方面的信息,但我肯定不是第一个遇到这种情况的人?在
使用Pickle
使用pickle模块,您可以序列化和反序列化Python对象,并将它们传递给Redis。在
从这个so答案-https://stackoverflow.com/a/20400288/4403600可以看出:
进一步阅读-https://docs.python.org/2/library/pickle.html
相关问题 更多 >
编程相关推荐