龚哥哥 爱生活、做自己!
redis事务
发表于 2017-6-26 | 浏览(859) | 服务器

一、事务

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"

阅读全文

TOP