[Redis-CentOS7]Redis事务操作(六)

事务操作

  • 隔离操作: 事务中所有的命令都会序列化,按顺序执行,不会被其他命令打扰
  • 原子操作: 事务中所有的命令要么全部执行,要么全部不执行

    添加事务并执行

127.0.0.1:6379> MULTI # 开启事务
OK
127.0.0.1:6379> INCR user_id # user_id+1
QUEUED
127.0.0.1:6379> INCR user_id
QUEUED
127.0.0.1:6379> INCR user_id
QUEUED
127.0.0.1:6379> PING # 发送信息
QUEUED
127.0.0.1:6379> EXEC
1) (integer) 1
2) (integer) 2
3) (integer) 3
4) PONG

DISCARD退出事务

127.0.0.1:6379> MULTI
OK
127.0.0.1:6379> set username 'peigy'
QUEUED
127.0.0.1:6379> set password 'password'
QUEUED
127.0.0.1:6379> PING "set ok"
QUEUED
127.0.0.1:6379> DISCARD
OK

监听防止事务修改

127.0.0.1:6379> set lock lock_times
OK
127.0.0.1:6379> watch lock lock_times
OK
127.0.0.1:6379> keys *
1) "user_id"
2) "lock"
127.0.0.1:6379> MULTI
OK
127.0.0.1:6379> set lock lock_times1
QUEUED
127.0.0.1:6379> EXEC
(nil)

解除监听

127.0.0.1:6379> UNWATCH
OK

参考网站:http://redisdoc.com/transaction/index.html

相关推荐