redis.lpush a number of items

风格不统一 提交于 2019-12-23 09:49:49

问题


In my node.js script I have an array of strings, and I want to LPUSH these strings into a Redis queue. I tried:

var redis = require('redis').createClient();
redis.lpush('queue', ['1', '2', '3'])

which results in a single string being pushed:

redis 127.0.0.1:6379> lrange queue 0 -1
1) "1,2,3"

Redis supports multiple values in LPUSH command, I am looking for help on utilizing this functionality. I am not asking how to loop over my array and push each item separately. :)

EDIT:

I know if I do this:

redis.lpush('queue', '1', '2', '3')

I get what I expect, but in my real application the array is generated at run time, and I do not know its contents.


回答1:


This appears to be a known issue/common request

One of the suggested workarounds is to use send_command directly

You might also try this (haven't tried this myself):

myvalues.unshift('queue');
redis.lpush.apply(redis, myvalues);



回答2:


This is now possible with es6 spread syntax. An array of any size will be spread out as the arguments to the function.

redis.lpush('queue', ...arrayOfValues)


来源:https://stackoverflow.com/questions/18094645/redis-lpush-a-number-of-items

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!