added cargo feature to cache guild prefixes. dont query language until necessary in framework

This commit is contained in:
2021-03-24 13:10:57 +00:00
parent 1473ac3bcf
commit c76a456af5
9 changed files with 173 additions and 50 deletions

View File

@ -44,6 +44,13 @@ use log::info;
use crate::models::GuildData;
use chrono_tz::Tz;
#[cfg(feature = "prefix-cache")]
struct PrefixCache;
#[cfg(feature = "prefix-cache")]
impl TypeMapKey for PrefixCache {
type Value = Arc<dashmap::DashMap<GuildId, String>>;
}
struct SQLPool;
impl TypeMapKey for SQLPool {
@ -165,6 +172,11 @@ DELETE FROM channels WHERE channel = ?
.cloned()
.expect("Could not get SQLPool from data");
#[cfg(feature = "prefix-cache")]
let prefix_cache = ctx.data.read().await.get::<PrefixCache>().cloned().unwrap();
#[cfg(feature = "prefix-cache")]
prefix_cache.remove(&guild.id);
sqlx::query!(
"
DELETE FROM guilds WHERE guild = ?
@ -262,6 +274,9 @@ async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
.expect("Error occurred creating client");
{
#[cfg(feature = "prefix-cache")]
let prefix_cache = dashmap::DashMap::new();
let pool = MySqlPool::connect(
&env::var("DATABASE_URL").expect("Missing DATABASE_URL from environment"),
)
@ -287,6 +302,9 @@ async fn main() -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
let mut data = client.data.write().await;
#[cfg(feature = "prefix-cache")]
data.insert::<PrefixCache>(Arc::new(prefix_cache));
data.insert::<SQLPool>(pool);
data.insert::<PopularTimezones>(Arc::new(popular_timezones));
data.insert::<ReqwestClient>(Arc::new(reqwest::Client::new()));