一、事务

1、开启事务 multi
2、提交事务 exec
3、取消事务 discard
127.0.0.1:6379> multi
OK
127.0.0.1:6379> set name devil
QUEUED
127.0.0.1:6379> set age 26
QUEUED
127.0.0.1:6379> exec
1) OK
2) OK
127.0.0.1:6379> get sex
"2"
127.0.0.1:6379> get age
"26"
127.0.0.1:6379>
127.0.0.1:6379> multi
OK
127.0.0.1:6379> set a 100
QUEUED
127.0.0.1:6379> del age
QUEUED
127.0.0.1:6379> discard
OK
127.0.0.1:6379> get age
"26"
127.0.0.1:6379> get a
(nil)
127.0.0.1:6379>

二、乐观锁

client-1 开启watch,并在事务中修改name
127.0.0.1:6379> get name
"mogui"
127.0.0.1:6379> watch name
OK
127.0.0.1:6379> multi
OK
127.0.0.1:6379> set name devil
QUEUED
127.0.0.1:6379> exec
(nil)

client-2 修改name
127.0.0.1:6379> get name
"mogui"
127.0.0.1:6379> get name
"mogui"
127.0.0.1:6379> set name gong
OK
127.0.0.1:6379> get name
"gong"

client-1 执行exec
127.0.0.1:6379> exec
(nil)
127.0.0.1:6379> get name
"gong"

三、发布及订阅消息

client-1 订阅devil频道
127.0.0.1:6379> subscribe devil
Reading messages... (press Ctrl-C to quit)
1) "subscribe"
2) "devil"
3) (integer) 1

client-2 发布频道devil消息

 127.0.0.1:6379> publish devil hello (integer) 1
client-1 订阅频道接收到的消息
1) "message"
2) "devil"
3) "hello"
---------------------------------------------------------------------------------------------
不忘初心 方得始终!

唯有志存高远,方能风行天下。

道之所存,虽千万人吾往矣! 情之所钟,虽千万里吾念矣~

原创作品,允许转载,转载时请务必以超链接形式标明文章 原始出处 、作者信息和本声明。否则将追究法律责任。