How to store and retrieve a dictionary with redis

前端 未结 11 1285
遇见更好的自我
遇见更好的自我 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 11:05

    An other way you can approach the matter:

    import redis
    conn = redis.Redis('localhost')
    
    v={'class':'user','grants': 0, 'nome': 'Roberto', 'cognome': 'Brunialti'}
    
    y=str(v)
    print(y['nome']) #<=== this return an error as y is actually a string
    conn.set('test',y)
    
    z=eval(conn.get('test'))
    print(z['nome']) #<=== this really works!
    

    I did not test it for efficiency/speed.

提交回复
热议问题