The good and the bad and the ugly in rediscounting

If you need to use paging in your Redis application, there are several strategies you can use to achieve the necessary functionality. Although pagination can be challenging, a quick look at these techniques should help make your choice of method and implementation easier. There are several paging strategies in Redis. Find out what they are and their advantages and disadvantages!

In Redis, you have several options to choose from. You can use the SSCAN command, or you can use a sort set. These have their own advantages, so choose the one that best suits your application and its infrastructure.

The SSCAN command is part of a group of commands similar to the regular scan command. These include:

  • Scan-used to iterate a set of keys in the current database.
  • Used to iterate over the elements of the collection.
  • Used to iterate field hashes and related values.
  • Used to iterate the elements of the sorted set and their scores.

Example Scan iteration. Source: Redis.

Therefore, when the regular scan command traverses the database keys, the SSCAN command can traverse the elements of the collection. By using the returned SSCAN cursor, you can page a Redis set.

The disadvantage is that you need some way to maintain the value of the cursor, if there are concurrent users, this may cause some strange behavior, because the cursor may not be in the expected position. However, this is useful for applications where traffic in these paging areas may be lighter.

In Redis, a sorted set is a non-repetitive set of strings associated with a musical score. This score is used to sort the collection from smallest to largest. This data type allows for fast updates, allowing you to easily access the element, even if the element is in the middle of the collection.

An example of a classification set element.

To paging, you can use the ZRANGE command to select a series of elements in the sorted set based on the score. For example, you can choose 1-20, 21-40, etc. fractions. By adjusting the range programmatically as the user moves the data, you can achieve the paging required by the application.

Since sorted sets and ZRANGE accomplish this task more intuitively than using scans, it is usually the preferred method of paging and is easier to implement among multiple users, because you can programmatically track what each user selects at any given time ZRANGE.

Finally, you can choose which method suits your particular situation. If you have a smaller application with less heterogeneous synchronization of streaming databases , scanning may be right for you. However, if you need a more powerful solution to handle larger data sets or higher utilization applications, then it is best to use ZRANGE and sorted sets to implement paging in the application.

Guess you like

Origin blog.csdn.net/weixin_49470452/article/details/107506307