How to store and retrieve a dictionary with redis

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

    Try rejson-py which is relatively new since 2017. Look at this introduction.

    from rejson import Client, Path
    
    rj = Client(host='localhost', port=6379)
    
    # Set the key `obj` to some object
    obj = {
        'answer': 42,
        'arr': [None, True, 3.14],
        'truth': {
            'coord': 'out there'
        }
    }
    rj.jsonset('obj', Path.rootPath(), obj)
    
    # Get something
    print 'Is there anybody... {}?'.format(
        rj.jsonget('obj', Path('.truth.coord'))
    )
    
    # Delete something (or perhaps nothing), append something and pop it
    rj.jsondel('obj', Path('.arr[0]'))
    rj.jsonarrappend('obj', Path('.arr'), 'something')
    print '{} popped!'.format(rj.jsonarrpop('obj', Path('.arr')))
    
    # Update something else
    rj.jsonset('obj', Path('.answer'), 2.17)
    

提交回复
热议问题