Articles voting and sorting in groups

Time:2020-10-11

Articles voting and sorting in groups
The basic command is skipped. Please refer to the command details redis.io Official website or redis Chinese official website

Redis profile:

Redis is an open source (BSD licensed) in memory data structure storage system, which can be used as database, cache and message middleware. It supports many types of data structures, such as strings, hashes, lists, sets, sorted sets and range queries, bitmaps, hyperloglogs and geospatial index radius queries. Redis has built-in replication, Lua scripting, LRU occurrence, transactions and different levels of disk persistence. It also provides high availability through redis sentinel and cluster.

Articles voting and sorting in groups
Articles voting and sorting in groups

Voting system – vote only

Objective: To study the clinical effect of the methodDesign a scoring system that decreases with time. At the same time, achieve the hottest / newest by group.
Score calculation method:Final score = the number of votes supported by the article × a constant + article publishing time (UNIX time)
Article storage mode:Articles voting and sorting in groups

The key is article:92617 , expressed as the article with ID 92617 in the article table (` hmset ')

realization:Two zsets: one key is the article ID, and the score is $\ underline {release time} $; (latest) the other key is the article ID, and the score is stored in $/ underline {score} $. (hottest)
Articles voting and sorting in groups
Prevent users from voting multiple times:Save the voted user ID with a set.
Articles voting and sorting in groups

Function realization

Voting:Add one to the value of votes in hashHINCRBY; the score of the corresponding article in the Zset of the record score increasesZINCRBY; the corresponding set table that records the voting personnel is added to the user. (follow a transaction)
release:Generate the article ID and store the article informationhmsetGenerate vote set, score and time ordered set.
Articles voting and sorting in groups
obtain: zrevrange/zrangeGet the hottest / latest articles_ IDS collection, by traversing the IDhgetallGet all attribute key value pairs of each article.
Articles voting and sorting in groups
grouping:Create a new set named group to save all articles_ id。
Articles voting and sorting in groups
To enable access to the hottest / latest articles by group. zinterstorecommand(compare the consumption time, which can be set to 60s before expiration)
Articles voting and sorting in groups
get_group_articles()
Articles voting and sorting in groups
Of course, add group to the hash of article_ ID to limit the number of articles grouped. (you should also be right when voting here score:programming Add points)

Recommended Today

Mybatis query error identifying class… Types() or values ()

We encountered this problem when we learned the relationship of mybatis. Error instantiating class com.springmvc_mybatis.pojo.Items with invalid types () or values (). Instantiate a class with an invalid type () or value() com.springmvc_ mybatis.pojo.items There was an error while reading terms of settlement: Instantiation failed, which indicates that there is something wrong with the constructor […]