use std::collections::HashMap;
use std::sync::Arc;
use tokio::sync::RwLock;
use crate::model::{Channel, Guild, Snowflake, User};
pub struct Cache {
pub guilds: RwLock<HashMap<Snowflake, Guild>>,
pub channels: RwLock<HashMap<Snowflake, Channel>>,
pub users: RwLock<HashMap<Snowflake, User>>,
pub current_user: RwLock<Option<User>>,
}
impl Cache {
pub fn new() -> Arc<Self> {
Arc::new(Self {
guilds: RwLock::new(HashMap::new()),
channels: RwLock::new(HashMap::new()),
users: RwLock::new(HashMap::new()),
current_user: RwLock::new(None),
})
}
pub async fn guild(&self, id: &str) -> Option<Guild> {
self.guilds.read().await.get(id).cloned()
}
pub async fn channel(&self, id: &str) -> Option<Channel> {
self.channels.read().await.get(id).cloned()
}
pub async fn user(&self, id: &str) -> Option<User> {
self.users.read().await.get(id).cloned()
}
pub async fn current_user(&self) -> Option<User> {
self.current_user.read().await.clone()
}
pub async fn guild_count(&self) -> usize {
self.guilds.read().await.len()
}
}