How to store and retrieve a dictionary with redis

前端 未结 11 1303
遇见更好的自我
遇见更好的自我 2020-12-07 10:20
# I have the dictionary my_dict
my_dict = {
    \'var1\' : 5
    \'var2\' : 9
}
r = redis.StrictRedis()

How would I store my_dict and retrieve it w

11条回答
  •  醉酒成梦
    2020-12-07 10:58

    One might consider using MessagePack which is endorsed by redis.

    import msgpack
    
    data = {
        'one': 'one',
        'two': 2,
        'three': [1, 2, 3]
    }
    
    await redis.set('my-key', msgpack.packb(data))
    val = await redis.get('my-key')
    print(msgpack.unpackb(val))
    
    # {'one': 'one', 'two': 2, 'three': [1, 2, 3]}
    

    Using msgpack-python and aioredis

提交回复
热议问题