Redis Watch command
Effect:
Used to monitor one (or more) keys. If this (or these) keys are changed by other commands before the transaction is executed, the transaction will be interrupted.
Usage:
redis 127.0.0.1:6379> WATCH key1 key2
OK
Redis Unwatch command
Effect:
Used to cancel the WATCH command to monitor all keys.
Usage:
redis 127.0.0.1:6379> UNWATCH
OK
Redis Multi command
Effect:
Used to mark the beginning of a transaction block. Several commands in the transaction block are placed in a queue in sequence and executed atomically by the EXEC command.
Usage:
Redis 127.0.0.1:6379 > MULTI # Mark Transaction Start
OK
Redis 127.0.0.1:6379 > INCR user_id
QUEUED
redis 127.0.0.1:6379> INCR user_id
QUEUED
redis 127.0.0.1:6379> INCR user_id
QUEUED
redis 127.0.0.1:6379> PING
QUEUED
Redis 127.0.0.1:6379 > EXEC # Execution
1) (integer) 1
2) (integer) 2
3) (integer) 3
4) PONG
Implementing incr with Watch
The specific methods are as follows:
WATCH mykey
val = GET mykey
val = val + 1
MULTI
SET mykey $val
EXEC
The key is monitored by WATCH command before the value of MyKey is acquired, and then the set command is surrounded in the transaction. This can effectively ensure that each connection is executed before EXEC. If the value of MyKey acquired by the current connection is modified by the client of other connections, then the EXEC command of the current connection will fail to execute. This allows the caller to know if Val has been successfully reset after judging the return value.
Note:
Since the WATCH command only prevents the execution of a transaction after the monitored key value has been modified, it can not guarantee that other clients do not modify the key value, so in general, we need to re-execute the entire function after EXEC fails to execute.
The EXEC command cancels the monitoring of all keys. If you do not want to execute the command in the transaction, you can also use the UNWATCH command to cancel the monitoring.
Example:
Open two redis-cli command-line windows session 1 and session 2
session 1:
Redis 127.0.0.1:6379> set test 1 # Set test= "1"
OK
Redis 127.0.0.1:6379 > get test # Gets the value of test as "1"
"1"
Redis 127.0.0.1:6379> watch test # monitoring test
OK
Redis 127.0.0.1:6379 > multi # open transaction
OK
Redis 127.0.0.1:6379> set test 2 # Set test to "2"
QUEUED
Redis 127.0.0.1:6379> exec # Execution of session 1 after session 2 has been executed, found that the execution failed
(nil)
Redis 127.0.0.1:6379 > get test # Gets the value of test and finds that the value of test is "3" set in session 2.
"3"
Redis 127.0.0.1:6379 > unwatch # Unmonitor all keys
OK
Redis 127.0.0.1:6379 > set test 4 # non-transactional change test value is "4"
OK
Redis 127.0.0.1:6379> get test # get test= "4"
"4"
session 2:
Redis 127.0.0.1:6379 > get test # gets test= "1" created by session 1
"1"
Redis 127.0.0.1:6379> watch test # monitoring test
OK
Redis 127.0.0.1:6379 > multi # open transaction
OK
Redis 127.0.0.1:6379> set test 3 # Set test to "3"
QUEUED
Redis 127.0.0.1:6379> exec # Execution transaction
1) OK
Redis 127.0.0.1:6379> get test # get test= "3"
"3"
summary
Above is the order of watch, multi and so on, which is introduced to you by Editor. I hope it will be helpful to you. If you have any questions, please leave a message for me, and Editor will reply to you in time. Thank you very much for your support to developpaer.