laravel-rust 0.1.0

Laravel for Rust: a full-stack Rust web framework, inspired by PHP Laravel
Documentation
//! The `cache:clear` and `cache:prune` commands.

use super::CacheManager;
use crate::Result;
use crate::console::{Console, clap};
use crate::container::{Inject, Registrar};
use crate::foundation::ServiceProvider;

pub(crate) struct CacheServiceProvider;

impl ServiceProvider for CacheServiceProvider {
    fn register(&self, app: &mut Registrar) {
        app.commands().add::<Clear>().add::<Prune>();
    }
}

/// Clear the application cache.
#[derive(clap::Parser)]
#[command(name = "cache:clear")]
struct Clear {
    /// The cache store to clear.
    store: Option<String>,
}

#[crate::console::command]
impl Clear {
    async fn handle(self, cache: Inject<CacheManager>, console: &mut Console) -> Result<()> {
        let store = cache.store(self.store.as_deref().unwrap_or(cache.default_store_name()))?;
        store.flush().await?;

        if matches!(
            crate::config().cache.stores.get(store.name()),
            Some(crate::config::cache::StoreConfig::Memory)
        ) {
            console.warn(format!(
                "Cache store `{}` only reaches this process, so other processes keep their entries.",
                store.name()
            ));
        }
        console.success(format!("Cache store `{}` cleared.", store.name()));
        Ok(())
    }
}

/// Prune expired cache entries.
#[derive(clap::Parser)]
#[command(name = "cache:prune")]
struct Prune {
    /// The cache store to prune.
    store: Option<String>,
}

#[crate::console::command]
impl Prune {
    async fn handle(self, cache: Inject<CacheManager>, console: &mut Console) -> Result<()> {
        let store = cache.store(self.store.as_deref().unwrap_or(cache.default_store_name()))?;
        store.prune().await?;

        console.success(format!("Expired entries pruned from cache store `{}`.", store.name()));
        Ok(())
    }
}