Overview
Gogram includes a built-in caching system that stores peer information (users, channels, chats) to avoid repeatedly fetching the same data from Telegram servers. This improves performance and reduces API calls.Why caching is necessary: Telegram sometimes assumes you already have the access hash for a peer and sends “min” constructors instead, which lack the access hash needed to interact with that peer. The cache stores full peer information to prevent this issue.
What Gets Cached?
The cache stores:- Users - User IDs and access hashes
- Channels - Channel IDs and access hashes
- Chats - Basic chat information
- Usernames - Username to ID mappings
Cache Modes
- File-Based Cache (Default) - Persists cache to disk
- Memory-Only Cache - Cache cleared on exit
- Disabled Cache - No caching (cached for 20 seconds then cleared, for lib functionality)
Custom Cache Configuration
Using NewCache
Create a cache with custom configuration:Cache Configuration Options
Maximum number of users/channels to keep in cache.
0 = unlimited.When limit is reached, oldest entries are removed.Cache logging level:
DebugLevel, InfoLevel, WarnLevel, ErrorLevel.Enable colored cache logs.
Custom logger implementation.
See Logger Interface for details.
Use memory-only cache (don’t persist to file).
Disable the cache entirely.
Cache Methods
Export/Import Cache
Export and import cache data as JSON:Clear Cache
Clear all cached data:Check if ID is Cached
Retrieving Cached Data
Get User
Get Channel
Get Chat
Get Peer (Auto-Detect)
Automatically detects whether ID is a user, channel, or chat:Input Peers
Get InputPeer objects for API calls:Channel ID Formats
Gogram handles both channel ID formats:Cache Behavior
Automatic Updates
The cache automatically updates when:- Receiving messages
- Fetching users/channels
- Getting chat members
- Any API call that returns user/channel data
Min Users/Channels
Telegram sometimes returns “min” entities with limited information. The cache:- Keeps full data if already cached
- Only stores min data if no full data exists
- Doesn’t trigger file writes for min entities
Debounced Writes
Cache writes are debounced to avoid excessive file I/O:- Batches updates within 1 second
- Minimum 2 seconds between writes
- Asynchronous write operations
Advanced Usage
Database Integration
Store cache in your database instead of files:Size-Limited Cache
For bots with millions of users, limit cache size:When the cache reaches
MaxSize, oldest entries are automatically removed.Disable for Stateless Bots
For serverless/stateless bots that don’t need persistence:Cache File Structure
The cache file (cache.dat) stores:
Best Practices
Use File Cache
Enable file-based cache for persistent bots to avoid re-fetching data.
Set Size Limits
Use
MaxSize for bots handling millions of users to prevent memory issues.Export for Backup
Periodically export cache as JSON for backup or migration.
Memory Cache for Workers
Use memory-only cache for short-lived worker processes.
Monitor Cache Size
Enable debug logging to track cache growth and performance.
Database Integration
For distributed systems, store cache in a shared database.
Troubleshooting
”Missing from cache” Errors
Telegram only sends access hashes when you’ve interacted with a peer. If you get missing cache errors:Cache Not Persisting
Ensure:MemoryCacheisfalse(default)DisableCacheisfalse(default)- File has write permissions
- Program exits gracefully (cache is written on updates)