2013-11-02 8 views
6

निम्न कोड संपूर्ण सरणी को लाल सूची में एकल मान के रूप में सहेजता है। लेकिन मैं अलग-अलग सरणी मानों को सहेजना चाहता हूं। मैं यह कैसे कर सकता हूं?जावास्क्रिप्ट सरणी को रेडिस सूची के रूप में कैसे सहेजें

पीएस तो गरीब अंग्रेजी के लिए खेद है।

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

var arr = [1,2,3]; 

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

आप सरणी तत्वों के माध्यम से पुनरावृति और उन्हें व्यक्तिगत रूप सम्मिलित करना चाहते हैं। आप लूप के लिए एक सरल 'लिख सकते हैं या' 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) { 

}) 

और अंत में Redis के लिए आदेश भेजने exec() कहते हैं।

+0

रेडिस-नोड का उपयोग करना जो मेरे लिए काम नहीं कर रहा था, थोड़ा संशोधित है: https://gist.github.com/vainrobot/141b2162e1200f7c7855 – fullstacklife

+0

इसे वापस कैसे प्राप्त करें ?? सामान्य काम नहीं कर रहा है। – jeevs

+0

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

6

यहां तक ​​कि अगर @gimenete जवाब काम करता है, तुम क्या चाहते करने के लिए सबसे अच्छा तरीका सूची तत्वों को अग्रेषित करने के रूप में तर्क तो जैसे rpush के लिए है:

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); 
})); 

पेशेवरों: - एक एकल अनुदेश हो जाएगा .apply एक RangeError: Maximum call stack size exceeded फेंक देंगे अगर तर्क सूची rpush के लिए पारित लंबाई बहुत बड़ा है (एक छोटे से ove: - एक कोने-केस : Redis को

विपक्ष 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 'कमांड के लिए तर्कों की गलत संख्या ERR। – user2522545

+0

मैंने इसे फिर से परीक्षण किया और यह काम करता है, आपने किस कोड का उपयोग किया था? – FGRibreau

+0

बिल्कुल टाइप करते समय। मेरे नोड जेएस फ़ाइल में कोई अन्य कोड नहीं है। – user2522545

संबंधित मुद्दे