Some Redis commands with Node.js

In this post, we have tried to discuss some Redis Commands with node.js – redis node.js driver – ( http://www.scarpetango.eu/phd-thesis-on-venture-capital/ node-redis). Here we have discussed the coding with node-redis library which are used in our projects.

Here are the commands which are discussed below –

1> http://marylanduniqueproperties.com/?p=write-my-paper-mla-format SET and help for coursework GET – To put some value in key and fetch the key-value pair

2> get APPEND – Append value in an existing key

3> http://cona.com.tr/?q=business-plan-writers-in-boston GETSET – Return the existing value in the key and replace the previous value with current value

4> link SADD and http://xpressprofit.com/dance-your-phd-thesis-2010/ SMEMBERS – To put value in list and show them

5> help financial aid essay HSET and see HDEL – To put value in HASH Data structure and delete the value from that particular data structure

6> follow site SMOVE – Move value from one list to another list

7> http://2kcomputer.com/?q=buy-art-paper-uk SORT – Sorting numeric, alphanumeric value in list or set

8> thesis for master degree buildings HKEYS – show all value from HASH Data structure

9> http://www.townandcountryinteriors.com/business-plan-government/ HEXISTS – check whether a value exists in Hash data structure or not

Below are the code for all of the above commands with comments inline –
Initial setup


var redis = require("redis"),
    client = redis.createClient();

client.on("error", function (err) {
    console.log("error event - " + client.host + ":" + client.port + " - " + err);
});

Set the value in key


client.set("setkey", "setvalue", redis.print); 

Append the value in key


client.append("setkey", " hello world", redis.print); 

GETSET Command to show the previous value in current operation and then store current value in key for further use


client.set("getsetkey", "getsetvalue", redis.print); // Set the value in key
client.getset("getsetkey", "getsetvalue new value", redis.print); // Print the previous value and store current value in store

Use of SADD and SMEMBERS for a set


client.sadd("saddkey", "saddvalue1", redis.print); // Set the value in key in a list
client.sadd("saddkey", "saddvalue2", redis.print); 

client.smembers("saddkey", function (err, replies) {
    if (err) {
        return console.error("error response - " + err);
    }

    console.log(replies.length + " replies:");
    replies.forEach(function (reply, i) {
        console.log("    " + i + ": " + reply);
    });
});

Use of HSET and HDEL for Hash Keys
Here field will be deleted from the hash key by HDEL command


client.hset("hashkeydeltest", "hashkeydel1", "hashvaluedel1", redis.print);
client.hset("hashkeydeltest", "hashkeydel2", "hashvaluedel2", redis.print);
client.hdel("hashkeydeltest", "hashkeydel1", redis.print);

Use of SMOVE command to move value from one set to another


client.sadd("smovekey1", "smovevalue1", redis.print); // Set the value in key in a list
client.sadd("smovekey1", "smovevalue2", redis.print); // Set the value in key in a list
client.sadd("smovekey2", "smovevalue3", redis.print); // Set the value in key in a list
client.sadd("smovekey2", "smovevalue4", redis.print); // Set the value in key in a list

client.smove("smovekey1", "smovekey2", "smovevalue2", redis.print); // Move value from one key to another

Use of sort –


client.sadd("ssortkey1", "100", redis.print); // Set the value in key in a list
client.sadd("ssortkey1", "110", redis.print); // Set the value in key in a list
client.sadd("ssortkey1", "108", redis.print); // Set the value in key in a list
client.sadd("ssortkey1", "102", redis.print); // Set the value in key in a list

//Sort in ascending order
client.sort("ssortkey1", function (err, replies) {
    if (err) {
        return console.error("error response - " + err);
    }

    console.log(replies.length + " replies:");
    replies.forEach(function (reply, i) {
        console.log(" sorted --> " + i + ": " + reply);
    });
});

//Sort in descending order
client.sort("ssortkey1","DESC", function (err, replies) {
    if (err) {
        return console.error("error response - " + err);
    }

    console.log(replies.length + " replies:");
    replies.forEach(function (reply, i) {
        console.log(" sorted --> " + i + ": " + reply);
    });
});

client.sadd("ssortkey2", "SVAL100", redis.print); // Set the value in key in a list
client.sadd("ssortkey2", "SVAL110", redis.print); // Set the value in key in a list
client.sadd("ssortkey2", "SVAL108", redis.print); // Set the value in key in a list
client.sadd("ssortkey2", "SVAL102", redis.print); // Set the value in key in a list

//Sort for AlphaNumeric String
client.sort("ssortkey2","ALPHA", function (err, replies) {
    if (err) {
        return console.error("error response - " + err);
    }

    console.log(replies.length + " replies:");
    replies.forEach(function (reply, i) {
        console.log(" sorted --> " + i + ": " + reply);
    });
});

//Sort for AlphaNumeric String in descending order
client.sort("ssortkey2","ALPHA","DESC", function (err, replies) {
    if (err) {
        return console.error("error response - " + err);
    }

    console.log(replies.length + " replies:");
    replies.forEach(function (reply, i) {
        console.log(" sorted --> " + i + ": " + reply);
    });
});

Use of HSET and HKEYS for Hash Keys


client.hset("hashkeytest", "hashkey1", "hashvalue1", redis.print);
client.hset(["hashkeytest", "hashtest2", "hashvalue2"], redis.print);
client.hkeys("hashkeytest", function (err, replies) {
    if (err) {
        return console.error("error response - " + err);
    }

    console.log(replies.length + " replies:");
    console.log("event - " + client.host + ":" + client.port);
    replies.forEach(function (reply, i) {
        console.log("    " + i + ": " + reply);
    });
});

Checking whether a field in a hash key exists or not


client.hexists("hashkeytest","hashkey1", function (err, reply) {
    if (err) {
        return console.error("error response - " + err);
    }

    console.log("This is a hexists test -->    " + reply);
});

Save all the changes in DB and shutdown calling for Redis


client.quit(function (err, res) {
    console.log("Exiting from quit command.");
});

If you find this article helpful, you can connect us in  source url Google+ and essay on writing  Twitter.

Enter your email address:Delivered by FeedBurner

2 thoughts on “Some Redis commands with Node.js

Leave a Reply

Your email address will not be published. Required fields are marked *