小编Ale*_*exK的帖子

是否可以将嵌套字典作为哈希结构存储在Redis中?

我正在尝试使用 HSET 将嵌套字典存储为哈希,但这似乎不可能。

一个不起作用的例子:

from redis import StrictRedis
foo = {
    "host_data": {
        "hostname": "some_host",
        "mac_address": "82:fa:8e:63:40:05",
        "root_password": {
            "is_crypted": True,
            "password": "sha512_password"
        },
        "network": {
            "ip_address": "192.168.0.10/24",
            "default_gateway": "192.168.0.1",
            "vmnic": "vmnic3",
            "vlan": 20
        },
        "dns_servers": [
            "dns01.local",
            "dns02.local"
        ]
    },
    "installation_type": "esxi",
    "image_data": {
        "type": "vCenter_contentlib",
        "host": "vcenter01",
        "credential": {
            "username": "some_user",
            "password": "some_password"
        },
        "content_library": "the_content_lib_name",
        "image_name": "some_image"
    },
    "host_short_name": "esxi021"
}

redis_connection = StrictRedis(host='localhost', port=6379, charset="utf-8", decode_responses=True)
redis_connection.hset("test", mapping=foo)
Run Code Online (Sandbox Code Playgroud)

抛出以下错误:

Traceback (most recent call last):
  File …
Run Code Online (Sandbox Code Playgroud)

python redis redis-py

2
推荐指数
1
解决办法
4970
查看次数

标签 统计

python ×1

redis ×1

redis-py ×1