Node.js NodeJS with Redis Some more important operations supported by node_redis.


Checking the Existence of Keys

Sometimes you may need to check if a key already exists and proceed accordingly. To do so you can use exists() function as shown below:

client.exists('key', function(err, reply) {
    if (reply === 1) {
    } else {
        console.log('doesn\'t exist');

Deleting and Expiring Keys

At times you will need to clear some keys and reinitialize them. To clear the keys, you can use del command as shown below:

client.del('frameworks', function(err, reply) {

You can also give an expiration time to an existing key as following:

client.set('key1', 'val1');
client.expire('key1', 30);

The above snippet assigns an expiration time of 30 seconds to the key key1.

Incrementing and Decrementing

Redis also supports incrementing and decrementing keys. To increment a key use incr() function as shown below:

client.set('key1', 10, function() {
    client.incr('key1', function(err, reply) {
        console.log(reply); // 11

The incr() function increments a key value by 1. If you need to increment by a different amount, you can use incrby() function. Similarly, to decrement a key you can use the functions like decr() and decrby().