Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Batch value insert in Redis list

Tags:

python

redis

Is there a way to store multiple values in a Redis list at the same time? I can only find a way to insert 1 value at the time in a list.

I've been looking at the following commands documentation: http://redis.io/commands

update: I have created a ticket for this feature.

like image 307
jldupont Avatar asked Aug 24 '26 20:08

jldupont


2 Answers

You can use a pipeline, if using redis-py you could check out the below, I ran this on an aws instance of redis elasticache and found the following:

import redis
rs = redis.StrictRedis(host='host', port=6379, db=0)
q='test_queue'

Ran in 0.17s for 10,000 vals

def multi_push(q,vals):
    pipe = rs.pipeline()
    for val in vals:
        pipe.lpush(q,val)
    pipe.execute()

Ran in 13.20s for 10,000 vals

def seq_push(q,vals):
    for val in vals:
        rs.lpush(q,val)

~ 78x faster.

like image 51
Glen Thompson Avatar answered Aug 27 '26 09:08

Glen Thompson


Yeah, it doesn't look like that's possible. You might be able to use MULTI (transactions) to store multiple values in an atomic sequence.

like image 35
djc Avatar answered Aug 27 '26 10:08

djc



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!