Add cache clear command

This commit is contained in:
Natsu Kagami 2024-02-01 23:19:31 +01:00
parent 5998b37bd8
commit dc973c93dc
Signed by: nki
GPG key ID: 55A032EB38B49ADB
4 changed files with 45 additions and 1 deletions

View file

@ -218,6 +218,13 @@ impl CachedBeatmap {
.await .await
.map_err(Error::from) .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 { impl CachedBeatmap {
@ -291,6 +298,13 @@ impl CachedBeatmapContent {
.await .await
.map_err(Error::from) .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 { impl CachedBeatmapContent {

View file

@ -23,6 +23,12 @@ impl BeatmapMetaCache {
BeatmapMetaCache { client, pool } 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)] #[allow(clippy::wrong_self_convention)]
fn to_cached_beatmap(beatmap: &Beatmap, mode: Option<Mode>) -> models::CachedBeatmap { fn to_cached_beatmap(beatmap: &Beatmap, mode: Option<Mode>) -> models::CachedBeatmap {
models::CachedBeatmap { models::CachedBeatmap {

View file

@ -122,7 +122,8 @@ pub async fn setup(
check, check,
top, top,
server_rank, server_rank,
update_leaderboard update_leaderboard,
clean_cache
)] )]
#[default_command(std)] #[default_command(std)]
struct Osu; struct Osu;
@ -702,6 +703,23 @@ pub async fn top(ctx: &Context, msg: &Message, mut args: Args) -> CommandResult
Ok(()) 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::<BeatmapMetaCache>().unwrap();
meta_cache.clear().await?;
if args.remains() == Some("--oppai") {
let oppai = data.get::<BeatmapCache>().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 { async fn get_user(ctx: &Context, msg: &Message, mut args: Args, mode: Mode) -> CommandResult {
let data = ctx.data.read().await; let data = ctx.data.read().await;
let user = to_user_id_query(args.single::<UsernameArg>().ok(), &data, msg).await?; let user = to_user_id_query(args.single::<UsernameArg>().ok(), &data, msg).await?;

View file

@ -316,6 +316,12 @@ impl BeatmapCache {
BeatmapCache { client, pool } 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<str>) -> Result<BeatmapContent> { fn parse_beatmap(content: impl AsRef<str>) -> Result<BeatmapContent> {
let content = content.as_ref(); let content = content.as_ref();
let metadata = osuparse::parse_beatmap(content) let metadata = osuparse::parse_beatmap(content)