update code level
This commit is contained in:
44
src/cache/mod.rs
vendored
44
src/cache/mod.rs
vendored
@@ -1,7 +1,9 @@
|
||||
//! 缓存层 — 内存缓存管理
|
||||
|
||||
use crate::search_db::SearchDb;
|
||||
use crate::db::Database;
|
||||
use crate::models::*;
|
||||
use sqlx::Row;
|
||||
use std::collections::HashMap;
|
||||
use std::sync::Arc;
|
||||
use tokio::sync::RwLock;
|
||||
@@ -205,25 +207,55 @@ impl PersonCache {
|
||||
}
|
||||
|
||||
/// 刷新所有缓存(相册 + 标签 + 人物)
|
||||
/// 相册从主库读取,标签/人物从 search.db 读取
|
||||
pub async fn refresh_all_caches(
|
||||
db: &Database,
|
||||
search_db: &SearchDb,
|
||||
albums_cache: &Arc<RwLock<AlbumsCache>>,
|
||||
persons_cache: &Arc<RwLock<PersonCache>>,
|
||||
tags_cache: &Arc<RwLock<TagCache>>,
|
||||
) {
|
||||
// 1. 刷新相册
|
||||
if let Ok(albums) = search_db.get_all_albums().await {
|
||||
let mut c = albums_cache.write().await;
|
||||
c.update(albums.clone());
|
||||
// 1. 从主库读取相册(含 tags 和 persons 关联)
|
||||
if let Ok(albums) = db.get_all_albums().await {
|
||||
// 批量获取 album → tags 映射
|
||||
let tag_rows = sqlx::query("SELECT album_id, tag FROM album_tags")
|
||||
.fetch_all(&db.pool).await.unwrap_or_default();
|
||||
let mut album_tags: std::collections::HashMap<String, Vec<String>> = std::collections::HashMap::new();
|
||||
for r in &tag_rows {
|
||||
let aid: String = r.get("album_id");
|
||||
let tag: String = r.get("tag");
|
||||
album_tags.entry(aid).or_default().push(tag);
|
||||
}
|
||||
|
||||
// 2. 刷新标签
|
||||
// 批量获取 album → persons 映射
|
||||
let person_rows = sqlx::query(
|
||||
r#"SELECT pa.album_id, p.name FROM person_albums pa JOIN persons p ON pa.person_id = p.id"#
|
||||
).fetch_all(&db.pool).await.unwrap_or_default();
|
||||
let mut album_persons: std::collections::HashMap<String, Vec<String>> = std::collections::HashMap::new();
|
||||
for r in &person_rows {
|
||||
let aid: String = r.get("album_id");
|
||||
let name: String = r.get("name");
|
||||
album_persons.entry(aid).or_default().push(name);
|
||||
}
|
||||
|
||||
let enriched: Vec<AlbumInfo> = albums.into_iter().map(|mut a| {
|
||||
let id_str = a.id.to_string();
|
||||
a.tags = album_tags.remove(&id_str).unwrap_or_default();
|
||||
a.persons = album_persons.remove(&id_str).unwrap_or_default();
|
||||
a
|
||||
}).collect();
|
||||
|
||||
let mut c = albums_cache.write().await;
|
||||
c.update(enriched);
|
||||
|
||||
// 2. 刷新标签(从 search.db)
|
||||
let popular_tags = search_db.get_popular_tags(200).await;
|
||||
let tag_albums = search_db.get_all_tag_albums().await;
|
||||
let mut tc = tags_cache.write().await;
|
||||
tc.update(popular_tags, tag_albums);
|
||||
}
|
||||
|
||||
// 3. 刷新人物
|
||||
// 3. 刷新人物(从 search.db)
|
||||
let persons = search_db.get_all_persons_with_aliases().await;
|
||||
let mut pc = persons_cache.write().await;
|
||||
pc.update(persons);
|
||||
|
||||
Reference in New Issue
Block a user