Redis has the sort set data structure, but only the score sort can be installed,
I want to implement not only sort by score, but also sort by other fields,
For example, according to the Heat, publish time sorting, how should be implemented?
is similar to implementing MySQL in the SELECT * from topic ORDER BY hot desc, createtime desc This sort of
Environment: MySQL (database) + Redis (cache)
Scenario: List of forum posts
带分页需要按照时间/热度/等排序需要在列表就显示标题跟内容
Concurrency: Spikes may be up to tens of thousands of, but the duration may be one day, and read and write may be more frequent
Reply content:
Redis has the sort set data structure, but only the score sort can be installed,
I want to implement not only sort by score, but also sort by other fields,
For example, according to the Heat, publish time sorting, how should be implemented?
is similar to implementing MySQL in the SELECT * from topic ORDER BY hot desc, createtime desc This sort of
Environment: MySQL (database) + Redis (cache)
Scenario: List of forum posts
带分页需要按照时间/热度/等排序需要在列表就显示标题跟内容
Concurrency: Spikes may be up to tens of thousands of, but the duration may be one day, and read and write may be more frequent
The Redis mechanism is not keen on a more complex sort,
However, you can do the sort work first, then store in the Redis list, crop the list to a specified length, such as 1000, Redis only need to save the latest 1000, each need to get the latest posts or comments on the scope of the project, and then check the library get put into the cache
Can multiple ordered sets, each ordered set implementation of a sort
Run a script every morning to save sorted data into Redis
He's good at everything, and he's going to use it with each other.
Can put the heat $ A, release time $b do a specific proportion as score, heat accounted for 30%
zadd $a*0.3 $user_id zadd $b*0.7 $user_id
And thenzrangebyscore
Each sort is sorted in order, and the sort results are cached, the sort of complex operations are given to the DB, or the application, and Redis is responsible for caching only, not the business logic. After all, Redis is just a simple cache that cannot perform operations that are as responsible as db.
Redis uses a sorted set to solve the problem of sorting by two fields, that is, according to the Heat + time as the sort field, the key is how to splice the score problem. This feature of the scene, the solution is to assemble a floating point number, the integer part is the value of the heat, the fractional part is the time. It is important to note that the redis inside accuracy should be a decimal 6 bits, so the entire date cannot be used as a fractional part. For example, there is a set of data:
| Heat | Time |
| 2 | 2016-03-31 13:41:01 |
| 5 | 2016-03-31 13:41:01 |
| 2 | 2016-03-31 13:42:01 |
| 1 | 2016-03-31 13:41:01 |
Then the value of score can be assembled into:
| Heat | Time | Score
| 2 | 2016-03-31 13:41:01 | 2.134101
| 5 | 2016-03-31 13:41:01 | 5.134101
| 2 | 2016-03-31 13:42:01 | 2.134201
| 1 | 2016-03-31 13:41:01 | 1.134101
This limitation is that each zset can only save one day of data