How to Build an Unbeatable Weekly Hot‑Comment Ranking with Redis
The article shows how to avoid database pressure by recording daily comment counts in Redis sorted sets, merging the seven daily sets into a weekly ranking with a union command, and exposing the result through a Spring Boot service and a custom Freemarker tag, complete with code examples and screenshots.
When a site needs a weekly hot‑comment list, querying the database directly can overload it, so the author proposes using Redis as a cache. Each day’s comment totals are stored in a sorted set whose key follows the pattern day:<dayNumber> (e.g., day:1), using ZADD to add the comment count for each post.
After seven days of data have been recorded, a union operation ( UNION week:rank 7 day:1 ... day:8) creates a new sorted set week:rank that contains the combined scores, allowing the weekly ranking to be obtained with a single ZREVRANGE week:rank 0 -1 WITHSCORES call.
If a daily key may receive incremental updates (e.g., when a comment is added or removed), the article recommends ZINCRBY day:1 1 post:1 to increase the score and ZINCRBY day:1 -1 post:1 to decrease it, avoiding the need to rewrite the whole set.
In the Spring Boot implementation, a @Component class implements ApplicationRunner and runs at startup. It queries the last week’s posts (selecting only id, title, user_id, comment_count, view_count, created), then for each post:
Builds a daily key like day:rank:20210724 and stores the comment count with redisUtil.zSet(key, post.getId(), post.getCommentCount()).
Calculates the remaining TTL so the key expires after seven days and calls redisUtil.expire(key, expireTime).
Caches basic post information (id, title, comment count) in a hash rank:post:<postId> if it does not already exist.
After all daily sets are populated, the method zunionAndStore() builds the list of the past seven daily keys and calls redisUtil.zUnionAndStore(destkey, otherKeys, newkey) to produce the weekly ranking set week:rank.
The RedisUtil utility class provides generic Redis operations such as expire, hasKey, zSet, zUnionAndStore, and a full suite of hash, set, list, and sorted‑set helpers, all shown with their original Java code.
For the front‑end, a custom Freemarker directive HotsTemplate (registered as the tag hots) retrieves the top entries from week:rank using redisUtil.getZSetRank(key, 0, 6), builds a list of maps containing the post id, title (fetched from the hash), and comment count, and renders them in the page.
The configuration class FreemarkerConfig injects the custom tag into the Freemarker environment, making it usable in templates. Screenshots illustrate the Redis keys, the union result, and the final front‑end display.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Programmer XiaoFu
xiaofucode.com – a programmer learning guide driven by the pursuit of profit
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
