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.
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.
Update a cache with events that come in through the gateway:
```rust,norun use std::{env, error::Error}; use twilightcacheinmemory::InMemoryCache; use twilightgateway::{Intents, Shard, ShardId};
async fn main() -> Result<(), Box
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(())
} ```
All first-party crates are licensed under ISC