[][src]Struct twilight_cache::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 Cache designed to be used by only the current process. If the cache needs to be used by other processes, consider using twilight-cache-redis or another cache.

Public Immutability

The defining characteristic of this cache is that returned types (such as a guild or user) do not use locking for access. Although the internals of the cache use asynchronous locking for mutability, the returned types themselves are immutable. 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 time doesn't need to be up-to-date.

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. If you need the guild to always be up-to-date between operations, the intent is that you keep getting it from the cache.

Getting something from the cache is cheap and has low contention, so public immutability is preferred over using mutexes, read-write locks, or other smart atomic updating cells. Refer to the crate-level documentation for a list of known first-party and third-party cache implementations.

Caveats

Implementations

impl InMemoryCache[src]

Implemented methods and types for the cache.

Note: This section may appear empty. Please read the implementation in the Cache trait implementation.

pub fn new() -> InMemoryCache[src]

Creates a new, empty cache.

If you need to customize the cache, use the From<InMemoryConfig> implementation.

Examples

Creating a new InMemoryCache with a custom configuration, limiting the message cache to 50 messages per channel:

use twilight_cache_inmemory::{
    config::InMemoryConfig,
    InMemoryCache,
};

let config = InMemoryConfig::builder().message_cache_size(50);
let cache = InMemoryCache::from(config);

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

Returns a copy of the config cache.

pub async fn update<T>(&'_ self, value: &'_ T) -> Result<(), InMemoryCacheError> where
    T: UpdateCache<InMemoryCache, InMemoryCacheError>, 
[src]

pub async fn guild_channel(
    &'_ self,
    channel_id: ChannelId
) -> Result<Option<Arc<GuildChannel>>, InMemoryCacheError>
[src]

Gets a channel by ID.

This is an O(1) operation.

pub async fn current_user(
    &'_ self
) -> Result<Option<Arc<CurrentUser>>, InMemoryCacheError>
[src]

Gets the current user.

This is an O(1) operation.

pub async fn emoji(
    &'_ self,
    emoji_id: EmojiId
) -> Result<Option<Arc<CachedEmoji>>, InMemoryCacheError>
[src]

Gets an emoji by ID.

This is an O(1) operation.

pub async fn group(
    &'_ self,
    channel_id: ChannelId
) -> Result<Option<Arc<Group>>, InMemoryCacheError>
[src]

Gets a group by ID.

This is an O(1) operation.

pub async fn guild(
    &'_ self,
    guild_id: GuildId
) -> Result<Option<Arc<CachedGuild>>, InMemoryCacheError>
[src]

Gets a guild by ID.

This is an O(1) operation.

pub async fn member(
    &'_ self,
    guild_id: GuildId,
    user_id: UserId
) -> Result<Option<Arc<CachedMember>>, InMemoryCacheError>
[src]

Gets a member by guild ID and user ID.

This is an O(1) operation.

pub async fn message(
    &'_ self,
    channel_id: ChannelId,
    message_id: MessageId
) -> Result<Option<Arc<CachedMessage>>, InMemoryCacheError>
[src]

Gets a message by channel ID and message ID.

This is an O(log n) operation.

pub async fn presence(
    &'_ self,
    guild_id: Option<GuildId>,
    user_id: UserId
) -> Result<Option<Arc<CachedPresence>>, InMemoryCacheError>
[src]

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

This is an O(1) operation.

pub async fn private_channel(
    &'_ self,
    channel_id: ChannelId
) -> Result<Option<Arc<PrivateChannel>>, InMemoryCacheError>
[src]

Gets a private channel by ID.

This is an O(1) operation.

pub async fn role(
    &'_ self,
    role_id: RoleId
) -> Result<Option<Arc<Role>>, InMemoryCacheError>
[src]

Gets a role by ID.

This is an O(1) operation.

pub async fn user(
    &'_ self,
    user_id: UserId
) -> Result<Option<Arc<User>>, InMemoryCacheError>
[src]

Gets a user by ID.

This is an O(1) operation.

pub async fn voice_state(
    &'_ self,
    user_id: UserId,
    guild_id: GuildId
) -> Result<Option<Arc<VoiceState>>, InMemoryCacheError>
[src]

Gets a voice state by user ID and Guild ID.

This is an O(1) operation.

pub async fn clear(&'_ self) -> Result<(), InMemoryCacheError>[src]

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

pub async fn cache_current_user(&'_ self, __arg1: CurrentUser)[src]

pub async fn cache_guild_channels(
    &'_ self,
    guild_id: GuildId,
    guild_channels: impl IntoIterator<Item = GuildChannel>
) -> HashSet<ChannelId, RandomState>
[src]

pub async fn cache_guild_channel(
    &'_ self,
    guild_id: GuildId,
    __arg2: GuildChannel
) -> Arc<GuildChannel>
[src]

pub async fn cache_emoji(
    &'_ self,
    guild_id: GuildId,
    emoji: Emoji
) -> Arc<CachedEmoji>
[src]

pub async fn cache_emojis(
    &'_ self,
    guild_id: GuildId,
    emojis: impl IntoIterator<Item = Emoji>
) -> HashSet<EmojiId, RandomState>
[src]

pub async fn cache_group(&'_ self, group: Group) -> Arc<Group>[src]

pub async fn cache_guild(&'_ self, guild: Guild)[src]

pub async fn cache_member(
    &'_ self,
    guild_id: GuildId,
    member: Member
) -> Arc<CachedMember>
[src]

pub async fn cache_members(
    &'_ self,
    guild_id: GuildId,
    members: impl IntoIterator<Item = Member>
) -> HashSet<UserId, RandomState>
[src]

pub async fn cache_presences(
    &'_ self,
    guild_id: Option<GuildId>,
    presences: impl IntoIterator<Item = Presence>
) -> HashSet<UserId, RandomState>
[src]

pub async fn cache_presence(
    &'_ self,
    guild_id: Option<GuildId>,
    presence: Presence
) -> Arc<CachedPresence>
[src]

pub async fn cache_private_channel(
    &'_ self,
    private_channel: PrivateChannel
) -> Arc<PrivateChannel>
[src]

pub async fn cache_roles(
    &'_ self,
    guild_id: GuildId,
    roles: impl IntoIterator<Item = Role>
) -> HashSet<RoleId, RandomState>
[src]

pub async fn cache_role(&'_ self, guild_id: GuildId, role: Role) -> Arc<Role>[src]

pub async fn cache_user(&'_ self, user: User) -> Arc<User>[src]

pub async fn cache_voice_states(
    &'_ self,
    voice_states: impl IntoIterator<Item = VoiceState>
) -> HashSet<UserId, RandomState>
[src]

pub async fn delete_group(&'_ self, channel_id: ChannelId) -> Option<Arc<Group>>[src]

pub async fn unavailable_guild(&'_ self, guild_id: GuildId)[src]

pub async fn delete_guild_channel(
    &'_ self,
    channel_id: ChannelId
) -> Option<Arc<GuildChannel>>
[src]

pub async fn delete_role(&'_ self, role_id: RoleId) -> Option<Arc<Role>>[src]

Trait Implementations

impl<'_> Cache for &'_ InMemoryCache[src]

impl Cache for InMemoryCache[src]

impl Clone for InMemoryCache[src]

impl Debug for InMemoryCache[src]

impl Default for InMemoryCache[src]

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

impl UpdateCache<InMemoryCache, InMemoryCacheError> for TypingStart[src]

impl UpdateCache<InMemoryCache, InMemoryCacheError> for RoleDelete[src]

impl UpdateCache<InMemoryCache, InMemoryCacheError> for GuildDelete[src]

impl UpdateCache<InMemoryCache, InMemoryCacheError> for GuildUpdate[src]

impl UpdateCache<InMemoryCache, InMemoryCacheError> for RoleUpdate[src]

impl UpdateCache<InMemoryCache, InMemoryCacheError> for PresenceUpdate[src]

impl UpdateCache<InMemoryCache, InMemoryCacheError> for WebhooksUpdate[src]

impl UpdateCache<InMemoryCache, InMemoryCacheError> for BanRemove[src]

impl UpdateCache<InMemoryCache, InMemoryCacheError> for GuildEmojisUpdate[src]

impl UpdateCache<InMemoryCache, InMemoryCacheError> for MessageDelete[src]

impl UpdateCache<InMemoryCache, InMemoryCacheError> for MessageUpdate[src]

impl UpdateCache<InMemoryCache, InMemoryCacheError> for UserUpdate[src]

impl UpdateCache<InMemoryCache, InMemoryCacheError> for MemberChunk[src]

impl UpdateCache<InMemoryCache, InMemoryCacheError> for VoiceServerUpdate[src]

impl UpdateCache<InMemoryCache, InMemoryCacheError> for UnavailableGuild[src]

impl UpdateCache<InMemoryCache, InMemoryCacheError> for ReactionAdd[src]

impl UpdateCache<InMemoryCache, InMemoryCacheError> for Ready[src]

impl UpdateCache<InMemoryCache, InMemoryCacheError> for ChannelPinsUpdate[src]

impl UpdateCache<InMemoryCache, InMemoryCacheError> for ReactionRemoveAll[src]

impl UpdateCache<InMemoryCache, InMemoryCacheError> for Event[src]

impl UpdateCache<InMemoryCache, InMemoryCacheError> for ChannelDelete[src]

impl UpdateCache<InMemoryCache, InMemoryCacheError> for MemberAdd[src]

impl UpdateCache<InMemoryCache, InMemoryCacheError> for GuildIntegrationsUpdate[src]

impl UpdateCache<InMemoryCache, InMemoryCacheError> for MessageDeleteBulk[src]

impl UpdateCache<InMemoryCache, InMemoryCacheError> for MemberRemove[src]

impl UpdateCache<InMemoryCache, InMemoryCacheError> for ReactionRemove[src]

impl UpdateCache<InMemoryCache, InMemoryCacheError> for RoleCreate[src]

impl UpdateCache<InMemoryCache, InMemoryCacheError> for GuildCreate[src]

impl UpdateCache<InMemoryCache, InMemoryCacheError> for BanAdd[src]

impl UpdateCache<InMemoryCache, InMemoryCacheError> for ChannelCreate[src]

impl UpdateCache<InMemoryCache, InMemoryCacheError> for MessageCreate[src]

impl UpdateCache<InMemoryCache, InMemoryCacheError> for ChannelUpdate[src]

impl UpdateCache<InMemoryCache, InMemoryCacheError> for MemberUpdate[src]

impl UpdateCache<InMemoryCache, InMemoryCacheError> for VoiceStateUpdate[src]

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> 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, 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.