#discord #discord-api #twilight

twilight-cache-inmemory

In-process-memory based cache for the Twilight ecosystem

68 releases (15 breaking)

0.15.1 Feb 26, 2023
0.15.0-rc.1 Jan 8, 2023
0.14.0 Nov 14, 2022
0.12.1 Jul 26, 2022
0.2.3 Nov 29, 2020

#7 in Caching

Download history 155/week @ 2022-12-01 262/week @ 2022-12-08 186/week @ 2022-12-15 152/week @ 2022-12-22 196/week @ 2022-12-29 273/week @ 2023-01-05 170/week @ 2023-01-12 254/week @ 2023-01-19 258/week @ 2023-01-26 355/week @ 2023-02-02 396/week @ 2023-02-09 467/week @ 2023-02-16 264/week @ 2023-02-23 225/week @ 2023-03-02 247/week @ 2023-03-09 141/week @ 2023-03-16

972 downloads per month
Used in 6 crates (5 directly)

ISC license

1.5MB
35K SLoC

twilight-cache-inmemory

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

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::InMemoryCache;
use twilight_gateway::{Intents, Shard, ShardId};

#[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 = InMemoryCache::builder().message_cache_size(10).build();

    loop {
        let event = match shard.next_event().await {
            Ok(event) => event,
            Err(source) => {
                tracing::warn!(?source, "error receiving event");

                if source.is_fatal() {
                    break;
                }

                continue;
            }
        };

        // Update the cache with the event.
        cache.update(&event);
    }

    Ok(())
}

License

All first-party crates are licensed under ISC

Dependencies

~2.5–7.5MB
~126K SLoC