2013-11-02 2 views
6

다음 코드는 전체 배열을 redis list에 하나의 값으로 저장합니다. 하지만 배열 값을 개별적으로 저장하고 싶습니다. 내가 어떻게 해?자바 스크립트 배열을 redis 목록으로 저장하는 방법

P.S 영어가 불쌍해서 죄송합니다. 파이프 라인 여러 명령에

var redis = require('redis'), 
    client = redis.createClient(); 

var arr = [1,2,3]; 

client.rpush('testlist',arr); 
+1

당신은 배열 요소를 반복하고 개별적으로 삽입 할. 간단한'for' 루프를 작성하거나'Array.prototype.forEach'를 사용할 수 있습니다. – fardjad

답변

14

사용 multi() 번 : 마지막으로

var redis = require('redis'), 
    client = redis.createClient(); 

var arr = [1,2,3]; 

var multi = client.multi() 

for (var i=0; i<arr.length; i++) { 
    multi.rpush('testlist', arr[i]); 
} 

multi.exec(function(errors, results) { 

}) 

그리고 레디 스에 명령을 보내 exec()를 호출합니다.

var redis = require('redis'), 
    client = redis.createClient(); 

var arr = [1,2,3]; 
client.rpush.apply(client, ['testlist'].concat(arr)); 

// ... or with a callback 
client.rpush.apply(client, ['testlist'].concat(arr).concat(function(err, ok){ 
    console.log(err, ok); 
})); 

프로 : @gimenete 응답 작품, 당신이 원하는 것을 할 수있는 가장 좋은 방법은 인수과 같이 rpush하기로 목록 요소를 전달하는 경우에도

+0

나를 위해 작동하지 않는 redis-node를 사용하여 여기에서 약간 수정 : https://gist.github.com/vainrobot/141b2162e1200f7c7855 – fullstacklife

+0

다시 검색하는 방법 ?? 정상적인 것은 작동하지 않습니다. – jeevs

+0

@jeevs'lrange testlist 0 -1' https://redis.io/commands/lrange – MyMomSaysIamSpecial

6

을 - 하나의 명령이 될 것입니다 rpush에 전달 된 인수 목록의 길이가 너무 큰 경우 .applyRangeError: Maximum call stack size exceeded가 발생합니다 (약간 비켜 - 코너의 경우 :

단점을 레디 스에 transmited V8의 경우 100,000 개 항목). MDC에서

는 :

The consequences of applying a function with too many arguments (think more than tens of thousands of arguments) vary across engines (JavaScriptCore has hard-coded argument limit of 65536), because the limit (indeed even the nature of any excessively-large-stack behavior) is unspecified. Some engines will throw an exception. More perniciously, others will arbitrarily limit the number of arguments actually passed to the applied function.

+0

나를 위해 작동하지 않습니다. '오류 : 'rpush'명령에 대한 잘못된 인수 수입니다. – user2522545

+0

다시 시험 해봤는데 작동했는데 어떤 코드를 사용 했습니까? – FGRibreau

+0

정확히 입력 할 때. 노드 js 파일에는 다른 코드가 없습니다. – user2522545

관련 문제