feat(music): 实现热门音乐功能并优化性能
- 新增 HotMusicController 提供热门音乐接口 - 实现基于新热度算法的热门音乐列表获取 - 添加手动刷新缓存接口 - 新增 MusicService 接口和 MusicServiceImpl 实现类 - 使用 Redis 缓存热门音乐列表,提高响应速度 - 引入 HotScoreUtil 工具类计算热度分数 - 在 Music 实体中添加点赞数、收藏数、评论数和热度分数字段 - 更新数据库结构,增加相关字段 - 使用异步任务定期更新热门音乐缓存
This commit is contained in:
@@ -0,0 +1,54 @@
|
||||
package com.test.musichouduan.controller;
|
||||
|
||||
import com.test.musichouduan.dto.MusicDTO;
|
||||
import com.test.musichouduan.dto.Result;
|
||||
import com.test.musichouduan.service.MusicService;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 热门音乐控制器
|
||||
*/
|
||||
@RestController
|
||||
@RequestMapping("/api/hot-music")
|
||||
public class HotMusicController {
|
||||
|
||||
@Autowired
|
||||
private MusicService musicService;
|
||||
|
||||
/**
|
||||
* 获取基于新热度算法的热门音乐列表
|
||||
*
|
||||
* @param limit 限制数量
|
||||
* @return 响应结果
|
||||
*/
|
||||
/**
|
||||
* 获取基于新热度算法的热门音乐列表
|
||||
*
|
||||
* @param limit 限制数量
|
||||
* @return 响应结果
|
||||
*/
|
||||
@GetMapping("/list")
|
||||
public Result<List<MusicDTO>> getHotMusic(@RequestParam(defaultValue = "10") Integer limit) {
|
||||
// 调用 service 层的新方法,该方法会优先从 Redis 缓存获取
|
||||
List<MusicDTO> musicList = musicService.getHotMusic(limit);
|
||||
return Result.success(musicList);
|
||||
}
|
||||
|
||||
/**
|
||||
* 手动触发一次热门音乐缓存的更新。
|
||||
* 主要用于测试或紧急情况。
|
||||
* @return 响应结果
|
||||
*/
|
||||
@GetMapping("/refresh-cache")
|
||||
public Result<String> refreshHotMusicCache() {
|
||||
// 异步执行,立即返回,不阻塞当前请求
|
||||
musicService.updateHotMusicCache();
|
||||
return Result.success("热门音乐缓存更新任务已触发,请稍后查看结果。");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user