Introduction of Redis

Redis is a high performance key-value database. Redis stores data in memory and records data changes on disk. Because the data is stored in memory, data manipulation is very fast.

The installation

For example, in Windows, download the Redis of Windows as follows: 3.2.100 After downloading the redis, decompress it to the D:redis directory

Open the service

Open a CMD window, go to the directory D:redis and run redis.server.exe redis.windows.conf.

If the above interface appears, redis has started the service on the local port 6379, then you can connect to the Redis server using the client.

Use Redis in Node

First, install the driver: NPM install redis

Redis supports a variety of data types, such as key/value pairs, hash tables, linked lists, sets and so on.

Common data

Let’s start by looking at how to store and retrieve key/value pairs.

Var redis = require('redis') var client = redis.createclient (6379, '127.0.0.1') client.on('error', function (err) { console.log('Error ' + err); }); // 1 key pair client.set('color', 'red', redis.print); client.get('color', function(err, value) { if (err) throw err; console.log('Got: ' + value) client.quit(); })Copy the code

Run, the result is as follows

Hash table

Hash table type JS object.

client.hmset('kitty', {
  'age': '2-year-old',
  'sex': 'male'
}, redis.print);
client.hget('kitty', 'age', function(err, value) {
  if (err) throw err;
  console.log('kitty is ' + value);
});

client.hkeys('kitty', function(err, keys) {
  if (err) throw err;
  keys.forEach(function(key, i) {
    console.log(key, i);
  });
  client.quit();
});Copy the code

The result is as follows:

The list

A Redis list is similar to a JS array. Lpush adds values to the list and lrange gets the list elements in the range of arguments start and end. End is -1, indicating the last element in the list. Note: As the length of the list increases, the data retrieval slows down (O(n) in big O notation)

client.lpush('tasks', 'Paint the house red.', redis.print);
client.lpush('tasks', 'Paint the house green.', redis.print);
client.lrange('tasks', 0, -1, function(err, items) {
  if (err) throw err;
  items.forEach(function(item, i) {
    console.log(' ' + item);
  });
  client.quit();
});Copy the code

The result is as follows:

A collection of

Like the Set in JS, the elements in the Set must be unique, and its performance: O(1) in the big O notation

Client. Sadd (' IP 'and' 192.168.3.7, redis. Print). Client. Sadd (' IP 'and' 192.168.3.7, redis. Print). Client. Sadd (' IP 'and' 192.168.3.9, redis. Print). client.smembers('ip', function(err, members) { if (err) throw err; console.log(members); client.quit(); });Copy the code

The result is as follows:

channel

Redis goes beyond the traditional responsibilities of data storage and also provides channels, which are data delivery mechanisms that provide publish/reserve functionality.

Var clientA = redis.createclient (6379, '127.0.0.1') var clientA = redis.createclient (6379, '127.0.0.1') On ('message', function(channel, message) {console.log('Client A got message from channel %s: %s', channel, message); }); clientA.on('subscribe', function(channel, count) { clientB.publish('main_chat_room', 'Hello world! '); }); clientA.subscribe('main_chat_room');Copy the code

In the above code, clientA subscribes to main_chat_room. When clientA catches the subscription event and executes the callback function, clientB sends a message Hello world to main_chat_room. ClientA receives the information and prints it out on the console. The result is as follows:

summary

This article is just a basic introduction to Redis. For more information, please refer to Redis Documentation: Redis Development, operation and maintenance