In actual business, we will cache some hot data into redis. If the amount of data is relatively large at this time, we will have to page the hot data. There are 2 ways: (Recommended learning: Redis video tutorial)
First: After taking out all the data from redis, do memory paging (not recommended), hot data You can do this when you are young, and the performance difference is not very big, but when the amount of data is large, a large amount of memory will be occupied during paging, or it will burst;
Second: Data structure based on redis To do cache paging, there are two types here
①: Based on the list data structure of redis, directly through the list data structure, the range method can be used for paging. When the amount of data is large, the performance is also It’s impressive, but when there are high concurrent accesses to the interface, this list may be extended indefinitely, and there will be a lot of duplication of data, which will affect normal business (not very recommended);
②: Based on the ZSet data structure of redis, we can also do paging through Zset, an ordered collection. We also use the range method, but the more troublesome thing here is that when initializing the data, Zset must store TypedTuple type data. This type is a value and For the key-value pair of score, you can check Baidu for details. The generation of this score is troublesome. When I tested here, I used the position of the current data in this list. Then Zset is sorted according to the score value. The default is from small to large; The advantage of using this is that even under high concurrency conditions, there will be no duplicate data in Zset, which will affect normal business; and the paging efficiency is similar to the list structure;
③: Use hash and Zset to implement it together ; I asked a friend about this and learned that Zset stores ordered id fields, gets the id through paging, and then uses the id to get it from the hash. I feel that the efficiency should be about the same, but there are more layers in the middle. From the hash structure, another hash needs to be maintained; (I don’t know why this is done);
Post a picture of the results of my test list and ZSet
For more Redis-related technical articles, please visit the Redis Getting Started Tutorial column to learn!
The above is the detailed content of How to do paging in redis. For more information, please follow other related articles on the PHP Chinese website!