Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use redis for paging and sorting

2025-04-06 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)06/01 Report--

How to use redis for paging and sorting? To solve this problem, the editor summarizes this article about redis today, hoping to help more students who want to learn redis to find a more simple and easy way.

User comments under each topic are assembled and written into Redis, and each topic has a topicId, and each comment is associated with topicId. The general data model is as follows:

{topicId: 'xxxxxxxx', comments: [{username:' niuniu', createDate: 1447747334791, content: 'paging in Redis', commentId: 'xxxxxxx', reply: [{content:' yyyyyy' username: 'niuniu'},...]},...]}

After the comment data is queried from MySQL and assembled and stored in Redis, the assembled review data can be obtained from Redis every time. From the above data model, we can see that the data are all key- value data, which is undoubtedly stored in hash. But every time you get the review data, you need to page and sort by the createDate field. Hash certainly cannot do paging and sorting.

So, take a look at the data types supported by Redis:

1. String: mainly used to store strings, obviously does not support paging and sorting.

2. Hash: it is mainly used to store key- value data, and the comment model is full of key- value data, so Hash will undoubtedly be used here.

3, List: mainly used to store a list, each element in the list is saved according to the order in which the elements are inserted. If we sort the comment model according to createDate and then insert it into List, we seem to be able to sort it, and then use the LRANGE key start stop instruction in List to achieve paging.

Well, at this point, List seems to meet our paging and sorting requirements, but comments will still be deleted, so you need to update the data in Redis. If you rewrite all the data in Redis after each comment deletion, it is obviously not elegant and the efficiency will be greatly reduced. It would be better if you can delete the specified data, and only two instructions LPOP and RPOP are involved in deleting data in List. However, LPOP and RPOP can only delete the data in the header and footer of the list, but cannot delete the data in the specified location. (note: in fact, there is a LREM command that can be deleted, but it is very inconvenient). Moreover, when there is high concurrency access to the interface, the list may be extended indefinitely, and the data in it will be duplicated, which will affect normal business, so List is not suitable.

4. Set: mainly stores unordered sets, disordered! Rule out.

5. SortedSet: mainly stores ordered collections, and SortedSet's instruction to add elements ZADD key score member [[score,member] …] Each added element member is bound with a value for sorting score,SortedSet sorts the elements according to the size of the score value, and here you can use createDate as a score for sorting.

The instruction ZREVRANGE key start stop in SortedSet can return the members in the specified range and can be used for paging. The instruction ZREM key member of SortedSet can remove the specified members according to key, which can meet the requirement of deleting comments. Therefore, SortedSet is the most suitable here (time complexity O (log (N).

Therefore, the data types that need to be used are SortSet and Hash,SortSet for paging sorting, and Hash for storing specific key-value pairs. In the SortSet structure, the topicId of each topic is regarded as the key of the set, and the createDate and commentId of the comments associated with the topic are arranged as the score and member,commentId of the set respectively according to the size of the createDate.

When you need to query comments on a page of a topic, the topicId of the topic can find out the commintId of all comments in time order on a page under a topic by instructing zrevrange topicId (page-1) × 10 (page-1) × 10+perPage. Page is the page number of the query page, and perPage is the number of entries displayed on each page.

When you find the commentId of all the comments, you can use these commentId as key to go to the Hash structure to query the corresponding content of the comment.

In this way, the two structures of SortSet and Hash are used to achieve the purpose of paging and sorting in Redis.

Of course, you can just use the SrotedSet type instead of the Hash type, and store the comments directly in the member.

But why put comments and sorting into different types? The advantage is that you can set different sorting types for comments, such as positive and negative order of time, positive and negative order of likes, positive and negative order of viewing times, and so on. In this way, you only need to maintain different SrotedSet ordering, not the content of multiple sets of comments.

After reading the above, have you mastered the method of paging and sorting using redis? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 224

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report