DisGo’s caching system is highly extensible, allowing you to implement custom cache backends, policies, and strategies to fit your application’s needs.Documentation Index
Fetch the complete documentation index at: https://mintlify.com/disgoorg/disgo/llms.txt
Use this file to discover all available pages before exploring further.
Understanding the cache system
DisGo uses a multi-layered cache system:- Cache - Simple key-value store for single entities (guilds, channels, etc.)
- GroupedCache - Two-level cache for entities grouped by ID (members by guild, messages by channel)
- Flags - Control which entity types to cache
- Policies - Control which specific entities to cache
Cache interfaces
Cache interface
The basicCache[T] interface for single entities:
GroupedCache interface
For entities grouped by a parent ID (like members in guilds):Implementing a custom cache
Here’s a complete example implementing a custom grouped cache with an LRU eviction strategy:Using custom cache implementations
Register your custom cache when creating the bot:Cache policies
Policies control which entities to cache based on their properties.Built-in policies
Custom policies
Create custom policies as simple filter functions:Combining policies
Policies can be combined withAnd, Or, or helper functions:
Cache flags
Flags control which entity types to cache:FlagGuilds- Guild objectsFlagChannels- Channel objectsFlagRoles- Role objectsFlagMembers- Member objectsFlagMessages- Message objectsFlagEmojis- Emoji objectsFlagStickers- Sticker objectsFlagVoiceStates- Voice state objectsFlagPresences- Presence objectsFlagStageInstances- Stage instance objectsFlagGuildScheduledEvents- Scheduled event objectsFlagThreadMembers- Thread member objects
Redis cache example
Here’s an example using Redis as a cache backend:Best practices
Choose appropriate cache flags
Choose appropriate cache flags
Only cache what you need. Unnecessary caching wastes memory:
Use policies for large caches
Use policies for large caches
For large bots, use policies to limit member caching:
Make custom caches thread-safe
Make custom caches thread-safe
Always use mutexes in custom cache implementations:
Consider memory limits
Consider memory limits
Implement eviction strategies for large caches to prevent memory issues: