Overview
The caching system provides:- LRU Cache: Least Recently Used eviction strategy
- CacheManager: Unified cache management
AlgorithmCacheConfig
Note: There are two cache config classes in the library:Configure caching behavior for different lookup types:
myspellchecker.core.config.AlgorithmCacheConfig(Pydantic model) — used withSpellCheckerConfigfor algorithm-level cache sizingmyspellchecker.utils.cache.CacheConfig(dataclass) — used for low-level cache instance configuration withmaxsizeandname
Configuration Options
| Option | Type | Default | Description |
|---|---|---|---|
syllable_cache_size | int | 4096 | LRU cache size for syllable lookups |
word_cache_size | int | 8192 | LRU cache size for word lookups |
frequency_cache_size | int | 8192 | LRU cache size for frequency lookups |
bigram_cache_size | int | 16384 | LRU cache size for bigram lookups |
trigram_cache_size | int | 16384 | LRU cache size for trigram lookups |
Pre-defined Configurations
LRU Cache
Least Recently Used cache with fixed size:LRU Eviction
When the cache is full, the least recently accessed item is evicted:CacheManager
Unified cache management for multiple cache instances:Integration with SpellChecker
Caching is automatically configured viaSpellCheckerConfig:
What’s Cached
| Component | Cache Type | Default Size | Purpose |
|---|---|---|---|
| Syllable validation | LRU | 4096 | Syllable validity |
| Word lookup | LRU | 8192 | Dictionary results |
| Frequency lookup | LRU | 8192 | Frequency scores |
| Bigram probabilities | LRU | 16384 | Bigram scores |
| Trigram probabilities | LRU | 16384 | Trigram scores |
| Edit distance | LRU | 4096 | Damerau-Levenshtein |
| POS tags | LRU | 1024 | Tag sequences |
| Stemmer | LRU | 1024 | Root extraction |
Performance Tips
1. Size Appropriately
2. Monitor Hit Rates
4. Clear on Data Changes
Thread Safety
All cache implementations are thread-safe:Minimizing Cache
For debugging or testing with minimal caching:Best Practices
- Start with defaults: The default configuration works well for most cases
- Monitor hit rates: Use
SpellChecker.cache_stats()to identify underperforming caches - Size for working set: Cache should fit typical vocabulary in use
- Clear strategically: Clear cache when dictionary data changes
See Also
- Configuration Guide - General configuration
- Performance Tuning - Optimization strategies
- Connection Pooling - Database connections