Expand description
§twilight-cache-inmemory
twilight-cache-inmemory
is an in-process-memory cache for the
twilight-rs
ecosystem. It’s responsible for processing events and
caching things like guilds, channels, users, and voice states.
§Statistics
Statistics can be an important debugging tool for determining how large a
cache is or determining whether a cache has an expected amount of resources
within it. An interface for retrieving statistics about the amount of a
resource within the cache as a whole or on a guild-level can be retrieved
via InMemoryCache::stats
.
§Features
By default no feature is enabled.
§permission-calculator
The permission-calculator
feature flag will bring in support for the
PermissionCalculator
; an API for calculating permissions through it is
exposed via InMemoryCache::permissions
. Support for calculating the
permissions of a member on a root guild-level and in a guild channel is
included.
Refer to the permission
module for more documentation.
§Examples
Update a cache with events that come in through the gateway:
use std::{env, error::Error};
use twilight_cache_inmemory::DefaultInMemoryCache;
use twilight_gateway::{EventTypeFlags, Intents, Shard, ShardId, StreamExt as _};
#[tokio::main]
async fn main() -> Result<(), Box<dyn Error>> {
// Initialize the tracing subscriber.
tracing_subscriber::fmt::init();
let token = env::var("DISCORD_TOKEN")?;
let mut shard = Shard::new(ShardId::ONE, token, Intents::GUILD_MESSAGES);
// Create a cache, caching up to 10 messages per channel:
let cache = DefaultInMemoryCache::builder().message_cache_size(10).build();
while let Some(item) = shard.next_event(EventTypeFlags::all()).await {
let Ok(event) = item else {
tracing::warn!(source = ?item.unwrap_err(), "error receiving event");
continue;
};
// Update the cache with the event.
cache.update(&event);
}
Ok(())
}
§License
All first-party crates are licensed under ISC
Re-exports§
pub use self::traits::CacheableChannel;
pub use self::traits::CacheableCurrentUser;
pub use self::traits::CacheableEmoji;
pub use self::traits::CacheableGuild;
pub use self::traits::CacheableGuildIntegration;
pub use self::traits::CacheableMember;
pub use self::traits::CacheableMessage;
pub use self::traits::CacheableModels;
pub use self::traits::CacheablePresence;
pub use self::traits::CacheableRole;
pub use self::traits::CacheableStageInstance;
pub use self::traits::CacheableSticker;
pub use self::traits::CacheableUser;
pub use self::traits::CacheableVoiceState;
pub use self::permission::InMemoryCachePermissions;
permission-calculator
Modules§
- iter
- Iterators over the various resources stored in the cache.
- model
- Models built for utilizing efficient caching.
- permission
permission-calculator
- Calculate the permissions for members in on a guild- or channel-level with information from the cache.
- traits
- Traits for implementing a
InMemoryCache
with custom structs.
Structs§
- Config
- Configuration for an
InMemoryCache
. - Default
Cache Models - Guild
Resource - Resource associated with a guild.
- InMemory
Cache - An in-memory cache of Discord data.
- InMemory
Cache Builder - Builder to configure and construct an
InMemoryCache
. - InMemory
Cache Stats - Retrieve statistics about the number of entities of each resource in the cache.
- Reference
- Immutable reference to a resource in the cache.
- Resource
Type - A set of bitflags which can be used to specify what resource to process into the cache.
- Voice
Channel States - Iterator over a voice channel’s list of voice states.
Traits§
- Update
Cache - Implemented for dispatch events.
Type Aliases§
- Default
InMemory Cache - The default implementation of
InMemoryCache
. This is a type alias to the trait type defaults to allow the compiler to properly infer the generics.