Redis Operations Command Daquan (version Nodejs)

Source: Internet
Author: User

Label:

/*——————————————————————————————

* This case is based on the following operating environment:

* System: CentOS 5.x

* Nodejs version : 0.9 or more

* Redis version: 2.8

* Redis-nodejs extension : 0.12.1

/*——————————————————————————————

Part 1: Installing extensions

Use the following command:

npm -g install redis

Redis instance:

Host: 127.0.0.1  Port: 6379

Part 2: Instance

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

Client.info (function (err,response) {        console.log (err,response);}); /If the query has no errors, the value of err is null


Create a Redis connection

The following code shows creating a Redis socket connection and querying the Redis server information, which is the REDIS-CLI Info command

var Redis = require ("Redis"), var client = redis.createclient (6379, ' 127.0.0.1 '); Client.info (function (err,response) {        Console.log (err,response);});

The above code extends some of the issues:

1. What happens if the server is not connected?

2. What if the connection is timed out?

For the above problems, we need to make some changes to the code in order to better handle the error, it will be a good habit.

CreateClient (port,host,options) This function accepts three parameters, the first is the connection port, the second is the host ip/name, the third parameter is the configuration item, Key=>value form For example: var client = Redis.createclient (63719, ' 127.0.0.1 ', {connect_timeout:1}); Increase the timeout option error handling: There are 2 Nodejs error handling mechanisms, 1 of which are synchronous operations that can be used with the try ... catch. To catch the error, the other is the event, Redisclient has an error event, when the error changes back to emmit this event, using the method: Client.on (' Error ', function (error) {        Console.log (Error);});

Set

Client.set (Key,value,callback), the callback function has 2 callback parameters, error and response, error indicates that errors during operation are null to indicate no error, response is a Boolean value
client.set("Roban","lee",function(err,response){
console.log(err,response);
});

Get

Client.get (Key,callback), the callback function has 2 callback parameters, error and response, and error indicates that errors during the operation are null to indicate that there is no error, response is the obtained value, Null indicates that no data was obtained
client.get("Roban",function(err,response){
console.log(err,response); //will print lee
});


Hset

Client.hset (hashkey,field,value,callback) hash data type, the first parameter is the key name, the second is the field that needs to be set key, the third is a value, the fourth parameter is the callback parameter, the content and set are consistent
client.hset("roban:demo:hset","today","fine",function(err,response){
console.log(err,response);
});


Hmset

Client.hmset (Hashkey,field,value,field,value ... callback) hash data type, the first parameter is the key name, the following parameter is an invariant parameter, the data format is Key,value, key, Value
client.hset("roban:demo:hset","lastday","notgood","nextday","willbefine",function(err,response){
console.log(err,response);
});


Hget

Client.hget (hashkey,field,callback) Gets a field value in the hash data
client.hset("roban:demo:hset","today",function(err,response){
console.log(err,response);
});

Hgetall

Client.hgetall (Hashkey,callback) gets all the data of the hash data, including fields and values
client.hset("roban:demo:hset",function(err,response){
console.log(err,response);
});

Blpop

The blocking popup queue data pops up from the top (left) of the data, and when Blpop is called, if there is at least one non-empty list in the given key, then the header element of the first non-empty list that is encountered is popped, together with the name of the list to which the popup element belongs, and the result is returned to the caller. When there are more than one given key, Blpop checks each list in sequence in the order given by the key parameter.
Grammar:
Blpop key [key ...] timeout

The following code indicates that blocking Roban:demo:blpop this queue for 10 seconds, if there is data, immediately eject from the left, if not, continue to block until 10 seconds

client.blpop("roban:demo:blpop",10,function(err,response){
console.log(err,response);
});

Brpop

The blocking popup queue data pops up from the end of the data (to the right) and, when given multiple key parameters, examines each list in the order of key keys and pops up the trailing element of the first non-empty list. Use the same method as Blpop, but the data pops up in a different way
Grammar:
Brpop key [key ...] timeout

client.brpop("roban:demo:blpop",10,function(err,response){
console.log(err,response);
});

Sadd


Adding one or more member elements to the collection key, the member element that already exists in the collection is ignored.
If key does not exist, create a collection that contains only the member element as a member.
When key is not a collection type, an error is returned.

Grammar:
Sadd key member [member ...]

client.sadd("roban:demo:sdemo",hello,this,fuck,world,function(err,response){
console.log(err,response);
});

SCard


Returns the cardinality of the collection key (the number of elements in the collection).

Grammar:
SCard Key

client.scard("roban:demo:sdemo",function(err,response){
console.log("Number of key roban:demo:sdemo is:" + response);
});

SPOP


Removes and returns a random element in the collection.

You can use the Srandmember command if you want to get only one random element, but you don't want the element to be removed from the collection.

Grammar:
SPOP Key

client.spop("roban:demo:sdemo",function(err,response){
console.log("Poped value of key roban:demo:sdemo is:" + response);
});

Srandmember


If the command executes, only the key parameter is supplied, then a random element in the collection is returned.
Starting with Redis version 2.6, the Srandmember command accepts the optional count parameter:
1. If count is a positive number and is less than the collection cardinality, the command returns an array containing count elements, each of which differs from the elements in the array. If count is greater than or equal to the collection cardinality, then the entire collection is returned.

2. If count is a negative number, the command returns an array in which the elements in the array may recur multiple times, and the length of the array is the absolute value of count.
The operation is similar to SPOP, but SPOP removes and returns random elements from the collection, and Srandmember returns only random elements without any changes to the collection.

Grammar:
Srandmember key [Count]

client.srandmember("roban:demo:sdemo",function(err,response){
console.log("Poped value of key roban:demo:sdemo is:" + response);
});

Redis Operations Command Daquan (version Nodejs)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

Tags Index: