Overview
Discord Player provides a built-in query caching system that stores search results to improve performance and reduce redundant API calls. The cache automatically expires entries after a configurable interval and can be customized with your own cache provider.Default Query Cache
The defaultQueryCache implementation stores track URLs in memory and automatically cleans up expired entries.
Configuration
Cache Options
The query cache accepts the following options:Interval in milliseconds to check for expired cache entries (default: 5 hours)
Example Usage
How It Works
The caching system works automatically:- When you search for a track, the result is cached using the track URL as the key
- Subsequent searches for the same URL return the cached result
- Cache entries expire after 5 hours by default
- A cleanup timer runs at the configured interval to remove expired entries
Custom Cache Provider
You can implement your own cache provider by implementing theQueryCacheProvider interface:
Cache Management
Manual Cleanup
You can manually trigger cache cleanup:Bypassing Cache
You can bypass the cache for specific searches:Cache Entry Structure
Each cache entry is wrapped in aDiscordPlayerQueryResultCache object:
The cached data (usually a Track)
Timestamp (in milliseconds) when this entry expires
Performance Benefits
Reduced API Calls
Cache frequently requested tracks to avoid hitting rate limits
Faster Response
Return cached results instantly without network requests
Lower Bandwidth
Save bandwidth by reusing previously fetched metadata
Custom Storage
Use Redis, database, or any storage backend you prefer
Best Practices
Set appropriate expiry times
Set appropriate expiry times
Balance between cache freshness and performance. 5 hours is a good default, but you may want shorter times for frequently updated content.
Monitor cache size
Monitor cache size
For high-traffic bots, consider implementing cache size limits to prevent memory issues.
Use persistent storage for large bots
Use persistent storage for large bots
If your bot handles many unique queries, implement a Redis or database-backed cache provider.
Clear cache on extractor updates
Clear cache on extractor updates
When updating extractors, clear the cache to ensure users get fresh metadata.
Related
Search API
Learn about search options and query types
Player Configuration
Configure other player options