Corrections mineurs des systèmes de caches
This commit is contained in:
@@ -115,7 +115,7 @@ pub struct ErrorResponse {
|
||||
/// Liste tous les items en cache avec leurs statistiques
|
||||
///
|
||||
/// Retourne la liste complète des entrées du cache triées par nombre d'accès décroissant.
|
||||
pub async fn list_items<C: CacheConfig>(State(cache): State<Arc<Cache<C>>>) -> impl IntoResponse {
|
||||
pub async fn list_items<C: CacheConfig + 'static>(State(cache): State<Arc<Cache<C>>>) -> impl IntoResponse {
|
||||
match cache.db.get_all(true) {
|
||||
Ok(entries) => (StatusCode::OK, Json(entries)).into_response(),
|
||||
Err(e) => (
|
||||
@@ -132,7 +132,7 @@ pub async fn list_items<C: CacheConfig>(State(cache): State<Arc<Cache<C>>>) -> i
|
||||
/// Récupère les informations d'un item spécifique
|
||||
///
|
||||
/// Retourne les métadonnées d'un item identifié par sa clé (pk).
|
||||
pub async fn get_item_info<C: CacheConfig>(
|
||||
pub async fn get_item_info<C: CacheConfig + 'static>(
|
||||
State(cache): State<Arc<Cache<C>>>,
|
||||
Path(pk): Path<String>,
|
||||
) -> impl IntoResponse {
|
||||
@@ -153,7 +153,7 @@ pub async fn get_item_info<C: CacheConfig>(
|
||||
///
|
||||
/// Retourne le statut actuel du téléchargement (progression, tailles, erreurs).
|
||||
/// Si le téléchargement est terminé, retourne les informations du fichier.
|
||||
pub async fn get_download_status<C: CacheConfig>(
|
||||
pub async fn get_download_status<C: CacheConfig + 'static>(
|
||||
State(cache): State<Arc<Cache<C>>>,
|
||||
Path(pk): Path<String>,
|
||||
) -> impl IntoResponse {
|
||||
@@ -265,7 +265,7 @@ enum AddSource<'a> {
|
||||
///
|
||||
/// Télécharge l'item depuis l'URL fournie et l'ajoute au cache.
|
||||
/// Si l'item existe déjà, il est mis à jour.
|
||||
pub async fn add_item<C: CacheConfig>(
|
||||
pub async fn add_item<C: CacheConfig + 'static>(
|
||||
State(cache): State<Arc<Cache<C>>>,
|
||||
Json(req): Json<AddItemRequest>,
|
||||
) -> impl IntoResponse {
|
||||
@@ -337,7 +337,7 @@ pub async fn add_item<C: CacheConfig>(
|
||||
/// Supprime un item du cache
|
||||
///
|
||||
/// Supprime l'item et toutes ses variantes du disque et de la base de données.
|
||||
pub async fn delete_item<C: CacheConfig>(
|
||||
pub async fn delete_item<C: CacheConfig + 'static>(
|
||||
State(cache): State<Arc<Cache<C>>>,
|
||||
Path(pk): Path<String>,
|
||||
) -> impl IntoResponse {
|
||||
@@ -389,7 +389,7 @@ pub async fn delete_item<C: CacheConfig>(
|
||||
/// Purge complètement le cache
|
||||
///
|
||||
/// Supprime tous les items et vide la base de données. Opération irréversible.
|
||||
pub async fn purge_cache<C: CacheConfig>(State(cache): State<Arc<Cache<C>>>) -> impl IntoResponse {
|
||||
pub async fn purge_cache<C: CacheConfig + 'static>(State(cache): State<Arc<Cache<C>>>) -> impl IntoResponse {
|
||||
match cache.purge().await {
|
||||
Ok(_) => (
|
||||
StatusCode::OK,
|
||||
@@ -413,7 +413,7 @@ pub async fn purge_cache<C: CacheConfig>(State(cache): State<Arc<Cache<C>>>) ->
|
||||
///
|
||||
/// Re-télécharge les items manquants et supprime les fichiers orphelins.
|
||||
/// Utile pour réparer un cache corrompu.
|
||||
pub async fn consolidate_cache<C: CacheConfig>(
|
||||
pub async fn consolidate_cache<C: CacheConfig + 'static>(
|
||||
State(cache): State<Arc<Cache<C>>>,
|
||||
) -> impl IntoResponse {
|
||||
match cache.consolidate().await {
|
||||
|
||||
@@ -152,7 +152,7 @@ pub struct Cache<C: CacheConfig> {
|
||||
_phantom: std::marker::PhantomData<C>,
|
||||
}
|
||||
|
||||
impl<C: CacheConfig> Cache<C> {
|
||||
impl<C: CacheConfig + 'static> Cache<C> {
|
||||
/// Retourne le chemin du fichier marker de complétion
|
||||
fn get_completion_marker_path(&self, pk: &str) -> PathBuf {
|
||||
self.get_file_path(pk)
|
||||
@@ -424,6 +424,58 @@ impl<C: CacheConfig> Cache<C> {
|
||||
})
|
||||
}
|
||||
|
||||
/// Lance une consolidation en arrière-plan pour un cache existant
|
||||
///
|
||||
/// Cette fonction utilitaire lance une tâche asynchrone qui consolide le cache
|
||||
/// (supprime les fichiers incomplets sans marker de complétion) et retourne
|
||||
/// immédiatement le cache fourni en paramètre.
|
||||
///
|
||||
/// Idéal pour les crates spécialisées qui veulent offrir une fonction
|
||||
/// `new_cache_with_consolidation` sans dupliquer la logique de lancement.
|
||||
///
|
||||
/// # Arguments
|
||||
///
|
||||
/// * `cache` - Instance du cache à consolider
|
||||
///
|
||||
/// # Returns
|
||||
///
|
||||
/// Le même `Arc<Cache<C>>` fourni en paramètre
|
||||
///
|
||||
/// # Exemple
|
||||
///
|
||||
/// ```rust,ignore
|
||||
/// use pmocache::{Cache, CacheConfig};
|
||||
/// use std::sync::Arc;
|
||||
///
|
||||
/// struct MyConfig;
|
||||
/// impl CacheConfig for MyConfig {
|
||||
/// fn file_extension() -> &'static str { "dat" }
|
||||
/// }
|
||||
///
|
||||
/// async fn create_cache_with_cleanup() -> anyhow::Result<Arc<Cache<MyConfig>>> {
|
||||
/// let cache = Arc::new(Cache::new("./cache", 1000)?);
|
||||
/// Ok(Cache::with_consolidation(cache).await)
|
||||
/// }
|
||||
/// ```
|
||||
pub async fn with_consolidation(cache: Arc<Cache<C>>) -> Arc<Cache<C>> {
|
||||
let cache_clone = cache.clone();
|
||||
tokio::spawn(async move {
|
||||
if let Err(e) = cache_clone.consolidate().await {
|
||||
tracing::warn!(
|
||||
"Failed to consolidate {} cache on startup: {}",
|
||||
C::cache_name(),
|
||||
e
|
||||
);
|
||||
} else {
|
||||
tracing::info!(
|
||||
"{} cache consolidated successfully on startup",
|
||||
C::cache_name()
|
||||
);
|
||||
}
|
||||
});
|
||||
cache
|
||||
}
|
||||
|
||||
/// Configure la taille minimale de prébuffering
|
||||
///
|
||||
/// # Arguments
|
||||
@@ -1559,7 +1611,7 @@ fn link_file(source: &Path, destination: &Path) -> std::io::Result<()> {
|
||||
}
|
||||
|
||||
/// Implémentation du trait FileCache pour Cache
|
||||
impl<C: CacheConfig> FileCache<C> for Cache<C> {
|
||||
impl<C: CacheConfig + 'static> FileCache<C> for Cache<C> {
|
||||
fn get_cache_dir(&self) -> &Path {
|
||||
self.cache_dir()
|
||||
}
|
||||
|
||||
@@ -91,7 +91,7 @@ pub trait CacheConfigExt {
|
||||
/// let config = get_config();
|
||||
/// let cache = config.create_cache::<AudioConfig>("audio_cache", "cache_audio", 500)?;
|
||||
/// ```
|
||||
fn create_cache<C: crate::CacheConfig>(
|
||||
fn create_cache<C: crate::CacheConfig + 'static>(
|
||||
&self,
|
||||
cache_type: &str,
|
||||
default_dir: &str,
|
||||
@@ -121,7 +121,7 @@ impl CacheConfigExt for Config {
|
||||
self.set_value(&["host", cache_type, "size"], Value::Number(n))
|
||||
}
|
||||
|
||||
fn create_cache<C: crate::CacheConfig>(
|
||||
fn create_cache<C: crate::CacheConfig + 'static>(
|
||||
&self,
|
||||
cache_type: &str,
|
||||
default_dir: &str,
|
||||
|
||||
@@ -111,7 +111,7 @@ async fn get_file_with_param<C: CacheConfig + 'static>(
|
||||
}
|
||||
|
||||
#[cfg(feature = "pmoserver")]
|
||||
async fn serve_finalized_pk<C: CacheConfig>(
|
||||
async fn serve_finalized_pk<C: CacheConfig + 'static>(
|
||||
cache: &Arc<Cache<C>>,
|
||||
pk: &str,
|
||||
param: &str,
|
||||
@@ -143,7 +143,7 @@ async fn serve_finalized_pk<C: CacheConfig>(
|
||||
/// 5. Broadcast l'event pour PK switching
|
||||
/// 6. Sert directement le fichier téléchargé
|
||||
#[cfg(feature = "pmoserver")]
|
||||
async fn serve_lazy_audio_file<C: CacheConfig>(
|
||||
async fn serve_lazy_audio_file<C: CacheConfig + 'static>(
|
||||
cache: &Arc<Cache<C>>,
|
||||
lazy_pk: &str,
|
||||
param: &str,
|
||||
@@ -187,7 +187,7 @@ async fn serve_lazy_audio_file<C: CacheConfig>(
|
||||
/// Sinon, le fichier complet est servi normalement.
|
||||
/// Si le fichier n'existe pas et qu'un param_generator est fourni, tente de générer le param.
|
||||
#[cfg(feature = "pmoserver")]
|
||||
async fn serve_file_with_streaming<C: CacheConfig>(
|
||||
async fn serve_file_with_streaming<C: CacheConfig + 'static>(
|
||||
cache: &Arc<Cache<C>>,
|
||||
pk: &str,
|
||||
param: &str,
|
||||
|
||||
Reference in New Issue
Block a user