This commit is contained in:
lychang
2026-07-20 02:15:39 +08:00
parent 1700585a36
commit be581977f3
10 changed files with 399 additions and 115 deletions

View File

@@ -53,13 +53,17 @@ async fn main() -> std::io::Result<()> {
let image_processor = Arc::new(image_processor::ImageProcessor::new(config.storage.clone()));
// 相册内存缓存(必须在管线任务之前创建)
// ── 内存缓存(必须在管线任务之前创建)──
let albums_cache = Arc::new(tokio::sync::RwLock::new(AlbumsCache::new()));
let persons_cache = Arc::new(tokio::sync::RwLock::new(cache::PersonCache::default()));
let tags_cache = Arc::new(tokio::sync::RwLock::new(cache::TagCache::default()));
// ── 启动管线(单次):导入 → 解析 → 合并 → 缓存刷新 ──
let boot_config = config.clone();
let boot_db = Arc::clone(&db);
let boot_cache = Arc::clone(&albums_cache);
let boot_albums_cache = Arc::clone(&albums_cache);
let boot_persons_cache = Arc::clone(&persons_cache);
let boot_tags_cache = Arc::clone(&tags_cache);
tokio::spawn(async move {
// ① 扫描导入新相册
info!("[启动管线] ① 扫描导入新相册...");
@@ -77,15 +81,17 @@ async fn main() -> std::io::Result<()> {
result.persons_updated, result.tags_updated,
result.persons_merged, result.aliases_moved, result.tags_removed_person);
// ③ 刷新内存缓存
info!("[启动管线] ③ 刷新内存缓存...");
refresh_albums_cache(&boot_db, &boot_cache).await;
// ③ 刷新所有内存缓存
info!("[启动管线] ③ 刷新所有内存缓存...");
cache::refresh_all_caches(&boot_db, &boot_albums_cache, &boot_persons_cache, &boot_tags_cache).await;
info!("[启动管线] 启动管线执行完毕");
});
// ── 定时管线(每 30 分钟):解析 → 合并 → 缓存刷新 ──
let timer_cache = Arc::clone(&albums_cache);
let timer_db = Arc::clone(&db);
let timer_persons_cache = Arc::clone(&persons_cache);
let timer_tags_cache = Arc::clone(&tags_cache);
tokio::spawn(async move {
let mut interval = interval(Duration::from_secs(30 * 60));
loop {
@@ -100,9 +106,9 @@ async fn main() -> std::io::Result<()> {
result.persons_updated, result.tags_updated,
result.persons_merged, result.aliases_moved, result.tags_removed_person);
// ② 刷新内存缓存
info!("[定时管线] ② 刷新内存缓存...");
refresh_albums_cache(&timer_db, &timer_cache).await;
// ② 刷新所有内存缓存
info!("[定时管线] ② 刷新所有内存缓存...");
cache::refresh_all_caches(&timer_db, &timer_cache, &timer_persons_cache, &timer_tags_cache).await;
}
});
@@ -157,6 +163,8 @@ async fn main() -> std::io::Result<()> {
.app_data(web::Data::new(Arc::clone(&db)))
.app_data(web::Data::new(Arc::clone(&image_processor)))
.app_data(web::Data::new(Arc::clone(&albums_cache)))
.app_data(web::Data::new(Arc::clone(&persons_cache)))
.app_data(web::Data::new(Arc::clone(&tags_cache)))
.app_data(web::Data::new(Arc::clone(&task_queue)))
// 相册 API
.route("/api/albums", web::get().to(routes::get_albums))
@@ -238,16 +246,3 @@ async fn main() -> std::io::Result<()> {
server.await
}
/// 从 DB 重新加载相册数据到内存缓存
async fn refresh_albums_cache(db: &crate::db::Database, cache: &Arc<tokio::sync::RwLock<AlbumsCache>>) {
match db.get_all_albums().await {
Ok(albums) => {
let mut c = cache.write().await;
c.update(albums);
}
Err(e) => {
eprintln!("❌ 缓存刷新失败: {}", e);
}
}
}