Struct twilight_cache_inmemory::InMemoryCache [−][src]
pub struct InMemoryCache { /* fields omitted */ }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.
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);This is supported on crate feature permission-calculator only.
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::{ChannelId, UserId};
let resource_types = ResourceType::CHANNEL
| ResourceType::MEMBER
| ResourceType::ROLE;
let cache = InMemoryCache::builder()
.resource_types(resource_types)
.build();
let channel_id = ChannelId::new(4).expect("non zero");
let user_id = UserId::new(5).expect("non zero");
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 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.
pub fn emoji(
&self,
emoji_id: EmojiId
) -> Option<Reference<'_, EmojiId, GuildResource<CachedEmoji>>>
pub fn emoji(
&self,
emoji_id: EmojiId
) -> Option<Reference<'_, EmojiId, GuildResource<CachedEmoji>>>
Gets an emoji by ID.
This requires the GUILD_EMOJIS intent.
Gets a group by ID.
Gets a guild by ID.
This requires the GUILDS intent.
pub fn guild_channel(
&self,
channel_id: ChannelId
) -> Option<Reference<'_, ChannelId, GuildResource<GuildChannel>>>
pub fn guild_channel(
&self,
channel_id: ChannelId
) -> Option<Reference<'_, ChannelId, GuildResource<GuildChannel>>>
Gets a channel 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 intents.
pub fn guild_integrations(
&self,
guild_id: GuildId
) -> Option<Reference<'_, GuildId, HashSet<IntegrationId>>>
pub fn guild_integrations(
&self,
guild_id: GuildId
) -> Option<Reference<'_, GuildId, HashSet<IntegrationId>>>
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.
pub fn integration(
&self,
guild_id: GuildId,
integration_id: IntegrationId
) -> Option<Reference<'_, (GuildId, IntegrationId), GuildResource<GuildIntegration>>>
pub fn integration(
&self,
guild_id: GuildId,
integration_id: IntegrationId
) -> Option<Reference<'_, (GuildId, IntegrationId), GuildResource<GuildIntegration>>>
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.
pub fn private_channel(
&self,
channel_id: ChannelId
) -> Option<Reference<'_, ChannelId, PrivateChannel>>
pub fn private_channel(
&self,
channel_id: ChannelId
) -> Option<Reference<'_, ChannelId, PrivateChannel>>
Gets a private channel by ID.
This requires the DIRECT_MESSAGES intent.
Gets a role by ID.
This requires the GUILDS intent.
pub fn stage_instance(
&self,
stage_id: StageId
) -> Option<Reference<'_, StageId, GuildResource<StageInstance>>>
pub fn stage_instance(
&self,
stage_id: StageId
) -> Option<Reference<'_, StageId, GuildResource<StageInstance>>>
Gets a stage instance by ID.
This requires the GUILDS intent.
pub fn sticker(
&self,
sticker_id: StickerId
) -> Option<Reference<'_, StickerId, GuildResource<CachedSticker>>>
pub fn sticker(
&self,
sticker_id: StickerId
) -> Option<Reference<'_, StickerId, GuildResource<CachedSticker>>>
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
Returns the “default value” for a type. Read more
