四,redis常⽤的数据类型--set和zset set和zset都是集合
set是对string类型的⽆序集合,zset是有序的集合
set中的值是不能重复的;
是否是集合是否有序有序实现⽅式应⽤场景
set是否⽆标签,社交
zset是是分值排⾏系统,社交
⼀,set常⽤指令
set是不允许有重复的值存在的。
1)sadd和smembers t添加set和获取set中的值
2) sismember 还可以判断set中是否包含某个字符串
127.0.0.1:6379> sadd s laoli
(integer) 1
127.0.0.1:6379> sadd s laowang
(integer) 1
127.0.0.1:6379> sadd s laozhang
(integer) 1
127.0.0.1:6379> sadd s laohao
(integer) 1
127.0.0.1:6379> SMEMBERS s
1) "laohao"
2) "laowang"
3) "laozhang"
4) "laoli"
127.0.0.1:6379> SMEMBERS s
1) "laohao"
2) "laowang"
3) "laozhang"
4) "laoli"
127.0.0.1:6379> SISMEMBER s laohao
(integer) 1
127.0.0.1:6379> SISMEMBER s laohao1
(integer) 0
3)scard 返回集合中的元素总数
127.0.0.1:6379> SMEMBERS s
1) "laohao"
2) "laowang"
3) "laozhang"
4) "laoli"
127.0.0.1:6379> scard s
(integer) 4
4)srem移除集合中的⼀个或多个字符串
127.0.0.1:6379> SMEMBERS s
1) "laohao"
2) "laowang"
3) "laozhang"
4) "laoli"
127.0.0.1:6379> srem s laohao laoli
(integer) 2
127.0.0.1:6379> SMEMBERS s
1) "laozhang"
2) "laowang"
5)srandmember 随机抽取集合中的⼀个或者多个元素
127.0.0.1:6379> SMEMBERS s
1) "laowang"
2) "laok"
3) "laozhang"
4) "laoli"
5) "laob"
127.0.0.1:6379> SRANDMEMBER s 1
1) "laowang"
127.0.0.1:6379> SRANDMEMBER s 1
1) "laob"
127.0.0.1:6379> SRANDMEMBER s 1
1) "laozhang"
127.0.0.1:6379> SRANDMEMBER s 2
1) "laozhang"
2) "laoli"
6)spop 移除并返回集合中的⼀个随机元素
spop 应该是随机移除集合中的⼀个元素并且返回⼀个移除的值
127.0.0.1:6379> SMEMBERS s
1) "laozhang"
2) "laowang"
3) "laoli"
4) "laok"
5) "laob"
127.0.0.1:6379> spop s
"laob"
127.0.0.1:6379> spop s
"laowang"
127.0.0.1:6379> SMEMBERS s
1) "laozhang"
2) "laoli"
3) "laok"
7)smove 将⼀个set中的元素移动到另⼀个set中
⽐如,讲s中的test移动到s1中
⽐如在抽奖系统中,讲⼀等奖中奖⽤户放到⼀等奖的名单中,⼆等奖的⽤户放在⼆等奖的名单中。127.0.0.1:6379> SMEMBERS s
1) "laohao"
2) "laozhang"
3) "test"
4) "laowang"
5) "laoli"
6) "laok"
127.0.0.1:6379> SMEMBERS s1
1) "laojiao"
2) "laowang"
3) "laozhang"
4) "laoli"
127.0.0.1:6379> smove s s1 test
(integer) 1
127.0.0.1:6379> SMEMBERS s1
1) "laojiao"
2) "laowang"
3) "laozhang"
4) "laoli"
5) "test"
8)set是⼀个集合,肯定会有交集,并集和差集;
sdiff 查看两个集合不同的元素--差集
sinter 查看两个集合中相同的元素---交集
sunion 两个集合中所有的元素---并集
127.0.0.1:6379> SMEMBERS s
1) "laohao"
2) "laozhang"
3) "laowang"
4) "laoli"
5) "laok"
127.0.0.1:6379> SMEMBERS s1
1) "laojiao"
2) "laowang"
3) "laozhang"
127.0.0.1:6379> sdiff s s1
1) "laohao"
2) "laok"
127.0.0.1:6379> SINTER s s1
1) "laozhang"
2) "laowang"
3) "laoli"
127.0.0.1:6379> SUNION s s1
1) "laohao"
2) "laozhang"
3) "laojiao"
4) "test"
5) "laowang"
6) "laoli"
7) "laok"
⼆,zset的常⽤指令
1)zadd和zrange
zadd是添加元素
zrange是查看元素
127.0.0.1:6379> zadd s 1 laoli
(integer) 1
127.0.0.1:6379> zadd s 2 laowang
(integer) 1
127.0.0.1:6379> zadd s 3 laozhang
redis支持的五种数据类型
(integer) 1
127.0.0.1:6379> zadd s 4 laoxu
(integer) 1
127.0.0.1:6379> zrange s 0 -1
1) "laoli"
2) "laowang"
3) "laozhang"
4) "laoxu"
2)zset的排序指令
⾸先,我们赋值,⽐如下⾯的语⽂分数
127.0.0.1:6379> zadd score_ch 98 laozhang
(integer) 1
127.0.0.1:6379> zadd score_ch 95 laoli
(integer) 1
127.0.0.1:6379> zadd score_ch 93 laowang
(integer) 1
127.0.0.1:6379> zadd score_ch 89 laohao
(integer) 1
①ZRANGEBYSCORE 从低到⾼排序,从数值中可以看到laohao的值分数最低
127.0.0.1:6379> ZRANGEBYSCORE score_ch -inf +inf
1) "laohao"
2) "laowang"
3) "laoli"
4) "laozhang"
127.0.0.1:6379> ZRANGEBYSCORE score_ch -inf +inf withscores
1) "laohao"
2) "89"
3) "laowang"
4) "93"
5) "laoli"
6) "95"
7) "laozhang"
8) "98"
可以查看区间分数,如果不存在的话,就返回empty,如果存在就返回存在区间的值;有集合的地⽅,总会有区间;127.0.0.1:6379> ZRANGEBYSCORE score_ch 1 2
(empty list or set)
127.0.0.1:6379> ZRANGEBYSCORE score_ch 80 94
1) "laohao"
2) "laowang"
127.0.0.1:6379> ZRANGEBYSCORE score_ch 80 94 withscores
1) "laohao"
2) "89"
②zrem 移除集合中的指定元素
127.0.0.1:6379> zrange score_ch 0 -1
1) "laohao"
2) "laowang"
3) "laoli"
4) "laozhang"
127.0.0.1:6379> zrem score_ch laozhang laohao
(integer) 2
127.0.0.1:6379> zrange score_ch 0 -1
1) "laowang"
2) "laoli"
③zrevrange 从⾼到底排序
127.0.0.1:6379> zrevrange score_ch 0 -1 withscores
1) "laoli"
2) "95"
3) "laowang"
4) "93"
④zcount 判断集合区间中有多少值
127.0.0.1:6379> zrange score_ch 0 -1 withscores
1) "sun"
2) "56"
3) "ke"
4) "57"
5) "liu"
6) "65"
7) "laowang"
8) "93"
9) "laoli"
10) "95"
127.0.0.1:6379> zcount score_ch 50 60
(integer) 2
指令可以看出50到60之间的有两个值;
三,应⽤场景
1,zset是可以排序的,我们可以做阅读量排⾏,加权判断,热门等等;
2,set和zset是⼀个集合,可以在集合中给⽤户打不同的标签,多个⽤户中相同点,做好友推荐;共同好友等等;
⽐如,微博中的共同关注等等
127.0.0.1:6379> SMEMBERS s1) "laohao"2) "laozhang"3) "test"4) "laowang"5) "laoli"6) "laok"127.0.0.1:6379> SMEMBERS s11) "laojiao"2) "laowang"3) "laozhang"4) "laoli"127.0.0.1:6379> smove s s1 test(integer) 1127.0.0.1:6379> SMEMBERS s11) "laojiao"2) "laowang"3) "laozhang"4) "laoli"5) "test"
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论