How to get all keys and values ​​in redis in javascript?

I am creating a node API using javascript. I used redis as a keystore. I created a redis client in my application and can get the values ​​for the perticular key.

I want to get all the keys along with my values. So far I have done this:

app.get('/jobs', function (req, res) { var jobs = []; client.keys('*', function (err, keys) { if (err) return console.log(err); if(keys){ for(var i=0;i<keys.length;i++){ client.get(keys[i], function (error, value) { if (err) return console.log(err); var job = {}; job['jobId']=keys[i]; job['data']=value; jobs.push(job); }); } console.log(jobs); res.json({data:jobs}); } }); }); 

but I always get an empty array in response.

Is there any way to do this in javascript?

thanks

+8
javascript key-value redis
source share
6 answers

First of all, the problem in your question is that inside the for client.get is called with an asynchronous callback, where the synchronous for loop will not wait for the asynchronous callback and therefore the next line res.json({data:jobs}); called immediately after the for loop before asynchronous callbacks. While calling the string res.json({data:jobs}); the jobs array remains empty [] and returns with a response.

To mitigate this, you should use any of the same name modules like async , bluebird , ES6 Promise , etc.

Modified code using async ,

 app.get('/jobs', function (req, res) { var jobs = []; client.keys('*', function (err, keys) { if (err) return console.log(err); if(keys){ async.map(keys, function(key, cb) { client.get(key, function (error, value) { if (error) return cb(error); var job = {}; job['jobId']=key; job['data']=value; cb(null, job); }); }, function (error, results) { if (error) return console.log(error); console.log(results); res.json({data:results}); }); } }); }); 

But from the Redis documentation , it is noted that the use of Keys is intended for debugging and special operations, such as changing keyboard layouts and inappropriate production environments.

Therefore, I would suggest using another module called redisscan , as shown below, which uses SCAN instead of KEYS , as suggested in the Redis documentation .

Something like,

 var redisScan = require('redisscan'); var redis = require('redis').createClient(); redisScan({ redis: redis, each_callback: function (type, key, subkey, value, cb) { console.log(type, key, subkey, value); cb(); }, done_callback: function (err) { console.log("-=-=-=-=-=--=-=-=-"); redis.quit(); } }); 
+3
source share

The combination of two queries:

 import * as ioredis from 'ioredis'; const redis = new ioredis({ port: redisPort, host: redisServer, password: '', db: 0 }); const keys = await redis.collection.keys('*'); const values = await redis.collection.mget(keys); 

The order will be the same for both arrays.

+2
source share

This will get all the keys, but no values:

 const redis = require('redis'); const client = redis.createClient(); client.keys('*', (err, keys) => { // ... }); 

Now you need to get the values ​​for these keys in the usual way. For example:

 Promise.all(keys.map(key => client.getAsync(key))).then(values => { // ... }); 

or with the async module or in any other way.

+1
source share

If client is Object , then just do:

 var keys = Object.keys(client); var values = Object.values(client); 

You will get an array of keys and an array of values. That in the index, I in the values ​​corresponds to value , corresponding to the key located in the index in keys .

+1
source share

You should never do this. Firstly, it is not recommended to use KEYS * in production. Secondly, it does not scale (cluster).

You can arrange your cached entries in SET and query for items in SET, and then get the link keys. It also facilitates invalidation.

Check out some storage practices.

https://redis.io/topics/data-types-intro how to get all keys and values ​​in redis in javascript?

+1
source share

You can find something useful in this link.

https://github.com/NodeRedis/node_redis/tree/master/examples

+1
source share

All Articles