Redis combat -Redis Affairs

Redis transaction from start to execution will go through the following three stages:

  • Begin transaction
  • Command into the team
  • The enforcement branch

1. Start a transaction MULTI
2 is placed in a batch operation before sending queue buffer EXEC command.
3. Upon receipt EXEC command to enter the transaction execution, transaction arbitrary command execution fails, the rest of the command is still being executed during the execution of a transaction, other command request submitted by the client will not be inserted into the transaction execution command sequence.

Redis transaction commonly used commands:

command Described by Examples and
MULTI It marks the start of a block transaction
EXEC Execute commands within blocks of all transactions
DISCARD Cancel the transaction, giving up all the commands in a transaction block
WATCH key [key ...] A monitor (or more) key, if this (or these) key changes to other orders are executed before the transaction, then the transaction will be interrupted
UNWATCH Cancel WATCH command to monitor all the key

Example:

#使用multi标记一个事务的开始
127.0.0.1:6379> multi
OK
#设置一个key-value
127.0.0.1:6379> set key1 SUNDAY
QUEUED
127.0.0.1:6379> set key2 XI~AN
QUEUED
127.0.0.1:6379> set key3 PLAY
QUEUED
#执行当前事务
127.0.0.1:6379> exec
1) OK
2) OK
3) OK
#查看刚才插入的键值
127.0.0.1:6379> get key1
"SUNDAY"
127.0.0.1:6379> get key2
"XI~AN"
127.0.0.1:6379> get key3
"PLAY"
127.0.0.1:6379>

At the same time you can also use visualization tools Redis Redis Desktop Manager tool to view the data just inserted in redis:

16750882-aee3dac2c49e63b1.png
image.png

The next section describes the installation and use of Redis Desktop Manager

DISCARD commands use

127.0.0.1:6379> multi
OK
127.0.0.1:6379> set a 1
QUEUED
127.0.0.1:6379> set b 2
QUEUED
127.0.0.1:6379> set c 3
QUEUED
# 取消事务,放弃执行事务块内的所有命令
127.0.0.1:6379> DISCARD
OK
127.0.0.1:6379>

WATCH and UNWATCH
two command before turning on the transaction, watch can monitor the dynamic key when the key is being monitored changes in other commands, the current transaction will be terminated, unwatch canceled monitor objects.

# 监听上述key1的值
127.0.0.1:6379> watch key1
OK
# 查看当前key1的值
127.0.0.1:6379> get key1
"wednesday"
# 修改key1的值为a
127.0.0.1:6379> set key1 a
OK
# 开始事务
127.0.0.1:6379> multi
OK
# 在事务中修改key1的值为2
127.0.0.1:6379> set key1 2
QUEUED
#执行事务,发现执行失败
127.0.0.1:6379> exec
(nil)
# 获取key1的值,发现还是在开启事务之前的a
127.0.0.1:6379> get key1
"a"
127.0.0.1:6379>

WATCH command can monitor one or more keys, once one of which keys were modified (or deleted), after the transaction will not be executed. Monitoring continued until EXEC command (transaction command is executed only after the EXEC, it is possible to modify the key WATCH monitoring after EXEC command) while using UNWATCH can cancel the monitoring, will not repeat them here.

Redis to ensure that all the commands in a transaction are either executed or not executed. If the EXEC command before sending the client disconnected, Redis will clear the transaction queue, all the commands in the transaction will not be executed. Once the client sends the EXEC command, all of the command will be executed, even if the client disconnected thereafter does not matter, because Redis command has been recorded for all to be executed. In addition, Redis transaction can ensure the command in order to perform a transaction without being inserted into the other commands. Imagine client A needs to perform a few commands, and the client B sends a command without using a transaction, the client command B may be inserted into several Client A command execution. If you do not want this to happen, you can also use the transaction.

Not afraid to start from scratch, I'm afraid never go!

References:
Rookie Tutorial: https://www.runoob.com/redis/redis-keys.html
"Redis real" Josiah L. Carlson the

Guess you like

Origin blog.csdn.net/weixin_34059951/article/details/90999394