Skip to content

Commit

Permalink
perf: チャンネルキャッシュのロジックを改善 (#37)
Browse files Browse the repository at this point in the history
  • Loading branch information
m1sk9 committed Oct 28, 2023
1 parent 5d004c9 commit 1c229f1
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 52 deletions.
18 changes: 0 additions & 18 deletions src/adapters/cache.rs

This file was deleted.

33 changes: 33 additions & 0 deletions src/adapters/channel.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
use anyhow::Context;
use serenity::{
http::Http,
model::prelude::{ChannelId, GuildChannel, GuildId},
};
use std::sync::Arc;

pub async fn get_channel(
channel_id: ChannelId,
guild_id: GuildId,
http: &Arc<Http>,
) -> anyhow::Result<GuildChannel> {
let guild_channels = guild_id
.channels(&http)
.await
.context("チャンネルリストの取得に失敗しました")?;

let channel = match guild_channels.get(&channel_id) {
Some(channel) => channel.clone(),
None => {
let guild_threads = guild_id.get_active_threads(http).await?;
let thread = match guild_threads.threads.iter().find(|c| c.id == channel_id) {
Some(channel) => channel.clone(),
None => {
return Err(anyhow::anyhow!("引用元チャンネルは見つかりませんでした."));
}
};
thread
}
};

Ok(channel)
}
41 changes: 8 additions & 33 deletions src/adapters/message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ use std::sync::Arc;

use anyhow::{Context, Ok};
use serenity::{http::Http, model::channel::Message};
use tracing::{debug, info, warn};
use tracing::info;

use crate::adapters::cache::{get_cache, save_cache};
use crate::adapters::channel::get_channel;
use crate::adapters::embed::build_citation_embed;
use crate::model::cache::CHANNEL_LIST_CACHE;
use crate::model::{id::DiscordIds, message::CitationMessage};

pub async fn send_citation_embed(
Expand Down Expand Up @@ -40,37 +41,11 @@ async fn get_citation_message(
}: DiscordIds,
http: &Arc<Http>,
) -> anyhow::Result<CitationMessage> {
let target_channel = match get_cache(channel_id).await {
Some(channel) => {
debug!("--- キャッシュから引用元チャンネルを取得しました.");
channel
}
None => {
warn!("--- キャッシュに保存されていなかったため, 再取得します.");
let guild_channels = guild_id
.channels(&http)
.await
.context("チャンネルリストの取得に失敗しました")?;
let channel = match guild_channels.get(&channel_id) {
Some(channel) => channel.clone(),
None => {
let guild_threads = guild_id
.get_active_threads(http)
.await
.context("スレッドリストの取得に失敗しました")?;
let thread = match guild_threads.threads.iter().find(|c| c.id == channel_id) {
Some(channel) => channel.clone(),
None => {
return Err(anyhow::anyhow!("引用元チャンネルは見つかりませんでした."));
}
};
thread
}
};
save_cache(channel_id, channel.clone()).await;
channel
}
};
let target_channel = CHANNEL_LIST_CACHE
.get_with(channel_id, async move {
get_channel(channel_id, guild_id, http).await.unwrap()
})
.await;

if target_channel.is_nsfw() {
return Err(anyhow::anyhow!("引用元チャンネルはNSFWに指定されています"));
Expand Down
2 changes: 1 addition & 1 deletion src/adapters/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
pub mod cache;
pub mod channel;
pub mod embed;
pub mod message;

0 comments on commit 1c229f1

Please sign in to comment.