Overview
The caching system stores API responses in memory to provide instant access when users revisit tabs or search terms. It uses a simple Map-based cache with automatic eviction to prevent unbounded memory growth.Cache Structure
The cache is initialized as a JavaScript Map at the module level:content.js:12
en:Albert Einstein:summaryar:الفيزياء:defineen:JavaScript:facts
The cache is cleared when the user changes their preferred language setting, ensuring language-specific results are always accurate.
Cache Configuration
The cache has a fixed size limit:content.js:22
- Balances memory usage with performance
- Covers typical browsing session needs
- Each entry contains JSON data (usually 1-5KB)
- Total memory footprint: ~30-150KB
Cache Key Generation
ThegetCacheKey() function creates consistent keys:
content.js:171-173
currentLanguage- Ensures language-specific contentterm- The cleaned search termtab- Tab ID to separate different content types
The search term should be cleaned with
cleanSearchTerm() before generating cache keys to ensure consistent matching.Reading from Cache
ThegetFromCache() function retrieves cached data:
content.js:175-177
Writing to Cache
ThesetCache() function stores data with automatic eviction:
content.js:179-185
- FIFO (First In, First Out) - Oldest entry is removed first
- Trigger: When cache reaches
CACHE_SIZElimit - Deletion: Removes one entry to make room for new data
The FIFO strategy is simple and predictable. For most users, 30 entries is sufficient to cache recent searches without sophisticated LRU (Least Recently Used) logic.
Cache Integration
Every tab that fetches data follows this pattern:Cache Invalidation
The cache is cleared in two scenarios:1. Language Change
content.js:66-69
2. Manual Re-translation
content.js:1102-1106
The Summary, Define, and Facts tabs never invalidate their cache unless the language changes, as Wikipedia/Wikidata content is relatively stable.
Cache Performance
Benefits:- Instant tab switching - No loading spinners for cached tabs
- Reduced API calls - Fewer requests to Wikipedia/Wiktionary/OpenAI
- Lower latency - No network round-trip for cached data
- Better UX - Smooth, responsive interface
- Memory usage - ~30-150KB per browsing session
- Stale data - Cache persists until page refresh or language change
- No persistence - Cache is lost when page reloads
Cache Lifecycle
Debugging Cache Issues
To inspect the cache during development:The cache is scoped to the content script instance, so each browser tab maintains its own independent cache.