game_manager_lib\commands/
caches.rs

1//! Comandos para gerenciar o cache de metadados
2//!
3//! Permite visualizar estatísticas, limpar cache expirado e invalidar entradas específicas
4//! Expõe comandos Tauri para uso no frontend.
5
6use crate::database::AppState;
7use crate::errors::AppError;
8use crate::services::cache;
9use tauri::State;
10
11/// Estatísticas detalhadas por tipo de cache
12#[derive(serde::Serialize)]
13pub struct DetailedCacheStats {
14    pub total: i32,
15    pub rawg_searches: i32,
16    pub steam_store: i32,
17    pub steam_reviews: i32,
18    pub steam_playtime: i32,
19    pub expired: i32,
20}
21
22/// Remove entradas expiradas do cache
23#[tauri::command]
24pub fn cleanup_cache(state: State<AppState>) -> Result<String, AppError> {
25    let conn = state.metadata_db.lock()?;
26
27    let deleted = cache::cleanup_expired_cache(&conn).map_err(AppError::DatabaseError)?;
28
29    Ok(format!("{} entradas removidas", deleted))
30}
31
32/// Limpa TODO o cache (use com cuidado)
33#[tauri::command]
34pub fn clear_all_cache(state: State<AppState>) -> Result<String, AppError> {
35    let conn = state.metadata_db.lock()?;
36
37    let deleted = conn.execute("DELETE FROM api_cache", [])?;
38
39    Ok(format!("Cache limpo: {} entradas removidas", deleted))
40}
41
42#[tauri::command]
43pub fn get_detailed_cache_stats(state: State<AppState>) -> Result<DetailedCacheStats, AppError> {
44    let conn = state.metadata_db.lock()?;
45
46    let total: i32 = conn
47        .query_row("SELECT COUNT(*) FROM api_cache", [], |row| row.get(0))
48        .unwrap_or(0);
49
50    let rawg: i32 = conn
51        .query_row(
52            "SELECT COUNT(*) FROM api_cache
53             WHERE source = 'rawg' AND external_id LIKE 'search_%'",
54            [],
55            |row| row.get(0),
56        )
57        .unwrap_or(0);
58
59    let store: i32 = conn
60        .query_row(
61            "SELECT COUNT(*) FROM api_cache
62             WHERE source = 'steam' AND external_id LIKE 'store_%'",
63            [],
64            |row| row.get(0),
65        )
66        .unwrap_or(0);
67
68    let reviews: i32 = conn
69        .query_row(
70            "SELECT COUNT(*) FROM api_cache
71             WHERE source = 'steam' AND external_id LIKE 'reviews_%'",
72            [],
73            |row| row.get(0),
74        )
75        .unwrap_or(0);
76
77    let playtime: i32 = conn
78        .query_row(
79            "SELECT COUNT(*) FROM api_cache
80             WHERE source = 'steam' AND external_id LIKE 'playtime_%'",
81            [],
82            |row| row.get(0),
83        )
84        .unwrap_or(0);
85
86    let stats = cache::get_cache_stats(&conn).map_err(AppError::DatabaseError)?;
87
88    Ok(DetailedCacheStats {
89        total,
90        rawg_searches: rawg,
91        steam_store: store,
92        steam_reviews: reviews,
93        steam_playtime: playtime,
94        expired: stats.expired_entries,
95    })
96}