Caching v0.3.0~
babyrite uses the concurrent caching library moka to cache channel information retrieved from the Discord API, cutting down on the need to refetch it.
About the caching system's technical constraints
- When Discord reports that a channel was created, updated or deleted (threads included), the cache for that channel and its guild is discarded.
- Making a channel private therefore takes effect immediately.
- An event missed across a gateway reconnect is the exception; the TTL resolves that within the hour.
- Cache keys are shared across the entire process.
GUILD_CHANNEL_CACHEis keyed bychannel_idalone, so a channel cached for another guild can still be a hit.- Every lookup therefore verifies that the channel really belongs to the requested guild. If it does not, the channel is not returned and the lookup fails.
Supported features
GitHub permalinks are not covered — babyrite fetches directly from raw.githubusercontent.com every time.
Cache layout
babyrite builds the following cache layout in the machine's memory.
| Cache | Key → Value | Purpose |
|---|---|---|
GUILD_CHANNEL_LIST_CACHE | GuildId → HashMap<ChannelId, GuildChannel> | Per-guild channel list |
GUILD_CHANNEL_CACHE | ChannelId → GuildChannel | Individual channels |
Both operate with the following shared settings:
- Up to 500 entries
- The cache is organized on a Least Recently Used (LRU) basis.
- Entries beyond the size limit are evicted starting with the least recently used data.
- TTI (Time To Idle):
- Data that hasn't been accessed for 1 hour is automatically removed.
- TTL (Time To Live):
- Data expires after 1 hour regardless of access frequency.
- Cached channel data feeds permission decisions, so the TTL is not just a memory bound — it also bounds how long a missed event can stay in effect.
Cache lookup flow
Message preview
- Look up
GUILD_CHANNEL_CACHE(the individual channel cache)- On a hit, return the channel if it belongs to the requested guild.
- If it belongs to another guild, fail. (Channel IDs are unique across Discord, so this means the request itself was wrong.)
- If not found, look up
GUILD_CHANNEL_LIST_CACHE(the channel list cache)- If it's a hit, look for the target channel in that map.
- If it's a miss, fetch from the Discord API and store the result in the cache.
- If the target channel isn't found in the channel list, search active threads (threads aren't included in the channel list).
- The channel that's found is ultimately written into the cache as well.