mirror of
https://github.com/natsukagami/youmubot.git
synced 2025-04-19 00:38:54 +00:00
explicitly cache member list
This commit is contained in:
parent
a70da4fa03
commit
13ce7ebe6d
2 changed files with 49 additions and 95 deletions
|
@ -46,7 +46,7 @@ pub async fn server_rank(ctx: &Context, m: &Message, mut args: Args) -> CommandR
|
||||||
.single::<ModeOrTotal>()
|
.single::<ModeOrTotal>()
|
||||||
.unwrap_or(ModeOrTotal::Mode(Mode::Std));
|
.unwrap_or(ModeOrTotal::Mode(Mode::Std));
|
||||||
let guild = m.guild_id.expect("Guild-only command");
|
let guild = m.guild_id.expect("Guild-only command");
|
||||||
// let member_cache = data.get::<MemberCache>().unwrap();
|
let member_cache = data.get::<MemberCache>().unwrap();
|
||||||
let osu_users = data
|
let osu_users = data
|
||||||
.get::<OsuSavedUsers>()
|
.get::<OsuSavedUsers>()
|
||||||
.unwrap()
|
.unwrap()
|
||||||
|
@ -55,30 +55,22 @@ pub async fn server_rank(ctx: &Context, m: &Message, mut args: Args) -> CommandR
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.map(|v| (v.user_id, v))
|
.map(|v| (v.user_id, v))
|
||||||
.collect::<HashMap<_, _>>();
|
.collect::<HashMap<_, _>>();
|
||||||
let users = guild
|
let users = member_cache
|
||||||
.members_iter(ctx)
|
.query_members(&ctx, guild)
|
||||||
.filter_map(|m| {
|
.await?
|
||||||
future::ready(
|
.iter()
|
||||||
m.ok()
|
.filter_map(|m| osu_users.get(&m.user.id).map(|ou| (m, ou)))
|
||||||
.and_then(|m| osu_users.get(&m.user.id).map(|ou| (m, ou))),
|
|
||||||
)
|
|
||||||
})
|
|
||||||
.filter_map(|(member, osu_user)| {
|
.filter_map(|(member, osu_user)| {
|
||||||
future::ready(|| -> Option<_> {
|
|
||||||
let pp = match mode {
|
let pp = match mode {
|
||||||
ModeOrTotal::Total
|
ModeOrTotal::Total if osu_user.pp.iter().any(|v| v.is_some_and(|v| v > 0.0)) => {
|
||||||
if osu_user.pp.iter().any(|v| v.is_some_and(|v| v > 0.0)) =>
|
|
||||||
{
|
|
||||||
Some(osu_user.pp.iter().map(|v| v.unwrap_or(0.0)).sum())
|
Some(osu_user.pp.iter().map(|v| v.unwrap_or(0.0)).sum())
|
||||||
}
|
}
|
||||||
ModeOrTotal::Mode(m) => osu_user.pp.get(m as usize).and_then(|v| *v),
|
ModeOrTotal::Mode(m) => osu_user.pp.get(m as usize).and_then(|v| *v),
|
||||||
_ => None,
|
_ => None,
|
||||||
}?;
|
}?;
|
||||||
Some((pp, member.user.name, osu_user))
|
Some((pp, member.user.name.clone(), osu_user))
|
||||||
}())
|
|
||||||
})
|
})
|
||||||
.collect::<Vec<_>>()
|
.collect::<Vec<_>>();
|
||||||
.await;
|
|
||||||
let last_update = users.iter().map(|(_, _, a)| a.last_update).min();
|
let last_update = users.iter().map(|(_, _, a)| a.last_update).min();
|
||||||
let mut users = users
|
let mut users = users
|
||||||
.into_iter()
|
.into_iter()
|
||||||
|
@ -197,6 +189,7 @@ pub async fn show_leaderboard(ctx: &Context, m: &Message, mut args: Args) -> Com
|
||||||
let style = args.single::<ScoreListStyle>().unwrap_or_default();
|
let style = args.single::<ScoreListStyle>().unwrap_or_default();
|
||||||
|
|
||||||
let data = ctx.data.read().await;
|
let data = ctx.data.read().await;
|
||||||
|
let member_cache = data.get::<MemberCache>().unwrap();
|
||||||
|
|
||||||
let (bm, _) = match super::load_beatmap(ctx, m).await {
|
let (bm, _) = match super::load_beatmap(ctx, m).await {
|
||||||
Some((bm, mods_def)) => {
|
Some((bm, mods_def)) => {
|
||||||
|
@ -230,17 +223,11 @@ pub async fn show_leaderboard(ctx: &Context, m: &Message, mut args: Args) -> Com
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.map(|v| (v.user_id, v))
|
.map(|v| (v.user_id, v))
|
||||||
.collect::<HashMap<_, _>>();
|
.collect::<HashMap<_, _>>();
|
||||||
let mut scores = guild
|
let mut scores = member_cache
|
||||||
.members_iter(&ctx)
|
.query_members(&ctx, guild)
|
||||||
.filter_map(|mem| {
|
.await?
|
||||||
future::ready(
|
.iter()
|
||||||
mem.ok()
|
.filter_map(|m| osu_users.get(&m.user.id).map(|ou| (m.distinct(), ou.id)))
|
||||||
.and_then(|m| osu_users.get(&m.user.id).map(|ou| (m.distinct(), ou.id))),
|
|
||||||
)
|
|
||||||
})
|
|
||||||
.collect::<Vec<_>>()
|
|
||||||
.await
|
|
||||||
.into_iter()
|
|
||||||
.map(|(mem, osu_id)| {
|
.map(|(mem, osu_id)| {
|
||||||
osu.scores(bm.0.beatmap_id, move |f| {
|
osu.scores(bm.0.beatmap_id, move |f| {
|
||||||
f.user(UserID::ID(osu_id)).mode(bm.1)
|
f.user(UserID::ID(osu_id)).mode(bm.1)
|
||||||
|
|
|
@ -5,19 +5,15 @@ use serenity::model::{
|
||||||
id::{GuildId, UserId},
|
id::{GuildId, UserId},
|
||||||
};
|
};
|
||||||
use serenity::{http::CacheHttp, prelude::*};
|
use serenity::{http::CacheHttp, prelude::*};
|
||||||
use std::collections::HashMap as Map;
|
|
||||||
use std::sync::Arc;
|
use std::sync::Arc;
|
||||||
|
|
||||||
use crate::OkPrint;
|
|
||||||
|
|
||||||
const VALID_CACHE_SECONDS: i64 = 15 * 60; // 15 minutes
|
const VALID_CACHE_SECONDS: i64 = 15 * 60; // 15 minutes
|
||||||
|
|
||||||
/// MemberCache resolves `does User belong to Guild` requests, and store them in a cache.
|
/// MemberCache resolves `does User belong to Guild` requests, and store them in a cache.
|
||||||
#[derive(Debug, Default)]
|
#[derive(Debug, Default)]
|
||||||
pub struct MemberCache {
|
pub struct MemberCache {
|
||||||
per_user: DashMap<(UserId, GuildId), Expiring<Option<Member>>>,
|
per_user: DashMap<(UserId, GuildId), Expiring<Option<Member>>>,
|
||||||
per_guild: DashMap<GuildId, Expiring<Map<UserId, Member>>>,
|
per_guild: DashMap<GuildId, Expiring<Arc<[Member]>>>,
|
||||||
guild_counts: DashMap<GuildId, Option<u64>>,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
|
@ -50,62 +46,6 @@ impl MemberCache {
|
||||||
cache_http: impl CacheHttp,
|
cache_http: impl CacheHttp,
|
||||||
user_id: UserId,
|
user_id: UserId,
|
||||||
guild_id: GuildId,
|
guild_id: GuildId,
|
||||||
) -> Option<Member> {
|
|
||||||
let members_count = match self.guild_counts.get(&guild_id) {
|
|
||||||
Some(v) => v.clone(),
|
|
||||||
None => {
|
|
||||||
let res = guild_id
|
|
||||||
.to_partial_guild_with_counts(cache_http.http())
|
|
||||||
.await
|
|
||||||
.ok()
|
|
||||||
.and_then(|v| v.approximate_member_count);
|
|
||||||
self.guild_counts.insert(guild_id, res);
|
|
||||||
res
|
|
||||||
}
|
|
||||||
};
|
|
||||||
match members_count {
|
|
||||||
Some(g) if g <= 1000 => self.query_per_guild(cache_http, user_id, guild_id).await,
|
|
||||||
_ => self.query_per_user(cache_http, user_id, guild_id).await,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
async fn query_per_guild(
|
|
||||||
&self,
|
|
||||||
cache_http: impl CacheHttp,
|
|
||||||
user_id: UserId,
|
|
||||||
guild_id: GuildId,
|
|
||||||
) -> Option<Member> {
|
|
||||||
let now = Utc::now();
|
|
||||||
// Check cache
|
|
||||||
if let Some(r) = self.per_guild.get(&guild_id) {
|
|
||||||
if r.timeout > now {
|
|
||||||
return r.get(&user_id).cloned();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
// query
|
|
||||||
let members = guild_id
|
|
||||||
.members(cache_http.http(), None, None)
|
|
||||||
.await
|
|
||||||
.pls_ok()?
|
|
||||||
.into_iter()
|
|
||||||
.map(|m| (m.user.id, m))
|
|
||||||
.collect::<Map<_, _>>();
|
|
||||||
let result = members.get(&user_id).cloned();
|
|
||||||
self.per_guild.insert(
|
|
||||||
guild_id,
|
|
||||||
Expiring::new(
|
|
||||||
members,
|
|
||||||
now + chrono::Duration::seconds(VALID_CACHE_SECONDS),
|
|
||||||
),
|
|
||||||
);
|
|
||||||
result
|
|
||||||
}
|
|
||||||
|
|
||||||
async fn query_per_user(
|
|
||||||
&self,
|
|
||||||
cache_http: impl CacheHttp,
|
|
||||||
user_id: UserId,
|
|
||||||
guild_id: GuildId,
|
|
||||||
) -> Option<Member> {
|
) -> Option<Member> {
|
||||||
let now = Utc::now();
|
let now = Utc::now();
|
||||||
// Check cache
|
// Check cache
|
||||||
|
@ -125,4 +65,31 @@ impl MemberCache {
|
||||||
);
|
);
|
||||||
t
|
t
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub async fn query_members(
|
||||||
|
&self,
|
||||||
|
cache_http: impl CacheHttp,
|
||||||
|
guild_id: GuildId,
|
||||||
|
) -> crate::Result<Arc<[Member]>> {
|
||||||
|
let now = Utc::now();
|
||||||
|
// Check cache
|
||||||
|
if let Some(r) = self.per_guild.get(&guild_id) {
|
||||||
|
if r.timeout > now {
|
||||||
|
return Ok(r.value.clone());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// query
|
||||||
|
let members: Arc<[Member]> = guild_id
|
||||||
|
.members(cache_http.http(), None, None)
|
||||||
|
.await?
|
||||||
|
.into();
|
||||||
|
self.per_guild.insert(
|
||||||
|
guild_id,
|
||||||
|
Expiring::new(
|
||||||
|
members.clone(),
|
||||||
|
now + chrono::Duration::seconds(VALID_CACHE_SECONDS),
|
||||||
|
),
|
||||||
|
);
|
||||||
|
Ok(members)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue