Struct twilight_cache_inmemory::InMemoryCache [−][src]
pub struct InMemoryCache(_);
Expand description
A thread-safe, in-memory-process cache of Discord data. It can be cloned and sent to other threads.
This is an implementation of a cache designed to be used by only the current process.
Events will only be processed if they are properly expressed with
Intents
; refer to function-level documentation for more details.
Cloning
The cache internally wraps its data within an Arc. This means that the cache can be cloned and passed around tasks and threads cheaply.
Design and Performance
The defining characteristic of this cache is that returned types (such as a
guild or user) do not use locking for access. The internals of the cache use
a concurrent map for mutability and the returned types themselves are Arcs.
If a user is retrieved from the cache, an Arc<User>
is returned. If a
reference to that user is held but the cache updates the user, the reference
held by you will be outdated, but still exist.
The intended use is that data is held outside the cache for only as long as necessary, where the state of the value at that point time doesn’t need to be up-to-date. If you need to ensure you always have the most up-to-date “version” of a cached resource, then you can re-retrieve it whenever you use it: retrieval operations are extremely cheap.
For example, say you’re deleting some of the guilds of a channel. You’ll probably need the guild to do that, so you retrieve it from the cache. You can then use the guild to update all of the channels, because for most use cases you don’t need the guild to be up-to-date in real time, you only need its state at that point in time or maybe across the lifetime of an operation. If you need the guild to always be up-to-date between operations, then the intent is that you keep getting it from the cache.
Implementations
impl InMemoryCache
[src]
impl InMemoryCache
[src]Implemented methods and types for the cache.
pub fn new() -> Self
[src]
pub fn new() -> Self
[src]Creates a new, empty cache.
Examples
Creating a new InMemoryCache
with a custom configuration, limiting
the message cache to 50 messages per channel:
use twilight_cache_inmemory::InMemoryCache; let cache = InMemoryCache::builder().message_cache_size(50).build();
pub fn builder() -> InMemoryCacheBuilder
[src]
pub fn builder() -> InMemoryCacheBuilder
[src]Create a new builder to configure and construct an in-memory cache.
pub const fn stats(&self) -> InMemoryCacheStats<'_>
[src]
pub const fn stats(&self) -> InMemoryCacheStats<'_>
[src]Create an interface for retrieving statistics about the cache.
Examples
Print the number of guilds in a cache:
use twilight_cache_inmemory::InMemoryCache; let cache = InMemoryCache::new(); // later on... let guilds = cache.stats().guilds(); println!("guild count: {}", guilds);
pub fn update(&self, value: &impl UpdateCache)
[src]
pub fn update(&self, value: &impl UpdateCache)
[src]Update the cache with an event from the gateway.
pub fn guild_channel(&self, channel_id: ChannelId) -> Option<Arc<GuildChannel>>
[src]
pub fn guild_channel(&self, channel_id: ChannelId) -> Option<Arc<GuildChannel>>
[src]Gets a channel by ID.
This is an O(1) operation. This requires the GUILDS
intent.
pub fn current_user(&self) -> Option<Arc<CurrentUser>>
[src]
pub fn current_user(&self) -> Option<Arc<CurrentUser>>
[src]Gets the current user.
This is an O(1) operation.
pub fn emoji(&self, emoji_id: EmojiId) -> Option<Arc<CachedEmoji>>
[src]
pub fn emoji(&self, emoji_id: EmojiId) -> Option<Arc<CachedEmoji>>
[src]Gets an emoji by ID.
This is an O(1) operation. This requires the GUILD_EMOJIS
intent.
pub fn group(&self, channel_id: ChannelId) -> Option<Arc<Group>>
[src]
pub fn group(&self, channel_id: ChannelId) -> Option<Arc<Group>>
[src]Gets a group by ID.
This is an O(1) operation.
pub fn guild(&self, guild_id: GuildId) -> Option<Arc<CachedGuild>>
[src]
pub fn guild(&self, guild_id: GuildId) -> Option<Arc<CachedGuild>>
[src]Gets a guild by ID.
This is an O(1) operation. This requires the GUILDS
intent.
pub fn guild_channels(&self, guild_id: GuildId) -> Option<HashSet<ChannelId>>
[src]
pub fn guild_channels(&self, guild_id: GuildId) -> Option<HashSet<ChannelId>>
[src]Gets the set of channels in a guild.
This is a O(m) operation, where m is the amount of channels in the
guild. This requires the GUILDS
intent.
pub fn guild_emojis(&self, guild_id: GuildId) -> Option<HashSet<EmojiId>>
[src]
pub fn guild_emojis(&self, guild_id: GuildId) -> Option<HashSet<EmojiId>>
[src]Gets the set of emojis in a guild.
This is a O(m) operation, where m is the amount of emojis in the guild.
This requires both the GUILDS
and GUILD_EMOJIS
intents.
pub fn guild_members(&self, guild_id: GuildId) -> Option<HashSet<UserId>>
[src]
pub fn guild_members(&self, guild_id: GuildId) -> Option<HashSet<UserId>>
[src]Gets the set of members in a guild.
This list may be incomplete if not all members have been cached.
This is a O(m) operation, where m is the amount of members in the guild.
This requires the GUILD_MEMBERS
intent.
pub fn guild_presences(&self, guild_id: GuildId) -> Option<HashSet<UserId>>
[src]
pub fn guild_presences(&self, guild_id: GuildId) -> Option<HashSet<UserId>>
[src]Gets the set of presences in a guild.
This list may be incomplete if not all members have been cached.
This is a O(m) operation, where m is the amount of members in the guild.
This requires the GUILD_PRESENCES
intent.
pub fn guild_roles(&self, guild_id: GuildId) -> Option<HashSet<RoleId>>
[src]
pub fn guild_roles(&self, guild_id: GuildId) -> Option<HashSet<RoleId>>
[src]Gets the set of roles in a guild.
This is a O(m) operation, where m is the amount of roles in the guild.
This requires the GUILDS
intent.
pub fn member(
&self,
guild_id: GuildId,
user_id: UserId
) -> Option<Arc<CachedMember>>
[src]
pub fn member(
&self,
guild_id: GuildId,
user_id: UserId
) -> Option<Arc<CachedMember>>
[src]Gets a member by guild ID and user ID.
This is an O(1) operation. This requires the GUILD_MEMBERS
intent.
pub fn message(
&self,
channel_id: ChannelId,
message_id: MessageId
) -> Option<Arc<CachedMessage>>
[src]
pub fn message(
&self,
channel_id: ChannelId,
message_id: MessageId
) -> Option<Arc<CachedMessage>>
[src]Gets a message by channel ID and message ID.
This is an O(n) operation. This requires one or both of the
GUILD_MESSAGES
or DIRECT_MESSAGES
intents.
pub fn presence(
&self,
guild_id: GuildId,
user_id: UserId
) -> Option<Arc<CachedPresence>>
[src]
pub fn presence(
&self,
guild_id: GuildId,
user_id: UserId
) -> Option<Arc<CachedPresence>>
[src]Gets a presence by, optionally, guild ID, and user ID.
This is an O(1) operation. This requires the GUILD_PRESENCES
intent.
pub fn private_channel(
&self,
channel_id: ChannelId
) -> Option<Arc<PrivateChannel>>
[src]
pub fn private_channel(
&self,
channel_id: ChannelId
) -> Option<Arc<PrivateChannel>>
[src]Gets a private channel by ID.
This is an O(1) operation. This requires the DIRECT_MESSAGES
intent.
pub fn role(&self, role_id: RoleId) -> Option<Arc<Role>>
[src]
pub fn role(&self, role_id: RoleId) -> Option<Arc<Role>>
[src]Gets a role by ID.
This is an O(1) operation. This requires the GUILDS
intent.
pub fn user(&self, user_id: UserId) -> Option<Arc<User>>
[src]
pub fn user(&self, user_id: UserId) -> Option<Arc<User>>
[src]Gets a user by ID.
This is an O(1) operation. This requires the GUILD_MEMBERS
intent.
pub fn voice_channel_states(
&self,
channel_id: ChannelId
) -> Option<Vec<Arc<VoiceState>>>
[src]
pub fn voice_channel_states(
&self,
channel_id: ChannelId
) -> Option<Vec<Arc<VoiceState>>>
[src]Gets the voice states within a voice channel.
This requires both the GUILDS
and GUILD_VOICE_STATES
intents.
pub fn voice_state(
&self,
user_id: UserId,
guild_id: GuildId
) -> Option<Arc<VoiceState>>
[src]
pub fn voice_state(
&self,
user_id: UserId,
guild_id: GuildId
) -> Option<Arc<VoiceState>>
[src]Gets a voice state by user ID and Guild ID.
This is an O(1) operation. This requires both the GUILDS
and
GUILD_VOICE_STATES
intents.
Trait Implementations
impl Clone for InMemoryCache
[src]
impl Clone for InMemoryCache
[src]fn clone(&self) -> InMemoryCache
[src]
fn clone(&self) -> InMemoryCache
[src]Returns a copy of the value. Read more
fn clone_from(&mut self, source: &Self)
1.0.0[src]
fn clone_from(&mut self, source: &Self)
1.0.0[src]Performs copy-assignment from source
. Read more
impl Debug for InMemoryCache
[src]
impl Debug for InMemoryCache
[src]impl Default for InMemoryCache
[src]
impl Default for InMemoryCache
[src]fn default() -> InMemoryCache
[src]
fn default() -> InMemoryCache
[src]Returns the “default value” for a type. Read more
Auto Trait Implementations
impl !RefUnwindSafe for InMemoryCache
impl Send for InMemoryCache
impl Sync for InMemoryCache
impl Unpin for InMemoryCache
impl !UnwindSafe for InMemoryCache
Blanket Implementations
impl<T> BorrowMut<T> for T where
T: ?Sized,
[src]
impl<T> BorrowMut<T> for T where
T: ?Sized,
[src]pub fn borrow_mut(&mut self) -> &mut T
[src]
pub fn borrow_mut(&mut self) -> &mut T
[src]Mutably borrows from an owned value. Read more
impl<T> Instrument for T
[src]
impl<T> Instrument for T
[src]fn instrument(self, span: Span) -> Instrumented<Self>
[src]
fn instrument(self, span: Span) -> Instrumented<Self>
[src]Instruments this type with the provided Span
, returning an
Instrumented
wrapper. Read more
fn in_current_span(self) -> Instrumented<Self>
[src]
fn in_current_span(self) -> Instrumented<Self>
[src]impl<T> ToOwned for T where
T: Clone,
[src]
impl<T> ToOwned for T where
T: Clone,
[src]type Owned = T
type Owned = T
The resulting type after obtaining ownership.
pub fn to_owned(&self) -> T
[src]
pub fn to_owned(&self) -> T
[src]Creates owned data from borrowed data, usually by cloning. Read more
pub fn clone_into(&self, target: &mut T)
[src]
pub fn clone_into(&self, target: &mut T)
[src]🔬 This is a nightly-only experimental API. (toowned_clone_into
)
recently added
Uses borrowed data to replace owned data, usually by cloning. Read more