Use Redis Sorted Set to implement the ranking function

Redis sorted set
1. Add (multiple) elements and element scores to the set

ZADD key score1 member1 [ score2 member2]

1. ZADD testkey 1 redis
2. ZADD testkey 2 mysql 3 java

Second, traverse the collection

ZRANGE key 0 -1 WITHSCORES
ZRANGE testkey 0 -1 WITHSCORES

Three, get the number of members in an ordered set

zcard key

zcard  testkey 

1
Four. Add and subtract elements in the set. The
Redis Zincrby command adds incremental increment to the scores of the specified members in the ordered set

  1. You can pass a negative value increment to subtract the corresponding value from the score , for example ZINCRBY key -5 member,
    subtract 5 from the score value of the member.

  2. When the key does not exist, or the score is not a member of the key, the ZINCRBY key increment member is
    equivalent ZADD key increment member.

  3. When the key is not an ordered set type, an error is returned.

  4. The fractional value can be an integer value or a double-precision floating point number.

ZADD myzset 1 "one"
ZADD myzset 2 "two"
ZINCRBY myzset 2 "one"
ZRANGE myzset 0 -1 WITHSCORES

It is found that one becomes 35.
Insert picture description here
Return the index of the specified member in the ordered set

ZRANK key  member

Insert picture description here
The members of the ordered set are arranged in the order of increasing points (from small to large).

That is to say, the larger the value, the larger the index returned

red:0> ZADD testkey 1 redis
"1"

red:0>ZADD testkey 2 mysql 3 java
"2"

red:0>ZADD testkey 2 mongo 3 tomcat
"2"

red:0>ZADD testkey 2 mongoDB 4 app

Sixth, according to the index ascending and descending sorting
Increasing:

red:0> ZRANGE testkey 0 -1 WITHSCORES    

Insert picture description here
//Decrement

red:0>ZREVRANGE  testkey 0 -1 WITHSCORES    

Insert picture description here
ZREVRANGEBYSCORE key +inf -inf # Sort all members in reverse order

red:0>ZREVRANGEBYSCORE testkey +inf -inf

Insert picture description here
ZREVRANGEBYSCORE testkey 2 1# Sort members with values ​​between 1 and 2 in reverse order.
Seven, return the score of a key in the set
Insert picture description here

 ZSCORE key member

Insert picture description here
After reading the sorted SET above, let’s try to implement the like function

7.1 Implementation of the number of likes by a single user
Whenever a user likes it, record the user ID

ZINCRBY wenZhangID  1 userID

Dislike

ZINCRBY wenZhangID -1 userID

Of course, there is a logic here, a user can only like and cancel.
If you want to count how many times a user’s article has been liked, you can directly take the number of members in it.

zcard wenZhangID

7.2 The design and implementation of the likes ranking
is actually to create a new set A, which stores the user ID and the number of likes.
Whenever the number of likes of a single user changes, the number of personal likes in A is updated synchronously
and the top is calculated in real time. That's it, such as top100

ZREVRANGE  key 0 99 WITHSCORES   

Guess you like

Origin blog.csdn.net/qq_26249609/article/details/103566848