How to store and retrieve a dictionary with redis

前端 未结 11 1306
遇见更好的自我
遇见更好的自我 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条回答
  •  旧时难觅i
    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

提交回复
热议问题