简体   繁体   中英

using redis-py to bulk populate a redis list

In a Django project, I'm using redis as a fast backend.

I can LPUSH multiple values in a redis list like so:

lpush(list_name,"1","2","3")

However, I can't do it if I try

values_list = ["1","2","3"]
lpush(list_name,values_list)

For the record, this doesn't return an error. Instead it creates a list list_name with a single value. Eg ['["1","2","3"]'] . This is not usable if later one does AnObject.objects.filter(id__in=values_list) . Nor does it work if one does AnObject.objects.filter(id__in=values_list[0]) (error: invalid literal for int() with base 10: '[').

What's the best way to LPUSH numerous values in bulk into a redis list (python example is preferred).

lpush (list_name, *values_list)

This will unpack contents in value_list as parameters.

If you have enormous numbers of values to insert into db, you can pipeline them. Or you can use the command line tool redis-cli --pipe to populate a database

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM