This is the 15th day of my participation in the First Challenge 2022

Hello, hello, I am gray little ape, a super bug writing program ape!

Today we are going to share the use of the zset type, the last of Redis’s five data types,

ZSet type

ZSet is similar to Set and does not allow repeated members, but it should be noted that the members of Set are rehearsed in disorder, while the members of ZSet are arranged in order.

The reason is that each element in a ZSet set is associated with a score of type double, which is how Redis sorts the members of the set from smallest to largest. Therefore, although the members of an ordered set are unique, the score is repeatable.

Adds one or more members to an ordered collection

With the ZADD command, we can insert one or more members into an ordered collection, but if the member already exists, the score for that member is updated, in the following format:

ZADD key score1 member1 [score2 member2…]

  • Key is the index of an ordered collection
  • Score is the score of the inserted member,
  • Member is the corresponding member. If multiple members are inserted, they are separated by Spaces

For example, if we want to insert the three member variables score is 1, member is ztest1, score is 3, member is ztest3, score is 4, member is ztest4 into myzset

127.0.0.1:6379> ZADD myzset 1 ztest1 3 ztest3 4 ztest4
(integer) 3
Copy the code

Gets the number of members of an ordered collection

The ZCARD command can obtain the number of member variables in the specified ordered set in the following format:

ZCARD key

  • Key is the index of an ordered collection

For example, we query the number of member variables in the ordered set myzset

127.0.0.1:6379> ZCARD myzset
(integer) 3
Copy the code

Computes the number of members of an ordered set with a specified interval fraction

If we want to query how many member variables are in the specified range, we can use the ZCOUNT command, in the following format:

ZCOUNT key min max

  • Key is the index of the ordered collection to be queried
  • Min is the minimum value of the interval
  • Max is the maximum value of the interval

For example, in the myzset set above, we want to query the number of members between 2 and 4 directly,

127.0.0.1:6379> ZCOUNT myzset 2 4
(integer) 2
Copy the code

Returns a member of the ordered collection within the specified interval based on the score

In the previous command we returned the number of members in the specified range based on the score. What if we wanted to return members? We can use

ZRANGEBYSCORE key min max [WITHSCORES]

  • Key represents the index of the collection to be queried
  • Min represents the minimum value of score
  • Max represents the minimum score
  • [WITHSCORES] is optional, indicating whether the corresponding score is included

If we query the ordered set myzset for member variables with scores between 1 and 3:

127.0.0.1:6379> ZRANGEBYSCORE myzset 1 1) "ztest1" 2) "ztest3" 127.0.0.1:6379> ZRANGEBYSCORE myzset 1 3 WITHSCORES 1) "ztest1" 2) "1" 3) "ztest3" 4) "3"Copy the code

Removes one or more elements from an ordered collection

If we want to remove one or more elements from an ordered collection, we can use the ZREM command, which has the following format:

ZREM key member1 [member2…]

  • Key is the index of the collection whose member is to be removed
  • Member is the member variable to be removed. There can be more than one member variable, separated by Spaces

If we want to remove the member variable ztest4 from myZSet,

127.0.0.1:6379> ZREM myzset ztest4
(integer) 1
Copy the code

Today’s summary

That a few articles have been sharing in the use of a variety of data types and common in Redis command operations, although the content is more, but the operation is easy, of course, the five types of data in the Redis commands far more than these, I just listed a few that are widely used in the command, so by today’s article, I hope you can master the basic use of the five data types in Redis, at least know how to assign or value, you can also combine the characteristics of these five data types to query or think about their use scenarios.

In the next article, I will share with you the use of three specific data types in Redis,

I’m Grey Ape, and I’ll see you next time!