Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to save javascript array as redis list

The following code save the whole array as single value in redis list. But I want to save array values individually. How can I do it?

P.S So sorry for poor English.

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

var arr = [1,2,3];

client.rpush('testlist',arr);
like image 302
Thant Sin Aung Avatar asked Nov 02 '13 18:11

Thant Sin Aung


People also ask

Can we store array in Redis cache?

You could store an array of unique ids, each one the key of a seperately stored hash.

What is Redis Javascript?

Redis, an in-memory database that stores data in the server memory, is a popular tool to cache data. You can connect to Redis in Node. js using the node-redis module, which gives you methods to retrieve and store data in Redis.

How do I start Redis on node JS?

Create new session. js file in the root directory with the following content: const express = require('express'); const session = require('express-session'); const redis = require('redis'); const client = redis. createClient(); const redisStore = require('connect-redis')(session); const app = express(); app.


1 Answers

Use multi() to pipeline multiple commands at once:

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) {

})

And finally call exec() to send the commands to redis.

like image 71
gimenete Avatar answered Sep 19 '22 05:09

gimenete