Now that you know how to connect with Redis from Node.js, let’s see how to store key-value pairs in Redis storage.
Storing Strings
All the Redis commands are exposed as different functions on the client object. To store a simple string use the following syntax:
client.set('framework', 'AngularJS');
Or
client.set(['framework', 'AngularJS']);
The above snippets store a simple string AngularJS against the key framework. You should note that both the snippets do the same thing. The only difference is that the first one passes a variable number of arguments while the later passes an args array to client.set()
function. You can also pass an optional callback to get a notification when the operation is complete:
client.set('framework', 'AngularJS', function(err, reply) {
console.log(reply);
});
If the operation failed for some reason, the err
argument to the callback represents the error. To retrieve the value of the key do the following:
client.get('framework', function(err, reply) {
console.log(reply);
});
client.get()
lets you retrieve a key stored in Redis. The value of the key can be accessed via the callback argument reply. If the key doesn’t exist, the value of reply will be empty.
Storing Hash
Many times storing simple values won’t solve your problem. You will need to store hashes (objects) in Redis. For that you can use hmset()
function as following:
client.hmset('frameworks', 'javascript', 'AngularJS', 'css', 'Bootstrap', 'node', 'Express');
client.hgetall('frameworks', function(err, object) {
console.log(object);
});
The above snippet stores a hash in Redis that maps each technology to its framework. The first argument to hmset()
is the name of the key. Subsequent arguments represent key-value pairs. Similarly, hgetall()
is used to retrieve the value of the key. If the key is found, the second argument to the callback will contain the value which is an object.
Note that Redis doesn’t support nested objects. All the property values in the object will be coerced into strings before getting stored. You can also use the following syntax to store objects in Redis:
client.hmset('frameworks', {
'javascript': 'AngularJS',
'css': 'Bootstrap',
'node': 'Express'
});
An optional callback can also be passed to know when the operation is completed.
All the functions (commands) can be called with uppercase/lowercase equivalents. For example, client.hmset()
and client.HMSET()
are the same.
Storing Lists
If you want to store a list of items, you can use Redis lists. To store a list use the following syntax:
client.rpush(['frameworks', 'angularjs', 'backbone'], function(err, reply) {
console.log(reply); //prints 2
});
The above snippet creates a list called frameworks and pushes two elements to it. So, the length of the list is now two. As you can see I have passed an args
array to rpush
. The first item of the array represents the name of the key while the rest represent the elements of the list. You can also use lpush()
instead of rpush()
to push the elements to the left.
To retrieve the elements of the list you can use the lrange()
function as following:
client.lrange('frameworks', 0, -1, function(err, reply) {
console.log(reply); // ['angularjs', 'backbone']
});
Just note that you get all the elements of the list by passing -1 as the third argument to lrange()
. If you want a subset of the list, you should pass the end index here.
Storing Sets
Sets are similar to lists, but the difference is that they don’t allow duplicates. So, if you don’t want any duplicate elements in your list you can use a set. Here is how we can modify our previous snippet to use a set instead of list.
client.sadd(['tags', 'angularjs', 'backbonejs', 'emberjs'], function(err, reply) {
console.log(reply); // 3
});
As you can see, the sadd()
function creates a new set with the specified elements. Here, the length of the set is three. To retrieve the members of the set, use the smembers()
function as following:
client.smembers('tags', function(err, reply) {
console.log(reply);
});
This snippet will retrieve all the members of the set. Just note that the order is not preserved while retrieving the members.
This was a list of the most important data structures found in every Redis powered app. Apart from strings, lists, sets, and hashes, you can store sorted sets, hyperLogLogs, and more in Redis. If you want a complete list of commands and data structures, visit the official Redis documentation. Remember that almost every Redis command is exposed on the client object offered by the node_redis module.