redis的高级使用

1 高级api使用

1.1 慢查询(假设redis性能不高了,如何取排除)

	慢查询指的是命令执行时长比较长的查询。通过slowlog get命令获取慢查询日志;通过slowlog len命令获取慢查询日志的长度;通过slowlog reset命令清理慢查询日志。通过slowlog-log-slower-than配置命令执行时长的阈值;通过slowlog-max-len配置慢查询日志最多存储的条数
config set slowlog-log-slower-than 1000   #毫秒
config set slowlog-max-len 1200     #记录最大条数
config rewrite   #写到配置文件里里面

2 pipline和watch

2.2.1python客户端实现pipline

import redis
pool = redis.ConnectionPool(host='10.211.55.4', port=6379)
r = redis.Redis(connection_pool=pool)
# pipe = r.pipeline(transaction=False)
#创建pipeline
pipe = r.pipeline(transaction=True)
#开启事务
pipe.multi()
pipe.set('name', 'lqw')
#其他代码,可能出异常
pipe.set('role', 'nb')
pipe.execute()

2.2.2 watch实现乐观锁

# 1 mutil  开启事务,放到管道中一次性执行
multi   # 开启事务
set name lqz
set age 18
exec
# 2 模拟事务
# 在开启事务之前,先watch
wathc age
multi
decr age
exec
# 另一台机器
mutil
decr age
exec  # 先执行,上面的执行就会失败(乐观锁,被watch的事务不会执行成功)

3发布订阅

发布者发布了消息,所有的订阅者都可以收到,就是生产者消费者模型(后订阅了,无法获取历史消息)

publish channel message #发布命令
ex: publish souhu:tv "hello world" #在souhu:tv频道发布一条hello world  返回订阅者个数
subscribe [channel] #订阅命令,可以订阅一个或多个
ex: subscribe souhu:tv  #订阅sohu:tv频道

unsubscribe [channel] #取消订阅一个或多个频道
ex: unsubscribe sohu:tv  #取消订阅sohu:tv频道
    
psubscribe [pattern...] #订阅模式匹配
ex: psubscribe c*  #订阅以c开头的频道

unpsubscribe [pattern...] #按模式退订指定频道
pubsub channels #列出至少有一个订阅者的频道,列出活跃的频道
pubsub numsub [channel...] #列出给定频道的订阅者数量
pubsub numat #列出被订阅模式的数量
使用场景:
	实时消息系统,比如普通的即时聊天,群聊等功能。

4 位图 (本质就是字符串)

set hello big #放入key位hello 值为big的字符串
getbit hello 0 #取位图的第0个位置,返回0
getbit hello 1 #取位图的第1个位置,返回1 如上图

##我们可以直接操纵位
setbit key offset value #给位图指定索引设置值
setbit hello 7 1 #把hello的第7个位置设为1 这样,big就变成了cig

setbit test 50 1 #test不存在,在key为test的value的第50位设为1,那其他位都以0补

bitcount key [start end] #获取位图指定范围(start到end,单位为字节,注意按字节一个字节8个bit为,如果不指定就是获取全部)位值为1的个数

bitop op destkey key [key...] #做多个Bitmap的and(交集)/or(并集)/not(非)/xor(异或),操作并将结果保存在destkey中 
bitop and after_lqz lqz lqz2 #把lqz和lqz2按位与操作,放到after_lqz中

bitpos key targetBit start end #计算位图指定范围(start到end,单位为字节,如果不指定是获取全部)第一个偏移量对应的值等于targetBit的位置
bitpos lqz 1 #big 对应位图中第一个1的位置,在第二个位置上,由于从0开始返回1
bitpos lqz 0 #big 对应位图中第一个0的位置,在第一个位置上,由于从0开始返回0
bitpos lqz 1 1 2 #返回9:返回从第一个字节到第二个字节之间 第一个1的位置,看上图,为9

使用场景:
     1.独立用户统计

5 HyperLogLog

pfadd key element #向hyperloglog添加元素,可以同时添加多个
pfcount key #计算hyperloglog的独立总数
pfmerge destroy sourcekey1 sourcekey2#合并多个hyperloglog,把sourcekey1和sourcekey2合并为destroy

pfadd uuids "uuid1" "uuid2" "uuid3" "uuid4" #向uuids中添加4个uuid
pfcount uuids #返回4
pfadd uuids "uuid1" "uuid5"#有一个之前存在了,其实只把uuid5添加了
pfcount uuids #返回5

pfadd uuids1 "uuid1" "uuid2" "uuid3" "uuid4"
pfadd uuids2 "uuid3" "uuid4" "uuid5" "uuid6"
pfmerge uuidsall uuids1 uuids2 #合并
pfcount uuidsall #统计个数 返回6
使用场景:
    统计注册 IP 数
    统计每日访问 IP 数
    统计页面实时 UV 数
    统计在线用户数
    统计用户每天搜索不同词条的个数

6 geo(地理信息定位)

geoadd key longitude latitude member #增加地理位置信息
geoadd cities:locations 116.28 39.55 beijing #把北京地理信息天津到cities:locations中
geoadd cities:locations 117.12 39.08 tianjin
geoadd cities:locations 114.29 38.02 shijiazhuang
geoadd cities:locations 118.01 39.38 tangshan
geoadd cities:locations 115.29 38.51 baoding
    
geopos key member #获取地理位置信息
geopos cities:locations beijing #获取北京地理信息

geodist key member1 member2 [unit]#获取两个地理位置的距离 unit:m(米) km(千米) mi(英里) ft(尺)
geodist cities:locations beijing tianjin km #北京到天津的距离,89公里

georadius key logitude latitude radiusm|km|ft|mi [withcoord] [withdist] [withhash] [COUNT count] [asc|desc] [store key][storedist key]

georadiusbymember key member radiusm|km|ft|mi [withcoord] [withdist] [withhash] [COUNT count] [asc|desc] [store key][storedist key]
#获取指定位置范围内的地理位置信息集合
'''
withcoord:返回结果中包含经纬度
withdist:返回结果中包含距离中心节点位置
withhash:返回解雇中包含geohash
COUNT count:指定返回结果的数量
asc|desc:返回结果按照距离中心店的距离做升序/降序排列
store key:将返回结果的地理位置信息保存到指定键
storedist key:将返回结果距离中心点的距离保存到指定键
'''
georadiusbymember cities:locations beijing 150 km
'''
1) "beijing"
2) "tianjin"
3) "tangshan"
4) "baoding"
'''

注意:
    geo本质时zset类型
	可以使用zset的删除,删除指定member:zrem cities:locations beijing
永远不要高估自己
原文地址:https://www.cnblogs.com/liqiangwei/p/14449923.html