1. Introduction of Redis

Redis is a high performance key-value database that is completely open source and free and complies with the BSD protocol.

2. Redis advantage

  • High performance – Redis can read 110,000 times /s and write 81,000 times /s.
  • Rich data types – Redis supports binary string, list, hash, collection, and ordered collection data type operations.
  • Atomicity – All operations in Redis are atomic, meaning that they either succeed or fail to be executed at all
  • Individual operations are atomic. Multiple operations also support transactions, namely atomicity, wrapped in MULTI and EXEC instructions.
  • Rich features – Redis also supports publish/subscribe, notifications, key expiration, and more.

3. Data types

  • string
  • Hash value
  • The list
  • A collection of
  • An ordered list

3.1 the string

Strings are the most basic type. Each key corresponds to a value

The setting of the SET

SET name yqjpx
Copy the code

GET access to the value

GET name
Copy the code

Increasing INCR INCRBY

SET age 1
INCR age
INCRBY age 6
DECR age
DECRBY age  9
Copy the code

key

DEL Key Deletes a key DEL user EXISTS key Determines whether a key EXISTS EXISTS user EXPIRE key seconds Sets the EXPIRE user 10 TTL key TTL User TYPE Key Indicates the TYPE of the value stored in the key. TYPE UserCopy the code

3.2 the hash value

A hash is a mapping of keys and values of a string type, especially suitable for storing objects.

HSET HMSET Set value

HSET Person Name Set a single value HMSET user name yqjpx age 9 Set multiple valuesCopy the code

**HGET HGETALL gets the value **

HGET User name Obtains a single value HMGET user name age Obtains multiple values HGETALL user obtains multiple valuesCopy the code

HDEL Key field Delete key

HDEL key field
HDEL user name
HGETALL user
Copy the code

HKEYS retrieves all KEYS

3.3 list

A list is a simple list of strings, sorted by insertion order. You can add an element to either the head (left) or the tail (right) of the list.

LPUSH RPUSH adds elements

Returns the length of the list

LPUSH ids 2
LPUSH ids 1
RPUSH ids 3
RPUSH ids 4
RPUSH ids  5 6 
Copy the code

LRANGE looks at elements

LRANGE ids 0 2
LRANGE ids 0 -1
Copy the code

LPOP RPOP pop elements

View and Delete

LPOP ids 
RPOP ids
Copy the code

LINDEX ids 1

Gets the elements in the list by index

LINDEX ids 0
Copy the code

LLEN key

Gets the list length LLEN IDSCopy the code

3.4 the collection

Collections are unordered collections of type string

**SADD adds ** returns if the specified element already exists in the collection0If it does not exist, the value is returned with success1

SADD tags 1
SADD tags 2
SADD tags 2
SADD tags 3
SADD tags  4 5 6**SMEMBERS tags** SMEMBERS View the collection SMEMBERS tags** SCARD gets the number of collection elements **SCARD tags** SREM deletes elements ** SREM tags member SREM tags4SMEMBERS tags ** Collection operations ** SADD A1 2 3 
SADD B 2 3 4SINTER A B intersection SDIFF A B difference SUNION A B union ** Ordered sets ** Ordered sets are collections of strings just like sets, and cannot be repeated except that each set is associated with A fraction of type double. Redis can use this classification to sort the elements in the collection from smallest to largest. Elements cannot be repeated, but scores can be repeated **ZADD adds elements **ZADD key score1 member1 [Score2 member2] ZADD levels1 one
ZADD levels 2 two
ZADD levels 3 three
ZADD levels 4Four **ZCARD Gets the number of members of the ordered set **ZCARD key ZCARD levels **ZRANGE Views the ordered set **ZRANGE levels0 -1View ZRANGE Levels by range0 2**ZREM removes one or more members of the ordered set **ZREM key member [member...]  ZADD levels1 one
ZADD levels 2 two
ZREM levels one
 ZRANGE levels  0 -1
Copy the code

4. Use in Node.js

const redis = require('redis');
let client = redis.createClient(6379.'127.0.0.1');
client.on('error'.function (error) {
    console.error(error);
});
//1. String type
client.set('name'.'yqjpx', redis.print);
client.get('name', redis.print);
/ / 2. Collection
client.hset('user'.'name'.'yqjpx', redis.print);
client.hset('user'.'age'.'8', redis.print);
client.hget('user'.'age', redis.print);
client.hkeys('user'.function (err, replies) {
    replies.forEach(function (item, index, items) {
        client.hget('user', item, redis.print);
    });
});
Copy the code

5.Redis publishes subscriptions

Redis publish subscription is a message communication model: the sender sends the message, the subscriber receives the message, and the client can subscribe to any number of channels.

let client1 = redis.createClient(6379.'127.0.0.1');
let client2 = redis.createClient(6379.'127.0.0.1');
let count = 0;
client1.subscribe('food');
client1.subscribe('drink');
client1.on('message'.function (channel, message) {
    console.log(channel, message);
    client1.unsubscribe('food');
});

client2.publish('food'.'bread');
client2.publish('drink'.'orange');
setTimeout(() = > {
    client2.publish('food'.Bread '2');
    client2.publish('drink'.'orange juice 2');
}, 2000);
Copy the code

6. Redis transactions

Redis transactions can execute multiple commands at once

  • Multiple commands can be placed in the cache queue prior to EXEC command execution
  • The cache queue is executed upon receipt of the EXEC command
  • During the execution of a transaction, new commits cannot be inserted into the transaction execution sequence
  • DISCARD can cancel a transaction, abandoning execution of all commands within a transaction block

Other: www.npmjs.com/package/red…