Forum Building
Describes how to use Redis to build a forum key features user accounts create posts, reply to posts, vote for posts in categories, view posts that belong to a specific category categorize categories into a tag (tab), view posts that belong to a specific tag Daily hot Talk posts ranking algorithm-based posts recommend the component of a post: category title The number of people who voted the author posted time click Content TAG according to category show posts
The category page sorts posts based on the last time the post was replied to. category Category
API |
function |
Implementation Principle |
Category (client, name) |
Set the name of the client and the category. |
|
Include_topic (TOPIC_ID) |
Adds a given post to the current category. |
Zadd |
Is_included (TOPIC_ID) |
Checks whether a given post belongs to the current category. |
Using Zscore to check the ordered set, the topic_id corresponds to the existence of the score, if not, then the post does not belong to the category. |
Count_topic () |
Returns the number of posts included in the current category. |
Zcard |
Paging (n, Count) |
Press count posts for a page and return to the post on page N of the category. |
Zrevrange |
Show posts based on tags
A label can contain multiple categories, and posts that belong to the included category appear in the list of tags. implementing the Label function requires two steps: record the relationship between the label and the classification.
For example, the program remembers that the categories "programmer", "Linux", and "node. js" belong to the "Technology" tab. Record the posts under the label, and when the user clicks on a tag, the individual posts under the tag are displayed.
For example, post 132312 belongs to the "programmer" category, and the category belongs to the "Technology" tab, so the program should show the post 132312 when the user clicks on the "Technology" page. Tag class
API |
function |
Implementation Principle |
Tag (client, tag_name) |
Sets the name of the client and label. |
|
Add_member (Category_name) |
Adds the given category to the current label. |
Sadd |
Is_member (Category_name) |
Checks whether the given classification belongs to the current label. |
Sismember |
Get_all_member () |
Returns all the categories contained in the current label. |
Smembers |
Count_member () |
Returns the number of categories that the label contains. |
SCard |
Include_topic (Category_name, topic_id) |
Adds a post for a given category to the list of posts in the current tab. |
Zadd |
Paging (n, Count) |
Returns the post that the label contains. |
Zrevrange |
Count_topic () |
Returns the number of posts currently included in the label. |
Zcard |
reply to a post
Users can reply to a post. Each reply will contain at least the information about the author, the response time, and the content of the reply. And each post needs a list to store all replies. This is basically the same as the requirements of the Comment class and the Commentlist class that we created earlier to implement Weibo reviews, so just make some simple changes to these two classes and reuse them. daily Hot discussion of post ranking
Daily Hot Topics The leaderboard shows the most frequently answered posts per day, which is updated daily.
To achieve this leaderboard, the program needs to use an ordered set of keys named Bbs::reply_day_rank, where the elements of an ordered set are the ID of the post, and the element's score is the number of posts that are currently being replied to.
Whenever you add a reply to a post with an ID of N, the program executes the following command to increase the number of responses to the post in the leaderboard:
Zincrby Bbs::reply_day_rank N 1
And the program will set a time-to-live for bbs:reply_day_rank, let it expire automatically after one day, and automatically create a new list.
This can be done by modifying and reusing the Dayrank classes previously described. Replydayrank class
API |
function |
Implementation Principle |
Replydayrank (client) |
Set up the client. |
|
Incr_reply_count (TOPIC_ID) |
Add one to the reply count value for a given post. |
Zincrby |
Get_top (N) |
Returns the top N posts in the leaderboard. |
Zrevrange |
Post recommendation System
When the algorithm scores the post, the factors to consider are: the user's contribution value, the release time, the number of users ' votes, the number of replies to the user, and so on.
Ways to implement the recommended system: Select a recommendation algorithm to calculate the rating of the post. Calculate ratings for individual posts and present them in an orderly fashion.
Topicrecommand class
API |
role |
Implementation principle |
Topicrecommand (client) |
sets the clients. |
|
update_rank (topic_id, Upvote,downvote, post_time) |
update the recommended ratings for posts. |
zadd |
Paging (n, count) |
returns the post on page n recommended by the algorithm. |
zrevrange |