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
- There is no explicit cache invalidation mechanism.
- Even after a channel is renamed, deleted, or has its permissions changed, stale data may still be returned until the TTI/TTL expires.
- Cache keys are shared across the entire process.
- If the caller's
guild_id/channel_idmatch, the cache is reused even for a different guild's Discord link expansion.
- If the caller's
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 12 hours regardless of access frequency.
Cache lookup flow
Message preview
- Look up
GUILD_CHANNEL_CACHE(the individual channel cache)- If it's a hit, return it as-is.
- 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.