song cache
This commit is contained in:
@@ -5,6 +5,7 @@ import { NextRequest, NextResponse } from 'next/server';
|
||||
import { getAuthInfoFromCookie } from '@/lib/auth';
|
||||
import { db } from '@/lib/db';
|
||||
import { MusicPlayRecord } from '@/lib/db.client';
|
||||
import { getCachedSongs, setCachedSong } from '@/lib/music-song-cache';
|
||||
|
||||
export const runtime = 'nodejs';
|
||||
|
||||
@@ -29,7 +30,28 @@ export async function GET(request: NextRequest) {
|
||||
}
|
||||
|
||||
const records = await db.getAllMusicPlayRecords(authInfo.username);
|
||||
return NextResponse.json(records, { status: 200 });
|
||||
|
||||
// 从缓存中获取歌曲信息并填充到记录中
|
||||
const keys = Object.keys(records).map(key => {
|
||||
const [platform, id] = key.split('+');
|
||||
return { platform, id };
|
||||
});
|
||||
const cachedSongs = getCachedSongs(keys);
|
||||
|
||||
// 将缓存的歌曲信息合并到记录中
|
||||
const enrichedRecords: Record<string, MusicPlayRecord> = {};
|
||||
for (const [key, record] of Object.entries(records)) {
|
||||
const cachedSong = cachedSongs.get(key);
|
||||
enrichedRecords[key] = {
|
||||
...record,
|
||||
name: cachedSong?.name || record.name,
|
||||
artist: cachedSong?.artist || record.artist,
|
||||
album: cachedSong?.album || record.album,
|
||||
pic: cachedSong?.pic || record.pic,
|
||||
};
|
||||
}
|
||||
|
||||
return NextResponse.json(enrichedRecords, { status: 200 });
|
||||
} catch (err) {
|
||||
console.error('获取音乐播放记录失败', err);
|
||||
return NextResponse.json(
|
||||
@@ -86,6 +108,16 @@ export async function POST(request: NextRequest) {
|
||||
}
|
||||
|
||||
await db.saveMusicPlayRecord(authInfo.username, platform, id, record);
|
||||
|
||||
// 缓存歌曲信息到服务器内存
|
||||
setCachedSong(platform, id, {
|
||||
id: record.id,
|
||||
name: record.name,
|
||||
artist: record.artist,
|
||||
album: record.album,
|
||||
pic: record.pic,
|
||||
});
|
||||
|
||||
return NextResponse.json({ success: true }, { status: 200 });
|
||||
} catch (err) {
|
||||
console.error('保存音乐播放记录失败', err);
|
||||
|
||||
@@ -3,6 +3,11 @@
|
||||
|
||||
import { useRouter } from 'next/navigation';
|
||||
import { useEffect, useRef, useState } from 'react';
|
||||
import {
|
||||
getAllMusicPlayRecords,
|
||||
saveMusicPlayRecord,
|
||||
MusicPlayRecord,
|
||||
} from '@/lib/db.client';
|
||||
|
||||
interface Song {
|
||||
id: string;
|
||||
@@ -145,9 +150,49 @@ export default function MusicPage() {
|
||||
}
|
||||
};
|
||||
|
||||
// 页面加载时恢复播放状态
|
||||
// 页面加载时恢复播放状态和数据库记录
|
||||
useEffect(() => {
|
||||
restorePlayState();
|
||||
const initializePlayState = async () => {
|
||||
// 先恢复 localStorage 中的播放状态
|
||||
restorePlayState();
|
||||
|
||||
// 从数据库加载播放记录
|
||||
try {
|
||||
const dbRecords = await getAllMusicPlayRecords();
|
||||
|
||||
// 将数据库记录转换为前端格式
|
||||
const records: PlayRecord[] = [];
|
||||
const songs: Song[] = [];
|
||||
|
||||
Object.entries(dbRecords).forEach(([key, record]) => {
|
||||
records.push({
|
||||
platform: record.platform,
|
||||
id: record.id,
|
||||
playTime: record.play_time,
|
||||
duration: record.duration,
|
||||
timestamp: record.save_time,
|
||||
});
|
||||
|
||||
songs.push({
|
||||
id: record.id,
|
||||
name: record.name,
|
||||
artist: record.artist,
|
||||
album: record.album,
|
||||
pic: record.pic,
|
||||
});
|
||||
});
|
||||
|
||||
// 更新状态
|
||||
if (records.length > 0) {
|
||||
setPlayRecords(records);
|
||||
setPlaylist(songs);
|
||||
}
|
||||
} catch (error) {
|
||||
console.error('加载播放记录失败:', error);
|
||||
}
|
||||
};
|
||||
|
||||
initializePlayState();
|
||||
}, []);
|
||||
|
||||
// 监听播放状态变化,自动保存
|
||||
@@ -578,6 +623,24 @@ export default function MusicPage() {
|
||||
...updated[playlistIndex],
|
||||
playTime: audio.currentTime,
|
||||
};
|
||||
|
||||
// 保存到数据库
|
||||
const record = updated[playlistIndex];
|
||||
const dbRecord: MusicPlayRecord = {
|
||||
platform: record.platform,
|
||||
id: record.id,
|
||||
name: currentSong.name,
|
||||
artist: currentSong.artist,
|
||||
album: currentSong.album,
|
||||
pic: currentSong.pic,
|
||||
play_time: audio.currentTime,
|
||||
duration: audio.duration || 0,
|
||||
save_time: Date.now(),
|
||||
};
|
||||
|
||||
saveMusicPlayRecord(record.platform, record.id, dbRecord).catch(err => {
|
||||
console.error('保存播放记录到数据库失败:', err);
|
||||
});
|
||||
}
|
||||
return updated;
|
||||
});
|
||||
@@ -607,6 +670,24 @@ export default function MusicPage() {
|
||||
...updated[playlistIndex],
|
||||
duration: audio.duration,
|
||||
};
|
||||
|
||||
// 保存到数据库(包含时长信息)
|
||||
const record = updated[playlistIndex];
|
||||
const dbRecord: MusicPlayRecord = {
|
||||
platform: record.platform,
|
||||
id: record.id,
|
||||
name: currentSong.name,
|
||||
artist: currentSong.artist,
|
||||
album: currentSong.album,
|
||||
pic: currentSong.pic,
|
||||
play_time: record.playTime,
|
||||
duration: audio.duration,
|
||||
save_time: Date.now(),
|
||||
};
|
||||
|
||||
saveMusicPlayRecord(record.platform, record.id, dbRecord).catch(err => {
|
||||
console.error('保存播放记录到数据库失败:', err);
|
||||
});
|
||||
}
|
||||
return updated;
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user