#discord #discord-api #twilight


In-process-memory based cache for the Twilight ecosystem

72 releases

new 0.16.0-rc.1 Feb 21, 2024
0.15.4 Sep 10, 2023
0.15.2 Apr 27, 2023
0.15.1 Feb 26, 2023
0.2.3 Nov 29, 2020

#14 in Caching

Download history 359/week @ 2023-11-05 341/week @ 2023-11-12 321/week @ 2023-11-19 412/week @ 2023-11-26 349/week @ 2023-12-03 310/week @ 2023-12-10 435/week @ 2023-12-17 447/week @ 2023-12-24 287/week @ 2023-12-31 384/week @ 2024-01-07 377/week @ 2024-01-14 369/week @ 2024-01-21 400/week @ 2024-01-28 242/week @ 2024-02-04 716/week @ 2024-02-11 1375/week @ 2024-02-18

2,832 downloads per month
Used in 7 crates (6 directly)

ISC license

37K SLoC


codecov badge discord badge github badge license badge rust badge

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


By default no feature is enabled.


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.


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 _};

async fn main() -> Result<(), Box<dyn Error>> {
    // Initialize the tracing subscriber.

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


        // Update the cache with the event.



All first-party crates are licensed under ISC


~66K SLoC