Redis Journey Part 5 - Redis Advanced Commands

I. Transactions
1. Basic Concepts

(1) Concept: A transaction in redis is a collection of commands. Transactions, like commands, are the smallest execution unit of redis. The commands in a transaction are either all executed or not all executed;

(2) Principle: First send all commands belonging to a transaction to the redis server, and then let redis execute these commands in turn.

(3) Grammar example:

127.0.0.1:6379> MULTI 
OK
127.0.0.1:6379> LPUSH list 76 87 99
QUEUED
127.0.0.1:6379> RPOP list 
QUEUED
127.0.0.1:6379> SET foo helo
QUEUED
127.0.0.1:6379> EXEC
1) (integer) 3
2) "76"
3) OK
127.0.0.1:6379>

2. Error handling

(1) Syntax error: none of the commands in the transaction will be executed

(2) Execution error (such as operating the list with the SADD command): the wrong line is not executed, and the rest are executed

3. WATCH command
(1) Function: One or more keys can be monitored. Once one of the keys is modified (deleted), subsequent transactions will not be executed. Monitoring continues until EXEC is completed.

(2) Grammar example:

127.0.0.1:6379> set key good
OK
127.0.0.1:6379> WATCH key
OK
127.0.0.1:6379> set key hello
OK
127.0.0.1:6379> MULTI
OK
127.0.0.1:6379> set key nihaoa
QUEUED
127.0.0.1:6379> EXEC
(nil)
127.0.0.1:6379> get key
"hello"

 

2. Survival time
(1) Set the survival time of a key EXPIRE key 20 unit seconds
Description: return 1 means the setting is successful, return 0 means the key does not exist or the setting fails

(2) Check how long a key has expired TTL key
description: return the remaining time of the key, if the key does not exist, return -2, if there is no time-to-live for the key, return -1 (permanently existing key)

(3) Cancel the survival time of the key to set the PERSIST key 
Description: If the setting is successful, return 1, if the key does not exist or the key is permanent, return 0


3. Sorting
1. Simple sorting (operations for lists, sets, and ordered sets)

By default, the elements will be converted to floating point numbers and then sorted. If the elements are not numbers, adding the ALPHA parameter can sort the elements according to the dictionary. For sorted sets, the score is ignored when sorting, and the elements are just sorted.

(2) Example 1

127.0.0.1:6379> LPUSH num 33 44 5 89 -2 
(integer) 5
127.0.0.1:6379> SORT num
1) "-2"
2) "5"
3) "33"
4) "44"
5) "89"
127.0.0.1:6379> SORT num DESC LIMIT 0 3
1) "89"
2) "44"
3) "33"

(3)示例2
127.0.0.1:6379> ZADD scores 19 tim 39 tom 55 jane
(integer) 3
127.0.0.1:6379> SORT scores
(error) ERR One or more scores can't be converted into double
127.0.0.1:6379> SORT scores ALPHA
1) "jane"
2) "tim"
3) "tom"
127.0.0.1:6379> SORT scores ALPHA DESC 
1) "tom"
2) "tim"
3) "jane"
127.0.0.1:6379> SORT scores ALPHA DESC LIMIT 0 2
1) "tom"
2) "tim"

2. Complex sorting (comprehensive sorting)

(1) BY parameter
BY reference key, the reference key can be a string type key or an attribute of a hash type key (represented as key name -> attribute name). Once a reference key is provided, then sort will not sort by value, but by the value of the reference key.
* denotes a placeholder. If no meaningful reference key is provided, it's like there is no BY parameter.

(2) GET parameters
GET parameters do not affect sorting. The function is to make the return result of the SORT command no longer the value of the element itself, but the key value specified in the GET parameter. Also use * as a placeholder. GET # indicates that it also returns its own value.
A SORT command can have multiple GET parameters, but BY can only have one

(3) STORE parameter
The STORE parameter is used to store the sorted results of SORT. And you can combine the EXPIRE command to set the effective time of the result. The saved key is of type list, and the returned result is the number of results.

(3)示例:
127.0.0.1:6379> LPUSH users 2 4 1 3 6 5 
(integer) 6
127.0.0.1:6379> HMSET users_1 name jianjian age 27
OK
127.0.0.1:6379> HMSET users_2 name zhangsan age 46
OK
127.0.0.1:6379> HMSET users_3 name john age 37
OK
127.0.0.1:6379> HMSET users_4 name lisi age 10
OK
127.0.0.1:6379> HMSET users_5 name kart age 60
OK
127.0.0.1:6379> HMSET users_6 name jim age 100
OK
127.0.0.1:6379> KEYS *
1) "users_6"
2) "users_3"
3) "users_5"
4) "users_1"
5) "users_2"
6) "users"
7) "users_4"
127.0.0.1:6379>SORT users
1) "1"
2) "2"
3) "3"
4) "4"
5) "5"
6) "6"
127.0.0.1:6379> SORT users BY users-*->age 
1) "1"
2) "2"
3) "3"
4) "4"
5) "5"
6) "6"
127.0.0.1:6379> SORT users BY users_*->age 
1) "4"
2) "1"
3) "3"
4) "2"
5) "5"
6) "6"
127.0.0.1:6379> SORT users BY users_*->age DESC 
1) "6"
2) "5"
3) "2"
4) "3"
5) "1"
6) "4"
127.0.0.1:6379> SORT users BY users_*->age DESC LIMIT 1 3
1) "5"
2) "2"
3) "3"
127.0.0.1:6379> SORT users BY users_*->name LIMIT 1 3
(error) ERR One or more scores can't be converted into double
127.0.0.1:6379> SORT users BY users_*->name ALPHA LIMIT 1 3
1) "6"
2) "5"
3) "4"
127.0.0.1:6379> SORT users BY users_*->name ALPHA LIMIT 1 3 GET users_*->name GET users_*->age GET #
1) "jim"
2) "100"
3) "6"
4) "kart"
5) "60"
6) "5"
7) "lisi"
8) "10"
9) "4"

127.0.0.1:6379> SORT users BY users_*->name ALPHA LIMIT 1 3 GET users_*->name GET users_*->age STORE result
(integer) 6
127.0.0.1:6379> TYPE result
list
127.0.0.1:6379> LRANGE result 0 -1
1) "jim"
2) "100"
3) "kart"
4) "60"
5) "lisi"
6) "10"
127.0.0.1:6379> EXPIRE result 20
(integer) 1
127.0.0.1:6379> TTL result
(integer) 2
127.0.0.1:6379> TTL result
(integer) -2
127.0.0.1:6379> KEYS *
1) "users_6"
2) "uname"
3) "scores"
4) "num"
5) "users_5"
6) "users_2"
7) "users_1"
8) "users_3"
9) "nums"
10) "users"
11) "users_4"


4. Message notification
1. Task queue 
The task queue is the queue for delivering tasks. The two parties involved are the producer and the consumer. The former is responsible for putting messages into the queue (such as serializing an object into the queue), and the latter is responsible for taking messages from the queue and processing them.

The advantage of queues is that they are loosely coupled, and producers and consumers can evolve independently.

2. The idea of ​​​​redis implementing task queues
Using the data structure of list, message queues can be implemented. Idea: The producer and consumer are connected to the same list (such as the tasks list), then the producer uses LPUSH to add new information to the queue, and the consumer uses RPOP to fetch messages from the queue for processing until all processing is completed.

3. The above case of the BRPOP command
has a special case. If the queue is empty, can it be possible to notify the consumer as soon as a new element is added to the queue instead of letting the consumer take a loop to see if a new task arrives?

(1) BRPOP queue timeout 
queue represents the monitored queue, and timeout represents the timeout time. If no new elements are added after the timeout period, then returning nil
timeout = 0 will block the connection to the redis server until a new task is added. Once a new one is added, the new task will be taken away immediately.

(2) Example: use two cli
127.0.0.1:6379> LPUSH queue ghjk
(integer) 1

127.0.0.1:6379> BRPOP queue 0
1) "queue"
2) "ghjk"
(52.71s)

4. Priority queue
(1) BRPOP key [key....] timeout Command
function: detect multiple keys at the same time, block if all keys have no elements, and pop up the element if one of the keys has an element
Meaning: if There are multiple keys (multiple lists), then the left takes precedence over the right. This idea can implement a priority queue.

(2) Example: using two cli


127.0.0.1:6379> LPUSH q1 89 56b 23y
(integer) 3
127.0.0.1:6379> LPUSH q2 what where which
(integer) 3
127.0.0.1:6379> LPUSH q3 a b c
(integer) 3

 

127.0.0.1:6379> BRPOP q1 q2 q3 0
1) "q1"
2) "89"
127.0.0.1:6379> BRPOP q1 q2 q3 0
1) "q1"
2) "56b"
127.0.0.1:6379> BRPOP q1 q2 q3 0
1) "q1"
2) "23y"
127.0.0.1:6379> BRPOP q1 q2 q3 0
1) "q2"
2) "what"
127.0.0.1:6379> BRPOP q1 q2 q3 0
1) "q2"
2) "where"
127.0.0.1:6379> BRPOP q1 q2 q3 0
1) "q2"
2) "which"
127.0.0.1:6379> BRPOP q1 q2 q3 0
1) "q3"
2) "a"
127.0.0.1:6379> BRPOP q1 q2 q3 0
1) "q3"
2) "b"
127.0.0.1:6379> BRPOP q1 q2 q3 0
1) "q1"
2) "hello"


5. Publish/Subscribe
(1) The publish-subscribe model includes two roles of publisher and subscriber. Publishers can publish messages to multiple channels, and subscribers can subscribe to multiple channels. Once you subscribe, you will receive messages. Communication between processes can also be achieved in this way.

(2)PUBLISH channel message 命令

Send a message to the specified channel, returning the number of subscribers. The sent message will not be persistent, and subscribers will not receive the previously sent message.

(3) The SUBSCRIBE channel [channel...] ​​command
subscribes to one or more channels

(4) The PSUBSCRIBE channel.?* command
subscribes to the channel according to the specified rules. ?* represents a wildcard, which means to subscribe to all channels starting with channel

(5) Example, two cli
127.0.0.1:6379> PUBLISH ch1 fuck
(integer) 1
127.0.0.1:6379> PUBLISH ch2 fuck2
(integer) 1
127.0.0.1:6379> PUBLISH ch3 fuck3
(integer) 0

 

127.0.0.1:6379> SUBSCRIBE ch1 ch2
Reading messages... (press Ctrl-C to quit)
1) "subscribe"
2) "ch1"
3) (integer) 1
1) "subscribe"
2) "ch2"
3) (integer) 2
1) "message"
2) "ch1"
3) "fuck3"
1) "message"
2) "ch2"
3) "fuck3"


127.0.0.1:6379> PSUBSCRIBE ch?*
Reading messages... (press Ctrl-C to quit)
1) "psubscribe"
2) "ch?*"
3) (integer) 1
1) "pmessage"
2) "ch?*"
3) "ch3"
4) "fuck3"
1) "pmessage"
2) "ch?*"
3) "ch2"
4) "fuck3"

 

Five. Others
1. Replication of master-slave database
(1) First start a redis instance./redis-server and then start another instance: ./redis-server --port 6380 --slaveof localhost 6379

The above command can ensure that redis on port 6380 is already the slave database of 6379. Data will be automatically synchronized from the master database to all slave databases (more than one can be set). Once the master database is down, select
one of the slave databases as the master database. The master-slave separation is for the separation of reading and writing, and also to reduce the access pressure of the single-point database server, which is the same as the idea of ​​ordinary databases.

(2) By default, it is not possible to write to the slave database, read-only mode. Can be modified through configuration.

(3) The example opens two cli


./redis-cli -p 6380
127.0.0.1:6380> set foo good
(error) READONLY You can't write against a read only slave.
127.0.0.1:6380> get name
"java"
127.0.0.1:6380> get name1
(nil)
127.0.0.1:6380> get name1
"ghbjnkm"

 

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326922727&siteId=291194637