twilight_cache_inmemory/
stats.rs1use twilight_model::id::{
2 marker::{ChannelMarker, GuildMarker},
3 Id,
4};
5
6use crate::{CacheableModels, DefaultCacheModels};
7
8use super::InMemoryCache;
9
10#[allow(clippy::type_complexity)]
33#[derive(Clone, Debug)]
34pub struct InMemoryCacheStats<'a, CacheModels: CacheableModels = DefaultCacheModels>(
35 &'a InMemoryCache<CacheModels>,
36);
37
38impl<'a, CacheModels: CacheableModels> InMemoryCacheStats<'a, CacheModels> {
39 #[allow(clippy::type_complexity)]
40 pub(super) const fn new(cache: &'a InMemoryCache<CacheModels>) -> Self {
41 Self(cache)
42 }
43
44 #[allow(clippy::type_complexity)]
46 pub const fn cache_ref(&'a self) -> &'a InMemoryCache<CacheModels> {
47 self.0
48 }
49
50 #[allow(clippy::type_complexity)]
53 pub const fn into_cache(self) -> &'a InMemoryCache<CacheModels> {
54 self.0
55 }
56
57 pub fn channels(&self) -> usize {
59 self.0.channels.len()
60 }
61
62 pub fn channel_messages(&self, channel_id: Id<ChannelMarker>) -> Option<usize> {
68 let channel = self.0.channel_messages.get(&channel_id)?;
69
70 Some(channel.len())
71 }
72
73 pub fn channel_voice_states(&self, channel_id: Id<ChannelMarker>) -> Option<usize> {
79 let channel = self.0.voice_state_channels.get(&channel_id)?;
80
81 Some(channel.len())
82 }
83
84 pub fn emojis(&self) -> usize {
86 self.0.emojis.len()
87 }
88
89 pub fn guilds(&self) -> usize {
91 self.0.guilds.len()
92 }
93
94 pub fn guild_channels(&self, guild_id: Id<GuildMarker>) -> Option<usize> {
98 let guild = self.0.guild_channels.get(&guild_id)?;
99
100 Some(guild.len())
101 }
102
103 pub fn guild_emojis(&self, guild_id: Id<GuildMarker>) -> Option<usize> {
107 let guild = self.0.guild_emojis.get(&guild_id)?;
108
109 Some(guild.len())
110 }
111
112 pub fn guild_members(&self, guild_id: Id<GuildMarker>) -> Option<usize> {
116 let guild = self.0.guild_members.get(&guild_id)?;
117
118 Some(guild.len())
119 }
120
121 pub fn guild_presences(&self, guild_id: Id<GuildMarker>) -> Option<usize> {
125 let guild = self.0.guild_presences.get(&guild_id)?;
126
127 Some(guild.len())
128 }
129
130 pub fn guild_roles(&self, guild_id: Id<GuildMarker>) -> Option<usize> {
134 let guild = self.0.guild_roles.get(&guild_id)?;
135
136 Some(guild.len())
137 }
138
139 pub fn guild_voice_states(&self, guild_id: Id<GuildMarker>) -> Option<usize> {
143 let guild = self.0.voice_state_guilds.get(&guild_id)?;
144
145 Some(guild.len())
146 }
147
148 pub fn members(&self) -> usize {
150 self.0.members.len()
151 }
152
153 pub fn presences(&self) -> usize {
155 self.0.presences.len()
156 }
157
158 pub fn roles(&self) -> usize {
160 self.0.roles.len()
161 }
162
163 pub fn unavailable_guilds(&self) -> usize {
165 self.0.unavailable_guilds.len()
166 }
167
168 pub fn users(&self) -> usize {
170 self.0.users.len()
171 }
172
173 pub fn voice_states(&self) -> usize {
175 self.0.voice_states.len()
176 }
177}
178
179#[cfg(test)]
180mod tests {
181 use crate::InMemoryCacheStats;
182 use static_assertions::assert_impl_all;
183 use std::fmt::Debug;
184
185 assert_impl_all!(InMemoryCacheStats<'_>: Clone, Debug, Send, Sync);
186}