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>();
}
}
#[derive(clap::Parser)]
#[command(name = "cache:clear")]
struct 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(())
}
}
#[derive(clap::Parser)]
#[command(name = "cache:prune")]
struct 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(())
}
}