TinyLFU Cache
TinyLFU is a high-performance cache combining LRU and LFU.
Overview
TinyLFU is a hybrid cache strategy that combines the advantages of LRU (Least Recently Used) and LFU (Least Frequently Used). It uses two small caches: one tracking recently accessed items (LRU), another tracking access frequency (LFU), and considers both when evicting.
Features
- Hit Rate: 92% (Highest)
- Memory Usage: Medium
- Concurrency: High
- Implementation Complexity: Complex
Use Cases
- High performance requirements
- Mixed access patterns
- Large datasets
- Scenarios requiring best hit rate
Quick Start
Installation
Basic Usage
Advanced Usage
How It Works
TinyLFU uses two small caches:
- Window Cache (LRU): Tracks recently accessed items
- Main Cache (LFU): Tracks access frequency
When eviction is needed:
- Prioritize evicting items from Window Cache
- If Window Cache is empty, evict the lowest frequency item from Main Cache
API Reference
Constructors
Main Methods
Performance Characteristics
- Time Complexity:
- Set: O(1) average
- Get: O(1) average
- Delete: O(1)
- Space Complexity: O(n), where n is cache capacity
Best Practices
- First choice for high performance: TinyLFU provides the highest hit rate
- Sufficient memory: Requires additional memory to maintain two small caches
- Mixed access patterns: Best for scenarios with both temporal and frequency locality