JAVA:Spring Boot 集成 Redis 实现每周热评

admin
6
2025-09-30

1、简述

在现代应用中,尤其是社交媒体和内容平台,展示热门评论是常见的功能。我们可以通过 Redis 的高性能和丰富的数据结构,轻松实现每周热评功能。本文将详细介绍如何利用 Redis 实现每周热评,并列出完整的实现代码。

image-1g56.png


2、需求分析

热评 是指在某个时间范围内(如一周内)获得最多点赞的评论。为了实现这个功能,我们可以使用 Redis 的 Sorted Set 数据结构,它可以根据评论的热度(如点赞数)对评论进行排序。

2.1 实现思路

  • 数据结构:Redis 的 Sorted Set 是非常合适的工具,它将 评论 ID 作为成员,点赞数 作为分数,按照点赞数自动排序。
  • 周期性更新:为了实现每周的热评功能,我们可以每天将新增的评论加入 Redis 中,并对 Redis 数据进行过期管理。
  • 用户操作:当用户点赞评论时,Redis 会自动更新对应评论的点赞数,系统可以根据点赞数实时排序。

2.2 Redis 数据结构设计

  • Key:存储每周热评的 Redis key 可以设计为 weekly:hot:comments:{week}, 其中 {week} 是该周的标识(如 2024-09-14)。
  • Value:使用 Redis 的 Sorted Set,将 评论 ID 作为成员,点赞数 作为分数。

3、代码实现

3.1 项目依赖

首先,确保项目中引入了 Redis 依赖(如果你使用的是 Spring Boot 和 Redis):

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>

3.2 热评功能的 Redis 服务类

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Set;
import java.util.concurrent.TimeUnit;

@Service
public class HotCommentService {

    @Autowired
    private StringRedisTemplate redisTemplate;

    // 获取当前周标识,通常用年份 + 周数表示
    private String getCurrentWeekKey() {
        return "weekly:hot:comments:" + System.currentTimeMillis() / (1000 * 60 * 60 * 24 * 7);
    }

    // 添加评论点赞数
    public void addCommentLike(String commentId) {
        String weekKey = getCurrentWeekKey();
        // Increment the like count (sorted set score) by 1
        redisTemplate.opsForZSet().incrementScore(weekKey, commentId, 1);
        // Set an expiration for the current week data (e.g., 7 days)
        redisTemplate.expire(weekKey, 7, TimeUnit.DAYS);
    }

    // 获取本周的热门评论
    public Set<String> getWeeklyHotComments(int topN) {
        String weekKey = getCurrentWeekKey();
        // 获取当前周点赞数最多的评论,按分数降序排列
        return redisTemplate.opsForZSet().reverseRange(weekKey, 0, topN - 1);
    }
}

3.3 Controller 层调用

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Set;

@RestController
public class HotCommentController {

    @Autowired
    private HotCommentService hotCommentService;

    // 用户点赞评论接口
    @PostMapping("/like/{commentId}")
    public String likeComment(@PathVariable String commentId) {
        hotCommentService.addCommentLike(commentId);
        return "Comment liked!";
    }

    // 获取本周热门评论接口
    @GetMapping("/hot-comments/{topN}")
    public Set<String> getHotComments(@PathVariable int topN) {
        return hotCommentService.getWeeklyHotComments(topN);
    }
}

3.4 Redis 的配置

在 application.properties 中配置 Redis:

spring.redis.host=localhost
spring.redis.port=6379

3.5 代码解析

  • 点赞处理:addCommentLike(String commentId) 方法会增加评论的点赞数,每当用户点赞时,调用 Redis 的 incrementScore 方法,使该评论的得分(点赞数)增加。同时设置 Redis key 的过期时间为 7 天,以确保每周热评只保留当前周的数据。
  • 获取热评:getWeeklyHotComments(int topN) 方法通过 Redis 的 reverseRange 获取按分数降序排列的评论 ID,返回当前周内点赞数最高的评论。
  • 定期清理:通过 expire 设置 Redis key 的过期时间,避免旧的热评数据占用内存。

4、应用场景

  • 社交平台:用于展示每周热门评论、热门帖子,增强用户互动性。
  • 内容推荐:对文章、视频等内容的点赞数进行排序,作为推荐系统的基础数据。
  • 电商评论:可以用于电商平台展示商品的热门评论,提高用户购买决策的效率。

5、结论

通过 Redis 的 Sorted Set 数据结构,我们能够轻松实现每周热评功能。Redis 的高性能和强大的数据处理能力,使得它在处理这种实时性强、数据量大的场景中非常适用。借助 Java 和 Spring Boot 的集成,我们可以将 Redis 热评功能快速引入到应用中,提升用户体验。

Redis 的实时数据处理功能,不仅适用于热门评论的实现,还可以在其他涉及实时排序、统计的场景中广泛应用。

动物装饰