[][src]Struct twilight_cache_inmemory::InMemoryCache

pub struct InMemoryCache(_);

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 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(log 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]

Clears the entire state of the Cache. This is equal to creating a new empty Cache.

Trait Implementations

impl Clone for InMemoryCache[src]

impl Debug for InMemoryCache[src]

impl Default for InMemoryCache[src]

Auto Trait Implementations

Blanket Implementations

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

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

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

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

impl<T> Instrument for T[src]

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

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

type Owned = T

The resulting type after obtaining ownership.

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.

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.