feat(music): 优化音乐数据加载和展示
- 优化音乐时长获取日志,增加文件名信息 - 添加歌手头像字段并处理相关逻辑 - 修改相关音乐推荐为热门音乐推荐- 优化相关音乐封面展示逻辑 - 使用 JOIN FETCH 优化音乐数据加载性能
This commit is contained in:
@@ -49,6 +49,7 @@ public class HotMusicController {
|
||||
* 主要用于测试或紧急情况。
|
||||
* @return 响应结果
|
||||
*/
|
||||
@SaIgnore
|
||||
@GetMapping("/refresh-cache")
|
||||
public Result<String> refreshHotMusicCache() {
|
||||
// 异步执行,立即返回,不阻塞当前请求
|
||||
|
||||
@@ -71,6 +71,11 @@ public class MusicDTO {
|
||||
*/
|
||||
private String singer;
|
||||
|
||||
/**
|
||||
* 歌手头像
|
||||
*/
|
||||
private String singerAvatar;
|
||||
|
||||
/**
|
||||
* 音乐类型列表
|
||||
*/
|
||||
|
||||
@@ -113,4 +113,12 @@ public interface MusicRepository extends JpaRepository<Music, Long>, JpaSpecific
|
||||
*/
|
||||
@Query("SELECT t.name, COUNT(m) FROM Music m JOIN m.types t GROUP BY t.name ORDER BY COUNT(m) DESC")
|
||||
List<Object[]> countByMusicType();
|
||||
|
||||
/**
|
||||
* 查询所有音乐,并立即加载关联的歌手信息
|
||||
*
|
||||
* @return 音乐列表
|
||||
*/
|
||||
@Query("SELECT DISTINCT m FROM Music m LEFT JOIN FETCH m.singers")
|
||||
List<Music> findAllWithSingers();
|
||||
}
|
||||
|
||||
@@ -95,7 +95,7 @@ public class FileServiceImpl implements FileService {
|
||||
int duration = audioFile.getAudioHeader().getTrackLength();
|
||||
return duration;
|
||||
} catch (Exception e) {
|
||||
logger.error("获取音乐时长失败", e);
|
||||
logger.error("获取音乐 '{}' 时长失败", file.getOriginalFilename(), e);
|
||||
return 0;
|
||||
} finally {
|
||||
if (tempFile != null) {
|
||||
|
||||
@@ -240,18 +240,8 @@ public class MusicServiceImpl implements MusicService {
|
||||
@Transactional(readOnly = true) // 使用只读事务,提高查询性能
|
||||
@Override
|
||||
public CompletableFuture<List<MusicDTO>> updateHotMusicCache() {
|
||||
// 为了处理大数据量,我们使用分页查询来代替一次性加载所有数据
|
||||
List<Music> allMusic = new ArrayList<>();
|
||||
Page<Music> musicPage;
|
||||
int pageNum = 0;
|
||||
int pageSize = 500; // 每次查询500条
|
||||
|
||||
do {
|
||||
Pageable pageable = PageRequest.of(pageNum, pageSize);
|
||||
musicPage = musicRepository.findAll(pageable);
|
||||
allMusic.addAll(musicPage.getContent());
|
||||
pageNum++;
|
||||
} while (musicPage.hasNext());
|
||||
// 使用 JOIN FETCH 一次性加载所有音乐及其关联的歌手信息
|
||||
List<Music> allMusic = musicRepository.findAllWithSingers();
|
||||
|
||||
// 使用并行流来加速计算过程
|
||||
allMusic.parallelStream().forEach(music -> {
|
||||
@@ -659,6 +649,11 @@ public class MusicServiceImpl implements MusicService {
|
||||
List<String> singerNames = music.getSingers().stream().map(Singer::getName).collect(Collectors.toList());
|
||||
musicDTO.setSingerNames(singerNames);
|
||||
musicDTO.setSinger(String.join("、", singerNames));
|
||||
|
||||
// 设置歌手头像
|
||||
if (music.getSingers().size() == 1) {
|
||||
musicDTO.setSingerAvatar(music.getSingers().iterator().next().getAvatar());
|
||||
}
|
||||
}
|
||||
|
||||
// 如果传入了用户ID,则查询该用户的收藏和点赞状态
|
||||
|
||||
Reference in New Issue
Block a user