Redis Operations Command Daquan (version Nodejs)

Source: Internet
Author: User



* 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:  Port: 6379

Part 2: Instance

var redis = require("redis"),
client = redis.createClient(6379,""); (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, ' '); (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, ' ', {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);});


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.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
console.log(err,response); //will print lee


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.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.hget (hashkey,field,callback) Gets a field value in the hash data


Client.hgetall (Hashkey,callback) gets all the data of the hash data, including fields and values


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.
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



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
Brpop key [key ...] timeout



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.

Sadd key member [member ...]



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

SCard Key

console.log("Number of key roban:demo:sdemo is:" + response);


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.


console.log("Poped value of key roban:demo:sdemo is:" + response);


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.

Srandmember key [Count]

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: and provide relevant evidence. A staff member will contact you within 5 working days.

Tags Index: