Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Redis & Node.js: All keys

Tags:

Basic question: Using Node.js I would like to get all the keys in my redis db. My redis db looks like this when I call keys *;

  1. aXF
  2. x9U
  3. lOk

So each record I have, has a unique key, generated as a random string. Now I would like to call something like foreach(key in Redis) and get all keys in the redis. Would it be possible to accomplish a "SELECT * FROM Redis"-like query with Node.js & Redis

like image 949
Ali Avatar asked Oct 09 '12 06:10

Ali


People also ask

What is Redis used for?

Redis enables you to write traditionally complex code with fewer, simpler lines. With Redis, you write fewer lines of code to store, access, and use data in your applications. The difference is that developers who use Redis can use a simple command structure as opposed to the query languages of traditional databases.

Is Redis a cache or database?

Redis is a database for a range of data sizes, from a few megabytes to hundreds of terabytes. With Redis Enterprise, you can use Redis as both an in-memory cache and a primary database in a single system, thus eliminating the complexity and latency of two separate systems.

Is Redis better than SQL?

Redis is faster though than most relational databases. If you're only going to be doing key:value pair queries, then you'll want to use Redis.

Is Redis better than MongoDB?

Redis is faster than MongoDB because it's an in-memory database. This makes it a great choice for building complicated data structures quickly. MongoDB, however, suits most medium-sized businesses that need a reliable database. It's relatively simple and easy to use and, as we mentioned earlier, very scalable.


2 Answers

Sure, you'll need to install the redis module for nodejs which can be found at https://github.com/redis/node-redis.

npm install redis 

Then you would do:

var redis = require('redis'),     client = redis.createClient();  client.keys('*', function (err, keys) {   if (err) return console.log(err);       for(var i = 0, len = keys.length; i < len; i++) {     console.log(keys[i]);   } });         

Generally speaking you won't want to always return all of the keys (performance will be bad for larger data sets), but this will work if you are just testing things out. There is even a nice warning in the Redis documentation:

Warning: consider KEYS as a command that should only be used in production environments with extreme care. It may ruin performance when it is executed against large databases. This command is intended for debugging and special operations, such as changing your keyspace layout. Don't use KEYS in your regular application code. If you're looking for a way to find keys in a subset of your keyspace, consider using sets.

like image 142
Bill Avatar answered Sep 28 '22 02:09

Bill


Install redis client for nodejs

npm install redis 

Then I do the following to get all key's data

var redis =   require('redis'),     client =  redis.createClient();  client.multi()     .keys('*', function (err, replies) {         // NOTE: code in this callback is NOT atomic         // this only happens after the the .exec call finishes.          console.log("MULTI got " + replies.length + " replies");          replies.forEach(function (reply, index) {             console.log("Reply " + index + ": " + reply.toString());             client.get(reply, function(err, data){                     console.log(data);             });         });      })     .exec(function (err, replies) {}); 
like image 31
Carlos Júlio Avatar answered Sep 28 '22 02:09

Carlos Júlio