redis-zset simple operation

Recently, I created a best-selling product list. The goal is to combine the sales volume and sales volume of the product to comprehensively calculate a score, and then rank the best-selling products.

初始化热销榜  
对set集合k1进行赋值:
zadd k1 1 a 2 b 3 c 4 d
(integer) 4

1 The first is the list of best-selling products (descending order of best-selling products)

 zrevrange k1 0 -1
1) "c"
2) "b"
3) "a"
4) "d"

2 If the product is out of stock, it will not participate in the best-seller ranking

移除集合k1里的元素d
zrem k1 d
(integer) 1

3 Get the best-selling list details

 zrange k1 0 -1
1) "a"
2) "b"
3) "c"
如果想获取商品综合分数
zrange k1 0 -1 withscores
1) "a"
2) "1"
3) "b"
4) "2"
5) "c"
6) "3"

4. Can obtain the ranking of the product in the list.

 逆向,倒序取排名,索引位置从0开始:
zrevrank k1 a
(integer) 3

The selection data structure is zset in redis. In addition to the above four requirements, it can also perform filtering and automatic sorting.

是可以进行滤重的,通过结果可以看出:eg:
 zadd k1 1 a 2 b 3 c
(integer) 0

`redis集合会按分数默认做好了排序,通过获取值即可看出 eg:

正向查询集合k1里的元素的位置:
 zrank k1 c
(integer) 2
zrank k1 a
(integer) 0

Extension:
If you need to obtain the score during work, you can use the following command

查询集合k1的元素分数,即score:
zscore k1 a
"1"
zscore k1 c
"3"

>= 2.4: Accepts multiple members. Before Redis 2.4, the command could only add or update one member.

Guess you like

Origin blog.csdn.net/qq_17033579/article/details/123478676