Cache
Twilight includes an in-process-memory cache. It's responsible for processing events and caching things like guilds, channels, users, and voice states.
Examples
Process new messages that come over a shard into the cache:
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
use std::env;
use twilight_cache_inmemory::DefaultInMemoryCache;
use twilight_gateway::{EventTypeFlags, Intents, Shard, ShardId, StreamExt as _};
let token = env::var("DISCORD_TOKEN")?;
let mut shard = Shard::new(ShardId::ONE, token, Intents::GUILD_MESSAGES);
let cache = DefaultInMemoryCache::new();
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;
};
cache.update(&event);
}
Ok(())
}
Links
source: https://github.com/twilight-rs/twilight/tree/main/twilight-cache-inmemory