How to append a dictionary in a Redis cache?

Viewed 5403

I want to append a dictionary in a Redis cache.

I don't have any problem with adding a dictionary into the Redis but I cannot append several dictionaries into the Redis.


Here's an example:

import redis

cli = redis.Redis('localhost')

list_of_dict = [  # dummy
    {
        'tags': {'module': 'destructor', 'station': 'SNMP'},
        'metric_name': 'destructor',
        'value': -8555,
        'time': '2018-11-13T15:25:07'
    },
    {
        'tags': {'module': 'voltage', 'station': 'SNMP'},
        'metric_name': 'voltage',
        'value': 222,
        'time': '2018-11-13T15:25:09'
    }
]

from pprint import pprint

for ld in list_of_dict:
    cli.hmset("pythonDict", ld)
    # conn.append(key, val)  # Needs a key value.

pprint(cli.hgetall("pythonDict"))

In the above code snippet .hmset() writes only the last element/dict of list_of_dict into the Redis (override) and .append() needs the a key,val.

How can I write/append the two dictionaries of list_of_dict on Redis and retrieve them?

3 Answers

Set the key and value separately. Write the code in the following way:

for key,val in list_of_dict:
    cli.hset("pythonDict", key, val)

pprint(cli.hgetall("pythonDict"))

If you want to store the array as it is and retrieve the value use json package. Below is a sample:

import json
cli.set("pythonDict", json.dumps(list_of_dict))
pprint(json.loads(cli.get("pythonDict")))

The approach you are using might turn suboptimal, as you are retrieving full data, appending to it, and saving it. Since your requirement is iteratively appending to an existing list, it is better you use a Redis LIST.

The storage and retrieval approach would be:

  1. Each element of your list would be a string ( which in turn is the JSON representation of your single dictionary element)

  2. Whenever you want to append a dictionary to your existing cache, you use an LPUSH command on python's redis client. That would append your new dictionaries to existing Redis list.

  3. For data retrieval, you would use LRANGE to retrieve your full or partial list of dictionaries. LRANGE has equivalent performance to HGETALL.

Example with redis client (the same set of commands can be used in the python redis client) :

enter image description here

Two solutions

1.use json

import json
cli.set("pythonDict", json.dumps(list_of_dict))
print(json.loads(cli.get("pythonDict")))

2.use pickle

import pickle
cli.set("pythonDict", pickle.dumps(list_of_dict))
print(pickle.loads(cli.get("pythonDict")))

if your dictionary key isn't string, you should use pickle.

Related