Redis news release and subscription and transaction mechanism

Redis news release and subscription and transaction mechanism

Subscribe released

SUBSCRIBE PUBLISH

Subscribe to the message queue and post messages.

# 首先要打开redis-cli shell窗口 一个用于消息发布 一个用于消息订阅
# SUBSCRIBE 订阅一个频道,如果频道不存在 就新增一个
# 返回参数 表示 第一个是命令 第二个是频道名称 第三个表示当前订阅该频道的数量
127.0.0.1:6379> SUBSCRIBE  mychannel
Reading messages... (press Ctrl-C to quit)
1) "subscribe"
2) "mychannel"
3) (integer) 1

# 在另外一个shell窗口 进行消息发布
127.0.0.1:6379> PUBLISH mychannel "hello world"
(integer) 1

# 在回到之前的shell 窗口 我们可以看到消息已经被订阅成功了
Reading messages... (press Ctrl-C to quit)
1) "subscribe"
2) "mychannel"
3) (integer) 1
# 下面是本次接受到消息
1) "message"
2) "mychannel"
3) "hello world"

UNSUBSCRIBE

UNSUBSCRIBE: unsubscribe.

# 第三个返回值表示当前没有订阅者订阅该频道
127.0.0.1:6379> UNSUBSCRIBE mychannel
1) "unsubscribe"
2) "mychannel"
3) (integer) 0

PSUBSCRIBE PUNSUBSCRIBE

According households subscribe or unsubscribe wildcard.

127.0.0.1:6379> PSUBSCRIBE my*
Reading messages... (press Ctrl-C to quit)
1) "psubscribe"
2) "my*"
3) (integer) 1
# 在另外一个Shell发布消息
127.0.0.1:6379> PUBLISH mychannel "hello world"
(integer) 1
127.0.0.1:6379> PUBLISH mynewchannel "hello world new"
(integer) 1
# 此时第一个Shell窗口就可以到如下的订阅消息
1) "pmessage"
2) "my*"
3) "mychannel"
4) "hello world"
1) "pmessage"
2) "my*"
3) "mynewchannel"
4) "hello world new"

Affairs

Redis support services, is based on four basic commands.

  • MULTI open affairs
  • EXEC enforcement branch
  • DISCARD abandon Affairs
  • WATHC monitoring

MULTI EXEC

MULTI command to open a business, after performing MULTI, clients can send multiple instructions to the server, these instructions will not be executed immediately, but is queued, when executed EXEC, all the commands in the queue will be executed.

127.0.0.1:6379> MULTI
OK
127.0.0.1:6379> INCR age
QUEUED
127.0.0.1:6379> INCRBY age 3
QUEUED
127.0.0.1:6379> EXEC
1) (integer) 1
2) (integer) 4
127.0.0.1:6379> get age
"4"
127.0.0.1:6379> del age 
(integer) 1
127.0.0.1:6379> MULTI
OK
127.0.0.1:6379> set age 18
QUEUED
127.0.0.1:6379> INCR age
QUEUED
127.0.0.1:6379> INCRBY age 3
QUEUED
127.0.0.1:6379> EXEC
1) OK
2) (integer) 19
3) (integer) 22
127.0.0.1:6379> get age
"22"
127.0.0.1:6379> 

Transaction error is

  • If you are into the team when it is being given, even if the input EXEC command, the transaction will not be executed.
  • If the team is successful, but an error when executed, it will execute the proper instruction, the error will be skipped, it does not terminate the transaction.
## 事务中出现错误的情况 入队错误 事务不执行
127.0.0.1:6379> get age
"22"
127.0.0.1:6379> MULTI
OK
127.0.0.1:6379> INCR age
QUEUED
# 写错指令 出现错误提示
127.0.0.1:6379> INCR age 3
(error) ERR wrong number of arguments for 'incr' command
127.0.0.1:6379> EXEC
(error) EXECABORT Transaction discarded because of previous errors.
127.0.0.1:6379> get age
"22"

##  事务中出现错误的情况 入队正确 执行错误 事务正常执行
127.0.0.1:6379> get age
"22"
127.0.0.1:6379> MULTI
OK
127.0.0.1:6379> INCR age
QUEUED
127.0.0.1:6379> INCRBY age abc
QUEUED
127.0.0.1:6379> INCRBY age 4
QUEUED
127.0.0.1:6379> exec
1) (integer) 23
2) (error) ERR value is not an integer or out of range
3) (integer) 27
127.0.0.1:6379> get age
"27"

Examples can be seen by the second error, Redis transaction rollback is not supported. This is because the Redis command is considering the failure is caused by programming errors, and these errors should be found in the development process, in addition Redis does not support rollback able to maintain internal Redis can be kept simple and fast.

DISCARD

Abandon the transaction.

127.0.0.1:6379> get age
"22"
127.0.0.1:6379> MULTI
OK
127.0.0.1:6379> INCR age
QUEUED
127.0.0.1:6379> DISCARD
OK
127.0.0.1:6379> get age
"22"

WATCH

Redis the CAS operation. WATCH makes EXEC command needs to be executed conditionally: a transaction can only be executed at all monitored keys have not been modified under the premise, if this premise is not met, the transaction will not be executed. When the EXEC command, WATCH on the key will be canceled.

WATCH is the key would be "monitored", if there is any one or more of the monitored key is modified before the EXEC execution, the entire transaction will be canceled, EXEC returns nil-reply to indicate that the transaction has failed.

# 第一个窗口
127.0.0.1:6379> set age 18
OK
127.0.0.1:6379> WATCH age
OK
127.0.0.1:6379> MULTI
OK
127.0.0.1:6379> set age 19
QUEUED
# 在第二个窗口 设置
127.0.0.1:6379> set age 20
OK
127.0.0.1:6379> get age
"20"
# 在第一个窗口执行事务 因为age已经被更改了,所以本次事务执行失败
127.0.0.1:6379> EXEC
(nil)


## 被监控的Key没有变化时 事务可以正确执行
127.0.0.1:6379> get age
"20"
127.0.0.1:6379> WATCH age
OK
127.0.0.1:6379> MULTI
OK
127.0.0.1:6379> set age 25
QUEUED
127.0.0.1:6379> exec
1) OK
127.0.0.1:6379> get age
"25"

These are subscriptions to the publication in Redis and affairs of common operating instructions, and more other instructions can refer to the official website, Redis official website , Thanks for reading, I hope for your help.

Guess you like

Origin www.cnblogs.com/enjoyitlife/p/11972110.html