pub struct InMemoryCache { /* private fields */ }
Expand description

An in-memory cache of Discord data.

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.

Using the cache in multiple tasks

To use a cache instance in multiple tasks, consider wrapping it in an std::sync::Arc or std::rc::Rc.

Caution required

The cache uses a concurrent map for mutability of cached resources. Return types of methods are immutable references to those resources. If a resource is retrieved from the cache then care must be taken to only hold it for as long as necessary. If the cache needs to mutate a resource to update it and a reference to it is being held then calls to InMemoryCache::update may be blocked.

In order to avoid blocking of cache updates care must be taken to hold them for as little as possible. For example, consider dropping references during long-running tasks such as HTTP requests. Processing HTTP requests takes milliseconds to seconds; retrieving a new reference to a resource is on the scale of nanoseconds. If only a couple of small fields are necessary from a reference consider copying or cloning them.

Implementations

Implemented methods and types for the cache.

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();

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

Clear the state of the Cache.

This is equal to creating a new empty cache.

Returns a copy of the config cache.

Create an interface for iterating over the various resources in the cache.

Via the iterator interface many resource types can be iterated over including, but not limited to, emojis, guilds, presences, and users.

Examples

Iterate over every guild in the cache and print their IDs and names:

use twilight_cache_inmemory::InMemoryCache;

let cache = InMemoryCache::new();

// later in the application...
for guild in cache.iter().guilds() {
    println!("{}: {}", guild.id(), guild.name());
}

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);
Available on crate feature permission-calculator only.

Create an interface for retrieving the permissions of a member in a guild or channel.

ResourceTypes must be configured for the permission interface to properly work; refer to the permission module-level documentation for more information.

Examples

Calculate the permissions of a member in a guild channel:

use twilight_cache_inmemory::{InMemoryCache, ResourceType};
use twilight_model::id::Id;

let resource_types = ResourceType::CHANNEL
    | ResourceType::MEMBER
    | ResourceType::ROLE;

let cache = InMemoryCache::builder()
    .resource_types(resource_types)
    .build();

let channel_id = Id::new(4);
let user_id = Id::new(5);

let permissions = cache.permissions().in_channel(user_id, channel_id)?;
println!("member has these permissions: {:?}", permissions);

Update the cache with an event from the gateway.

Gets the current user.

Gets a channel by ID.

Gets the set of messages in a channel.

This requires the DIRECT_MESSAGES or GUILD_MESSAGES intents.

Returns None if the channel is not cached.

Examples

Refer to ChannelMessages.

Gets an emoji by ID.

This requires the GUILD_EMOJIS_AND_STICKERS intent.

Gets a guild by ID.

This requires the GUILDS intent.

Gets the set of channels in a guild.

This requires the GUILDS intent.

Gets the set of emojis in a guild.

This requires both the GUILDS and GUILD_EMOJIS_AND_STICKERS intents.

Gets the set of integrations in a guild.

This requires the GUILD_INTEGRATIONS intent. The ResourceType::INTEGRATION resource type must be enabled.

Gets the set of members in a guild.

This list may be incomplete if not all members have been cached.

This requires the GUILD_MEMBERS intent.

Gets the set of presences in a guild.

This list may be incomplete if not all members have been cached.

This requires the GUILD_PRESENCES intent.

Gets the set of roles in a guild.

This requires the GUILDS intent.

Gets the set of stage instances in a guild.

This requires the GUILDS intent.

Gets the set of the stickers in a guild.

This is an O(m) operation, where m is the amount of stickers in the guild. This requires the GUILDS and GUILD_EMOJIS_AND_STICKERS intents and the STICKER resource type.

Gets the set of voice states in a guild.

This requires both the GUILDS and GUILD_VOICE_STATES intents.

Gets an integration by guild ID and integration ID.

This requires the GUILD_INTEGRATIONS intent. The ResourceType::INTEGRATION resource type must be enabled.

Gets a member by guild ID and user ID.

This requires the GUILD_MEMBERS intent.

Gets a message by ID.

This requires one or both of the GUILD_MESSAGES or DIRECT_MESSAGES intents.

Gets a presence by, optionally, guild ID, and user ID.

This requires the GUILD_PRESENCES intent.

Gets a role by ID.

This requires the GUILDS intent.

Gets a stage instance by ID.

This requires the GUILDS intent.

Gets a sticker by ID.

This is the O(1) operation. This requires the GUILDS and the GUILD_EMOJIS_AND_STICKERS intents and the STICKER resource type.

Gets a user by ID.

This requires the GUILD_MEMBERS intent.

Gets the voice states within a voice channel.

This requires both the GUILDS and GUILD_VOICE_STATES intents.

Gets a voice state by user ID and Guild ID.

This requires both the GUILDS and GUILD_VOICE_STATES intents.

Gets the highest role of a member.

This requires both the GUILDS and GUILD_MEMBERS intents.

Trait Implementations

Formats the value using the given formatter. Read more

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

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.