(三)Redis应用功能

1、慢查询

配置:

  1. slowlog-max-len
    1. 先进先出队列
    2. 固定长度
    3. 保存在内存内
  2. slowlog-log-slower-than
    1. 慢查询阀值(单位:微妙)
    2. slowlog-log-slower-than = 0:所以慢查询都记录
# 默认值
config get slowlog-max-len = 128
config get slowlog-log-slower-than = 10000
# 设置值
config get slowlog-max-len = 1000	(建议1ms)
config get slowlog-log-slower-than = 1000	(建议设置1000左右)
# 建议定期持久化慢查询

获取:

1、slowlog get [n]:获取慢查询队列

2、slowlog len:获取慢查询队列长度

2、流水线

减少网络传输时间开销

1、n个命令操作 = n次网络 + n次命令执行

2、流水线 = 1次网络操作 + n次命令执行

客户端pipeline操作:

Jedis jedis = new Jedis("127.0.0.1", 6379);
for (int i = 0; i < 100; i++) {
    Pipeline pipeline = jedis.pipelined();
    for (int j = i * 100; j < (i + 1) * 100; j++) {
        pipeline.hset("hashkey", "field" + j, "value" + j);
    }
    pipeline.syncAndReturnAll();
}

3、发布订阅

订阅:

127.0.0.1:6379> subscribe channel1
Reading messages... (press Ctrl-C to quit)
1) "subscribe"
2) "channel1"
3) (integer) 1
1) "message"
2) "channel1"
3) "hello"
1) "message"
2) "channel1"
3) "hello"

发布:

# 返回的是收到消息的观察者数量
127.0.0.1:6379> publish channel1 hello
(integer) 1

api:

1、psubscribe [pattern...] # 订阅模式
2、punsubscribe [pattern...] # 退订指定的模式
3、pubsub channels # 列出至少有一个订阅者的频道
4、pubsub numsub [channel...] # 列出给定频道的订阅者数量

4、bitmap(位图)

redis可以直接操作位

# 设置偏移量
setbit key offset value
# 获取偏移量
getbit key offset
# 获取位图指定范围(start end 单位为字节,不设置则为全部)位值为1的个数
bigcount key [start end]

持久化

1、RDB

快照:生成RDB二进制文件

save		 # 同步持久化(会造成阻塞)
bgsave	 	 # 异步持久化(消耗额外的内存)
配置		    # 自动持久化

策略:

文件策略:如果存在老的RDB文件,新替换老
复杂度:O(N)

配置:

save 900 1
save 300 10
save 60 10000
dbfilename dump-${port}.rdb
dir ./bigdiskpath
stop-write-on-bgsave-error yes	# 写入错误停止写入
rdbcompression yes	# 使用压缩方式
rdbchecksum yes	# 使用检查和

2、AOF

日志:将操作命令写入到AOF文件中

策略:

always: 写命令缓冲区 -> 每条命令都写入到缓冲区
everysec: 每秒写一次
no: 根据操作系统决定

配置:

auto-aof-rewrite-min-size	# AOF文件重写需要的尺寸
auto-aof-rewrite-percentage	# AOF文件增长率
aof_current_size			# AOF当前尺寸
aof_base_size				# AOF上次启动和重写的尺寸(单位:字节)
appendonly yes				# 
appendonlyfilename	"appendonly-${port}.aof"	#
appendsync	everysec
dir /bigdiskpath
no-appendfsync-on-rewrite yes	#

触发机制:

aof_current_size > auto-aof-rewrite-min-size	# 大小触发
aof_current_size - aof_base_size > auto-aof-rewrite-percentage 	# 增长率触发

3、持久化策略选择

命令 RDB AOF
启动优先级
体积
恢复速度
数据安全性 丢数据 根据策略决定
轻重
posted @ 2019-04-03 11:34  zuier~  阅读(149)  评论(0编辑  收藏  举报