perf: speed-up musicDB load

This commit is contained in:
mokurin000
2025-08-01 03:50:36 +08:00
parent 68e8a6e005
commit 7fe64ac4cd
3 changed files with 27 additions and 5 deletions

View File

@@ -4,12 +4,13 @@ use nyquest::{AsyncClient, Body, Request, header::USER_AGENT};
mod model;
use model::{GetResponse, GetUserId};
use serde::Serialize;
pub struct QRCode<'a> {
pub qrcode_content: &'a str,
}
#[derive(Debug, snafu::Snafu)]
#[derive(Debug, snafu::Snafu, Serialize)]
pub enum QRLoginError {
#[snafu(display("QRCode expired [10mins]"))]
QRCodeExpired10,
@@ -23,13 +24,17 @@ pub enum QRLoginError {
#[snafu(context(false))]
#[snafu(display("request error: {source}"))]
NyquestError {
#[serde(skip)]
source: nyquest::Error,
#[serde(skip)]
backtrace: Backtrace,
},
#[snafu(context(false))]
JSONError {
#[serde(skip)]
source: serde_json::error::Error,
#[serde(skip)]
backtrace: Backtrace,
},
}

View File

@@ -1,4 +1,4 @@
use std::{fs::OpenOptions, sync::LazyLock};
use std::{fs::OpenOptions, io::BufReader, sync::LazyLock, time::SystemTime};
use rust_decimal::{Decimal, dec, serde::DecimalFromString};
use rustc_hash::FxHashMap;
@@ -26,6 +26,7 @@ pub struct Level {
type MusicDB = FxHashMap<u32, MusicInfo>;
pub static MUSIC_DB: LazyLock<Option<MusicDB>> = LazyLock::new(|| {
let time = SystemTime::now();
info!("loading musicDB...");
let json = OpenOptions::new()
@@ -34,11 +35,17 @@ pub static MUSIC_DB: LazyLock<Option<MusicDB>> = LazyLock::new(|| {
.open("musicDB.json")
.inspect_err(|e| warn!("failed to load musicDB: {e}"))
.ok()?;
let buf_reader = BufReader::new(json);
let db: Vec<MusicInfo> = serde_json::from_reader(json)
let db: Vec<MusicInfo> = serde_json::from_reader(buf_reader)
.inspect_err(|e| warn!("failed to load musicDB: {e}"))
.ok()?;
info!(
"loaded musicDB, cost {}ms",
time.elapsed().unwrap_or_default().as_millis()
);
Some(db.into_iter().map(|entry| (entry.id, entry)).collect())
});