1

I have just discovered how cool node js is and was looking at options for persisting. I saw that you could use redis-client to store data in redis and I have been able to store data ok, like so:

var redis = require('redis-client');

var r = redis.createClient();

var messege = {'name' => 'John Smith'};

var type = "Contact";

r.stream.on( 'connect', function() {
  r.incr( 'id' , function( err, id ) {
    r.set( type+':'+id, JSON.stringify(messege), function() {
      sys.puts("Saved to redis");
    });
  });
});

This store a key with a json string as the value. I am however trying to retrieve all the keys from redis and loop around them. I am having trouble figuring out the way to do this, could anyone point me in the right direction?

Cheers

Eef

1
  • You may want to look into the redis2JSON module. It may make things easier Commented Aug 13, 2011 at 22:02

1 Answer 1

3

To get keys from redis, you should use the .keys parameter. The first parameter that you pass is a 'filter' and .keys will return any items matching the filter.

For example r.keys('*', ...) will return all of the keys in redis as an array.

Here's the documentation on this command: http://redis.io/commands/keys

To loop through them, you can just do a simple for in as follows: r.keys('*', function (keys) { for (key in keys) { console.log(key); } });

Sign up to request clarification or add additional context in comments.

2 Comments

ah gotcha. thought it would be something simple like that. total brain freeze. thanks!
No prob! I didn't try this code out before writing it, so let me know if you have any issues with it, I'm happy to take a look :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.