[] Redis Redis Affairs

Redis Affairs introduced

  Redis transaction can execute multiple commands once, and with the following three important guarantee:

  • It is placed in a batch operation before sending queue buffer EXEC command.
  • After receiving 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.

  From start to execute a transaction will go through the following three stages:

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

Examples

  The following is an example of a transaction, it is the first to  MULTI  begin a transaction, then multiple commands into teams to the transaction, and finally by the  EXEC  triggering transaction commands, together with all the commands in the transaction:

 1 127.0.0.1:6379> MULTI
 2 OK
 3 127.0.0.1:6379> SET book-name "Mastering C++ in 21 days"
 4 QUEUED
 5 127.0.0.1:6379> GET book-name
 6 QUEUED
 7 127.0.0.1:6379> SADD tag "C++" "Programming" "Mastering Series"
 8 QUEUED
 9 127.0.0.1:6379> SMEMBERS tag
10 QUEUED
11 127.0.0.1:6379> EXEC
12 1) OK
13 2) "Mastering C++ in 21 days"
14 3) (integer) 3
15 4) 1) "Mastering Series"
16    2) "Programming"
17    3) "C++"

  a, execution of a single command Redis is atomic, but without adding any Redis mechanism for maintaining atomicity in a transaction, the transaction is not performed Redis atomic.

  Transactions can be understood execute the script as a packaged volume, but the bulk of the instruction is not atomic operation, failure middle of an instruction (an exception is similar to running java 1/0) does not result in a rollback instructions previously done, also do not cause subsequent instruction.

  This is the official online description Redis docs ON the From  Transactions :

It's important to note that even when a command fails, all the other commands in the queue are processed – Redis will not stop the processing of commands.

  such as:

 1 127.0.0.1:6379> multi
 2 OK
 3 127.0.0.1:6379> set a aaa
 4 QUEUED
 5 127.0.0.1:6379> incr a
 6 QUEUED
 7 127.0.0.1:6379> set b bbb
 8 QUEUED
 9 127.0.0.1:6379> exec
10 1) OK
11 2) (error) ERR value is not an integer or out of range
12 3) OK
13 127.0.0.1:6379> get a 
14 "aaa"
15 127.0.0.1:6379> get b
16 "bbb"

  If it fails in incr a place, set a success would not have been rolled back, set b will continue.

  b, if there is a command error in transaction queue (similar to the java compiler error) , then the EXEC command, all commands are not executed

 1 127.0.0.1:6379> multi
 2 OK
 3 127.0.0.1:6379> set a aaa
 4 QUEUED
 5 127.0.0.1:6379> sett b bbb
 6 (error) ERR unknown command `sett`, with args beginning with: `b`, `bbb`, 
 7 127.0.0.1:6379> set c ccc
 8 QUEUED
 9 127.0.0.1:6379> exec
10 (error) EXECABORT Transaction discarded because of previous errors.
11 127.0.0.1:6379> get a
12 (nil)

Redis transaction command

  1, Redis Discard command is used to cancel the transaction, giving up all the commands in a transaction block.

    Syntax: DISCARD

1 redis 127.0.0.1:6379> MULTI
2 OK
3 redis 127.0.0.1:6379> PING
4 QUEUED
5 redis 127.0.0.1:6379> SET greeting "hello"
6 QUEUED
7 redis 127.0.0.1:6379> DISCARD
8 OK

  2, Redis Exec command to perform all transactions in the command block.

    Syntax: Exec

    As usage examples

  3, Redis Multi command is used to mark the start of a block transaction.

    A plurality of commands within the block transaction based on the order which is put into a queue, and finally by atomic EXEC command (Atomic) is performed.

    Syntax: Multi

    As usage examples

  4, Redis Unwatch command to remove the WATCH command to monitor all the key.

    Syntax: unwatch 

1 127.0.0.1:6379> WATCH lock lock_times
2 OK
3 127.0.0.1:6379> UNWATCH
4 OK

  5, Redis Watch for monitoring a command (or more) Key, if the transaction before executing this (or these) additional command Key are altered, then the transaction will be interrupted

    Syntax: WATCH Key [ Key ...]

1 127.0.0.1:6379> WATCH lock lock_times
2 OK

 

Guess you like

Origin www.cnblogs.com/h--d/p/11469012.html