如何在redis中存储一个复杂的对象(使用redis-py) [英] how to store a complex object in redis (using redis-py)

查看:45
本文介绍了如何在redis中存储一个复杂的对象(使用redis-py)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

hmset函数可以设置每个字段的值,但是我发现如果值本身是一个复杂的结构化对象,hget返回的值是一个序列化的字符串,而不是原始对象

The hmset function can set the value of each field, but I found that if the value itself is a complex structured object, the value return from hget is a serialized string, not the original object

例如

images= [{'type':'big', 'url':'....'},
     {'type':'big', 'url':'....'},
     {'type':'big', 'url':'....'}]   

redis = Redis()
redis.hset('photo:1', 'images', images)

i = redis.hget('photo:1', 'images')
print type(i)

i 的类型是字符串,不是python 对象,除了手动解析每个字段,有没有什么办法可以解决这个问题?

the type of i is a string, not a python object, is there any way to solve this problem besides manually parse each fields?

推荐答案

您无法在 Redis 中创建嵌套结构,这意味着您无法(例如)将本机 redis 列表存储在本机 redis 哈希映射中.

You can't create nested structures in Redis, meaning you can't (for example) store a native redis list inside a native redis hash-map.

如果你真的需要嵌套结构,你可能只想存储一个 JSON-blob(或类似的东西).另一种选择是将id"/key 作为映射键的值存储到不同的 redis 对象中,但这需要多次调用服务器才能获取完整对象.

If you really need nested structures, you might want to just store a JSON-blob (or something similar) instead. Another option is to store an "id"/key to a different redis object as the value of the map key, but that requires multiple calls to the server to get the full object.

这篇关于如何在redis中存储一个复杂的对象(使用redis-py)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆