如何使用 Redis 将多个值添加到一个键中

我一直在试图弄清楚如何将多个值放入一个键中,例如:


{

    "fruit": {

        "tomato": {

            "Color": "red",

            "Price": "100"

        },

        "banana": {

            "Color": "yellow",

            "Price": "150"

        }

    }

}

现在我当前的代码是:


r = serialized_redis.MsgpackSerializedRedis(host='localhost', port=6379, db=0)

r.set("fruit", {"tomato": {"Color": "red", "Price": "100"}})

r.set("fruit", {"banana": {"Color": "yellow", "Price": "150"}})

问题是,每次我执行 r.set 时,它似乎都会替换,这意味着当我运行此代码时,它只会被设置为:


{

    "fruit": {

        "banana": {

            "Color": "yellow",

            "Price": "150"

        }

    }

}

所以即使我做了一组“番茄”,它也会被“香蕉”取代,因为它是正在设置的最新一个。


我的问题是,如何将它添加到同一个键但具有不同的值,以便所有内容都在同一个键中但具有不同的“furits”?


{

    "fruit": {

        "tomato": {

            "Color": "red",

            "Price": "100"

        },

        "banana": {

            "Color": "yellow",

            "Price": "150"

        }

    }

}


红糖糍粑
浏览 140回答 1
1回答

拉丁的传说

你可以使用哈希值hmset fruit tomato your_json_serialized_here hmset fruit orange ...hmset你也可以像这样用多种水果来做hmset fruit apple 1 banana 2 orange 3使用 hmget 从哈希中访问数据就像这样hmget fruit orange banana apple
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python