Skip to main content

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 gamebrain_entries: i32,
17    pub steam_store: i32,
18    pub steam_reviews: i32,
19    pub steam_playtime: i32,
20    pub expired: i32,
21}
22
23/// Remove entradas expiradas do cache
24#[tauri::command]
25pub fn cleanup_cache(state: State<AppState>) -> Result<String, AppError> {
26    let conn = state.cache_db.lock()?;
27
28    let deleted = cache::cleanup_expired_cache(&conn).map_err(AppError::DatabaseError)?;
29
30    Ok(format!("{} entradas removidas", deleted))
31}
32
33/// Limpa TODO o cache (use com cuidado)
34#[tauri::command]
35pub fn clear_all_cache(state: State<AppState>) -> Result<String, AppError> {
36    let conn = state.cache_db.lock()?;
37
38    let deleted = conn.execute("DELETE FROM api_cache", [])?;
39
40    Ok(format!("Cache limpo: {} entradas removidas", deleted))
41}
42
43#[tauri::command]
44pub fn get_detailed_cache_stats(state: State<AppState>) -> Result<DetailedCacheStats, AppError> {
45    let conn = state.cache_db.lock()?;
46
47    let total: i32 = conn
48        .query_row("SELECT COUNT(*) FROM api_cache", [], |row| row.get(0))
49        .unwrap_or(0);
50
51    let rawg: i32 = conn
52        .query_row(
53            "SELECT COUNT(*) FROM api_cache
54             WHERE source = 'rawg' AND external_id LIKE 'search_%'",
55            [],
56            |row| row.get(0),
57        )
58        .unwrap_or(0);
59
60    let gamebrain: i32 = conn
61        .query_row(
62            "SELECT COUNT(*) FROM api_cache
63             WHERE source = 'gamebrain'",
64            [],
65            |row| row.get(0),
66        )
67        .unwrap_or(0);
68
69    let store: i32 = conn
70        .query_row(
71            "SELECT COUNT(*) FROM api_cache
72             WHERE source = 'steam' AND external_id LIKE 'store_%'",
73            [],
74            |row| row.get(0),
75        )
76        .unwrap_or(0);
77
78    let reviews: i32 = conn
79        .query_row(
80            "SELECT COUNT(*) FROM api_cache
81             WHERE source = 'steam' AND external_id LIKE 'reviews_%'",
82            [],
83            |row| row.get(0),
84        )
85        .unwrap_or(0);
86
87    let playtime: i32 = conn
88        .query_row(
89            "SELECT COUNT(*) FROM api_cache
90             WHERE source = 'steam' AND external_id LIKE 'playtime_%'",
91            [],
92            |row| row.get(0),
93        )
94        .unwrap_or(0);
95
96    let stats = cache::get_cache_stats(&conn).map_err(AppError::DatabaseError)?;
97
98    Ok(DetailedCacheStats {
99        total,
100        rawg_searches: rawg,
101        gamebrain_entries: gamebrain,
102        steam_store: store,
103        steam_reviews: reviews,
104        steam_playtime: playtime,
105        expired: stats.expired_entries,
106    })
107}