command example: 1. The transaction is performed normally:
#在Shell命令行下执行Redis的客户端工具.
/> REDIS-CLI
#在当前连接上启动一个新的事务.
Redis 127.0.0.1:6379>multi
Ok
#执行事务中的第一条命令, as can be seen from the return result of this command, the command is not executed immediately, but is stored in the command queue of the transaction.
Redis 127.0.0.1:6379>INCR T1
QUEUED
#又执行一个新的命令, it can be seen from the result that the command is also stored in the command queue of the transaction.
Redis 127.0.0.1:6379>INCR T2
QUEUED
#执行事务命令队列中的所有命令, as you can see from the results, the results of the command in the queue are returned.
Redis 127.0.0.1:6379>exec
1) (integer) 1
2) (integer) 1
2. There are failed commands in the transaction:
#开启一个新的事务.
Redis 127.0.0.1:6379>multi
Ok
#设置键a的值为string类型的3.
Redis 127.0.0.1:6379>set a 3
QUEUED
#从键a所关联的值的头部弹出元素, because the value is a string type, and the Lpop command can only be used with the list type, the command will fail when the EXEC command is executed.
Redis 127.0.0.1:6379>lpop A
QUEUED
#再次设置键a的值为字符串4.
Redis 127.0.0.1:6379>set a 4
QUEUED
#获取键a的值 to confirm that the value was successfully set by the second set command in the transaction.
Redis 127.0.0.1:6379>get A
QUEUED
#从结果中可以看出, the second command in a transaction fails Lpop execution, and the subsequent set and get commands are successful, which is the most important difference between a Redis transaction and a transaction in a relational database.
Redis 127.0.0.1:6379>exec
1) OK
2) (Error) ERR operation against a key holding the wrong kind of value
3) OK
4) "4"
3. Rolling back a transaction:
#为键t2设置一个事务执行前的值.
Redis 127.0.0.1:6379>set T2 TT
Ok
#开启一个事务.
Redis 127.0.0.1:6379>multi
Ok
#在事务内为该键设置一个新值.
Redis 127.0.0.1:6379>set T2 ttnew
QUEUED
#放弃事务.
Redis 127.0.0.1:6379>discard
Ok
#查看键t2的值, you can see from the result that the value of the key is still the value before the start of the transaction.
Redis 127.0.0.1:6379>get T2
"TT" more exciting content please pay attention to: http://bbs.superwu.cn attention to the Superman Academy QR Code: |
Example redis things command