一 慢查询
慢查询是一个先进先出的队,并且是固定长度的,并保存在内存中。
slowlog-log-slower-than=1000,慢查询的阈值,单位是微妙
#默认值
config get slowlog-max-len = 128
config get slowlog-log-slower-than = 10000
可通过修改配置文件重启(不建议)
动态配置方法
建议阈值值设置为1毫秒,队列长度1000左右
config set slowlog-max-len = 128
config setslowlog-log-slower-than = 1000
其他命令
#1.获取慢查询的队列
slowlog get [n]
#2.获取慢查询队列长度
slowlog len
#3.清空慢查询队列
slowlog reset
二 pipeline
pipline是批量操做命令的一个功能,例如set 1000次,不用循环1000次,每次都做set的操作,这样做的好处是减少每次循环网络的请求时间
三 发布订阅
角色:发布者(redis-cli)、订阅者(redis-ci)、频道(redis-server)
类似于生产者与消费者的模型, 发布者将消息发布到频道,订阅者接收信息
一个订阅者可订阅多个频道,新建的订阅者无法订阅到新建时间之前的消息
消息队列是 只有一个订阅者可以获得消息
API:
1.返回订阅者个数
publish channel message
publish channel1 "hello world"
2.返回订阅者的频道和改频道的订阅个数;以及消息对应的频道及消息内容
subscribe [channel] #订阅一个或多个频道
subscribe channel1
3.取消订阅一个或多个频道
返回 取消订阅的频道;取消订阅频道的订阅者个数
unsubscribe [channel]
unsubscribe channel1
psubscribe ['pattern..'] # 订阅模式
punsubscribe ['pattern..'] # 退订指定的模式
pubsub channels # 列出至少有一个订阅者的频道
pubsub numsub # 列出给定频道的订阅者数量
四 Bitmap (位图)
对字符串中的每个字符位的操作,最大长度为512MB,适合于大规模的数据
API
- 给位图指定索引设置值
setbit key offset value
返回值 是设置前的值
set hello world
getbit hello 0
setbit hello 7 1 #一个字节对应8个位,从0开始,7代表word中w的最后一位
注意 如果设置索引值大于当前位,则之间的位都会补0,如果索引值很大,会非常慢
2.获取位图上指定索引的值
返回值 是当前偏移量的位的值
getbit key offset
getbit hello 7
3.获取位图指定范围值为1的个数(单位为字节,如果不指定就是获取全部)
bitcount key [start end]
bitcount hello
4.多个bitmap的and(交集)、or(并集)、not(非)、xor(异或)操作并将结果保存在destkey中
bitop op destkey key [key....] # op = and | or | not | xor
返回值 是destkey中字节的长度
求两个位图的并集
bitop and key key1 key2
5.计算位图指定范围的第一个偏移量对应的值等于targetBit的位置(单位是字节,如果不指定就是获取全部)
bitpos key targetBit [start] [end]
返回值是 targetBit 的一个位置
bitpos key 1 # 获取key中值是1的第一个位置
五 HyperLogLog
用极小的空间完成独立数量统计
API
1.向hyperloglog中添加元素
pfadd key element [element....]
2.计算独立总数
pfcount key [key.......]
3.合并多个hyperloglog
pfmerge destkey sourcekey [sourcekey.....]
pfadd key1 1 2 3 4
# 返回1 代表添加正常
pfcount key1
# 返回数量 4
pfadd key2 4 5 6 7
# 返回 1
pfmerge key3 key1 key2
# 返回7 (1,2,3,4,5,6,7 重复的值只保留一个)
注意:计算错误率0.81%,但对于很大的值来说存在误差一般是可以接受的
六 GEO(3.2版本以上)
存储经纬度,计算两地距离,范围等
API
1.增加地理位置信息
返回1 表示添加成功
geo key longitude latitude member [longitude latitude member...]
geoadd city:location 116.28 39.55 beijing
2.获取地理位置信息
返回值是精度和纬度
geopos key member [member...]
geopos city:location beijing
3.获取两个地理位置的距离
geolist key member1 member2 [unit]
返回值 距离
unit: m、km、mi(英里)、ft(尺)
geolist city:location beijing tianjin km
4.算出指定范围内的member(通过经纬度)
georadius key longitude latitude unit [withcoord] [withdist] [withhash] [COUNT count] [asc|desc] [store key] [storedist key]
5..算出指定范围内的member(通过一个member)
georadiusbymember key member unit [withcoord] [withdist] [withhash] [COUNT count] [asc|desc] [store key] [storedist key]
withcoord:返回结果中包含经纬度
withdist:返回结果中包含中心节点位置
withhash:返回结果中包含geohash
COUNT count:指定返回结果的数量
asc|desc:返回结果按照距离中心节点的距离做升序或者降序
store key:将结果地理位置信息保存到指定键
storedist key:将返回结果距离中心节点的距离保存到指定键