storing data into redis through cron job

2019-01-27 11:47发布

问题:

I want to store data into redis from pandas through a cron job every 15 minute and below is my code:-

I am taking data into pandas every 15 minutes with below code and sending it to the redis dictionary mydict2 through a cron job.

import sys
import pickle
import redis

r = redis.StrictRedis(host='localhost', port=6379, db=0)

test_dict1 = results_df.set_index('user')['ua'].T.to_dict()

p_mydict = pickle.dumps(test_dict1)
r.set('mydict2', p_mydict)

I am getting the same output again and again in the key mydict2. Basically i want to store user ids for the whole month and at the end of the month i want the unique count of that.

Also i am using set method, what could be the best method assuming i am having a very large amount of data.

Can someone help me out here.

回答1:

Replace below

p_mydict = pickle.dumps(test_dict1)
r.set('mydict2', p_mydict)

with

    for k, v in test_dict1.items():
        r.hmset(k, {"ua" : v})
    print("Done adding stuff")

and each key in your dictionary will be a key in Redis.