redis message mode, transaction, slow query log, backup and recovery

1.redis message mode

Dissemination of information are usually divided into two modes: queue mode (queuing) and publish subscribe model (publish-subscribe).

Queue mode, while Consumers can read messages from the server, where each message is only read a consumer.

Subscription model release message is broadcast to all of the consumer, topic of the message will be assigned to a member of the group in. The consumer may be the same group in a different program, you may be on different machines.

Subscribe publication instance:

subscribe mq1 # client

publish mq1 “Redis is a great caching technique”

 

psubscribe subscribe to one or more channels that match a given pattern

psubscribe news "tech."

publish channel message

Send message to the specified channel channel. The return value represents the number of consumers

pubsub channels show subscriptions

pubsub numsub news print subscribers on each channel

punsubscribe unsubscribe multiple channels

Subscribe to subscribe to the information given in one or more channels

unsubscribe unsubscribe Channel

2, Redis affairs

A transaction is a single isolation operations: all command sequence of a transaction are performed in sequence.

During execution of a transaction, the command will not be sent to other client requests interrupted.

Atomic: A transaction either all commands are executed or not executed all.

Implementation process:

  Begin transaction

  Command into the team

  The enforcement branch

Transaction command:

discard cancel the transaction, giving up all the commands in a transaction block

exec execute commands within blocks of all transactions

multi mark the start of a block transaction

Key Watch [Key ...] monitor one or more key, if this (or these) key changes are the other commands before re-execute the transaction, then the transaction will be interrupted.

unwatch 取消watch命令对所有key的监视

实例:

127.0.0.1:6380> zadd salary 3000 zhangsan 5000 lisi
(integer) 2
127.0.0.1:6380> multi
OK
127.0.0.1:6380> zincrby salary 1000 zhangsan
QUEUED
127.0.0.1:6380> zincrby salary -1000 lisi
QUEUED
127.0.0.1:6380> exec
1) "4000"
2) "4000"
127.0.0.1:6380> zrange salary 0 -1 withscores
1) "lisi"
2) "4000"
3) "zhangsan"
4) "4000"
3.服务器命令

info  查看服务器配置信息

client list  查看当前连接客户端ip及端口

client kill ip:port 关闭客户端ip及端口

config get *

config resetstat 重置统计

config get/set 动态修改

dbsize

flushall 清空所有数据 select 1

flushdb 清空当前库

monitor 监控实时指令

127.0.0.1:6380> monitor
OK
1583403427.636228 [0 127.0.0.1:43648] "set" "name" "123"
1583403432.837921 [0 127.0.0.1:43648] "get" "name"

shutdown 关闭服务器

save 将当前数据保存

slaveof host:port 主从配置

slaveof no one

sync 主从同步

role返回从角色

4、慢日志查询:

slow log是redis用来记录查询执行时间的日志系统

slow log保存在内存里面,读写速度非常快

可以通过改写redis.conf文件或者用config get和config set命令对它们动态的进行修改

slowlog-log-slower-than 10000 超过多少微秒

config set slowlog-log-slower-than 100

config set slowlog-max-len 1000 保存多少慢日志

config get slow*    //查询当前慢日志

127.0.0.1:6380> config get slow*
1) "slowlog-log-slower-than"
2) "10000"
3) "slowlog-max-len"
4) "128"

slowlog get

slowlog reset

5.备份数据

config get dir 获取当前目录

save备份(无持久化策略时),生成时在redis当前目录中。

恢复时只需要将dump.rdb放入redis当前目录

127.0.0.1:6380> set name zhangsan
OK
127.0.0.1:6380> save
OK
关闭redis数据库后,备份直接拷贝生成的dump.rdb数据文件即可

还原数据时,将之前备份的dump.rdb数据文件拷贝回数据库数据文件夹,重启数据库即可

 

Guess you like

Origin www.cnblogs.com/Simplelearning/p/12424103.html