How to store and retrieve a dictionary with redis

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

    If you don't know exactly how to organize data in Redis, I did some performance tests, including the results parsing. The dictonary I used (d) had 437.084 keys (md5 format), and the values of this form:

    {"path": "G:\tests\2687.3575.json",
     "info": {"f": "foo", "b": "bar"},
     "score": 2.5}
    

    First Test (inserting data into a redis key-value mapping):

    conn.hmset('my_dict', d)  # 437.084 keys added in 8.98s
    
    conn.info()['used_memory_human']  # 166.94 Mb
    
    for key in d:
        json.loads(conn.hget('my_dict', key).decode('utf-8').replace("'", '"'))
        #  41.1 s
    
    import ast
    for key in d:
        ast.literal_eval(conn.hget('my_dict', key).decode('utf-8'))
        #  1min 3s
    
    conn.delete('my_dict')  # 526 ms
    

    Second Test (inserting data directly into Redis keys):

    for key in d:
        conn.hmset(key, d[key])  # 437.084 keys added in 1min 20s
    
    conn.info()['used_memory_human']  # 326.22 Mb
    
    for key in d:
        json.loads(conn.hgetall(key)[b'info'].decode('utf-8').replace("'", '"'))
        #  1min 11s
    
    for key in d:
        conn.delete(key)
        #  37.3s
    

    As you can see, in the second test, only 'info' values have to be parsed, because the hgetall(key) already returns a dict, but not a nested one.

    And of course, the best example of using Redis as python's dicts, is the First Test

    0 讨论(0)
  • 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

    0 讨论(0)
  • 2020-12-07 11:03

    As the basic answer has already give by other people, I would like to add some to it.

    Following are the commands in REDIS to perform basic operations with HashMap/Dictionary/Mapping type values.

    1. HGET => Returns value for single key passed
    2. HSET => set/updates value for the single key
    3. HMGET => Returns value for single/multiple keys passed
    4. HMSET => set/updates values for the multiple key
    5. HGETALL => Returns all the (key, value) pairs in the mapping.

    Following are their respective methods in redis-py library :-

    1. HGET => hget
    2. HSET => hset
    3. HMGET => hmget
    4. HMSET => hmset
    5. HGETALL => hgetall

    All of the above setter methods creates the mapping, if it doesn't exists. All of the above getter methods doesn't raise error/exceptions, if mapping/key in mapping doesn't exists.

    Example:
    =======
    In [98]: import redis
    In [99]: conn = redis.Redis('localhost')
    
    In [100]: user = {"Name":"Pradeep", "Company":"SCTL", "Address":"Mumbai", "Location":"RCP"}
    
    In [101]: con.hmset("pythonDict", {"Location": "Ahmedabad"})
    Out[101]: True
    
    In [102]: con.hgetall("pythonDict")
    Out[102]:
    {b'Address': b'Mumbai',
     b'Company': b'SCTL',
     b'Last Name': b'Rajpurohit',
     b'Location': b'Ahmedabad',
     b'Name': b'Mangu Singh'}
    
    In [103]: con.hmset("pythonDict", {"Location": "Ahmedabad", "Company": ["A/C Pri
         ...: sm", "ECW", "Musikaar"]})
    Out[103]: True
    
    In [104]: con.hgetall("pythonDict")
    Out[104]:
    {b'Address': b'Mumbai',
     b'Company': b"['A/C Prism', 'ECW', 'Musikaar']",
     b'Last Name': b'Rajpurohit',
     b'Location': b'Ahmedabad',
     b'Name': b'Mangu Singh'}
    
    In [105]: con.hget("pythonDict", "Name")
    Out[105]: b'Mangu Singh'
    
    In [106]: con.hmget("pythonDict", "Name", "Location")
    Out[106]: [b'Mangu Singh', b'Ahmedabad']
    

    I hope, it makes things more clear.

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 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)
    
    0 讨论(0)
提交回复
热议问题