diff --git a/youmubot-db-sql/src/models/osu.rs b/youmubot-db-sql/src/models/osu.rs index 9397966..ce8c9e5 100644 --- a/youmubot-db-sql/src/models/osu.rs +++ b/youmubot-db-sql/src/models/osu.rs @@ -218,6 +218,13 @@ impl CachedBeatmap { .await .map_err(Error::from) } + + /// Delete all of the caches. + pub async fn clear_all(conn: impl Executor<'_, Database = Database>) -> Result<()> { + conn.execute("DELETE FROM osu_cached_beatmapsets; DELETE FROM osu_cached_beatmaps;") + .await?; + Ok(()) + } } impl CachedBeatmap { @@ -291,6 +298,13 @@ impl CachedBeatmapContent { .await .map_err(Error::from) } + + /// Delete all of the caches. + pub async fn clear_all(conn: impl Executor<'_, Database = Database>) -> Result<()> { + conn.execute("DELETE FROM osu_cached_beatmap_contents;") + .await?; + Ok(()) + } } impl CachedBeatmapContent { diff --git a/youmubot-osu/src/discord/beatmap_cache.rs b/youmubot-osu/src/discord/beatmap_cache.rs index 40f3d76..b523844 100644 --- a/youmubot-osu/src/discord/beatmap_cache.rs +++ b/youmubot-osu/src/discord/beatmap_cache.rs @@ -23,6 +23,12 @@ impl BeatmapMetaCache { BeatmapMetaCache { client, pool } } + /// Clean the cache. + pub async fn clear(&self) -> Result<()> { + models::CachedBeatmap::clear_all(&self.pool).await?; + Ok(()) + } + #[allow(clippy::wrong_self_convention)] fn to_cached_beatmap(beatmap: &Beatmap, mode: Option) -> models::CachedBeatmap { models::CachedBeatmap { diff --git a/youmubot-osu/src/discord/mod.rs b/youmubot-osu/src/discord/mod.rs index b50d559..a8e7ff9 100644 --- a/youmubot-osu/src/discord/mod.rs +++ b/youmubot-osu/src/discord/mod.rs @@ -122,7 +122,8 @@ pub async fn setup( check, top, server_rank, - update_leaderboard + update_leaderboard, + clean_cache )] #[default_command(std)] struct Osu; @@ -702,6 +703,23 @@ pub async fn top(ctx: &Context, msg: &Message, mut args: Args) -> CommandResult Ok(()) } +#[command("cleancache")] +#[owners_only] +#[description = "Clean the beatmap cache."] +#[usage = "[--oppai to clear oppai cache as well]"] +#[max_args(1)] +pub async fn clean_cache(ctx: &Context, msg: &Message, args: Args) -> CommandResult { + let data = ctx.data.read().await; + let meta_cache = data.get::().unwrap(); + meta_cache.clear().await?; + if args.remains() == Some("--oppai") { + let oppai = data.get::().unwrap(); + oppai.clear().await?; + } + msg.reply_ping(ctx, "Beatmap cache cleared!").await?; + Ok(()) +} + async fn get_user(ctx: &Context, msg: &Message, mut args: Args, mode: Mode) -> CommandResult { let data = ctx.data.read().await; let user = to_user_id_query(args.single::().ok(), &data, msg).await?; diff --git a/youmubot-osu/src/discord/oppai_cache.rs b/youmubot-osu/src/discord/oppai_cache.rs index 6720d20..54d86ed 100644 --- a/youmubot-osu/src/discord/oppai_cache.rs +++ b/youmubot-osu/src/discord/oppai_cache.rs @@ -316,6 +316,12 @@ impl BeatmapCache { BeatmapCache { client, pool } } + /// Clean the cache. + pub async fn clear(&self) -> Result<()> { + models::CachedBeatmapContent::clear_all(&self.pool).await?; + Ok(()) + } + fn parse_beatmap(content: impl AsRef) -> Result { let content = content.as_ref(); let metadata = osuparse::parse_beatmap(content)