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]

Implemented methods and types for the cache.

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]

Create a new builder to configure and construct an in-memory cache.

pub fn config(&self) -> Config[src]

Returns a copy of the config cache.

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]

Update the cache with an event from the gateway.

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]

Gets the current user.

This is an O(1) operation.

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]

Gets a group by ID.

This is an O(1) operation.

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]

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]

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]

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]

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]

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]

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]

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]

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]

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]

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]

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]

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]

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.

pub fn clear(&self)[src]

Clear the state of the Cache.

This is equal to creating a new empty cache.

Trait Implementations

impl Clone for 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]

Performs copy-assignment from source. Read more

impl Debug for InMemoryCache[src]

fn fmt(&self, f: &mut Formatter<'_>) -> Result[src]

Formats the value using the given formatter. Read more

impl Default for InMemoryCache[src]

fn default() -> InMemoryCache[src]

Returns the “default value” for a type. Read more

Auto Trait Implementations

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

pub fn type_id(&self) -> TypeId[src]

Gets the TypeId of self. Read more

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

pub fn borrow(&self) -> &T[src]

Immutably borrows from an owned value. Read more

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

pub fn borrow_mut(&mut self) -> &mut T[src]

Mutably borrows from an owned value. Read more

impl<T> From<T> for T[src]

pub fn from(t: T) -> T[src]

Performs the conversion.

impl<T> Instrument for T[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]

Instruments this type with the current Span, returning an Instrumented wrapper. Read more

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

pub fn into(self) -> U[src]

Performs the conversion.

impl<T> ToOwned for T where
    T: Clone
[src]

type Owned = T

The resulting type after obtaining ownership.

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]

🔬 This is a nightly-only experimental API. (toowned_clone_into)

recently added

Uses borrowed data to replace owned data, usually by cloning. Read more

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

pub fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>[src]

Performs the conversion.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.

pub fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>[src]

Performs the conversion.