Driver Cache Design Pattern¶
🎯 Overview¶
Many hardware drivers should not access hardware on every read.
Instead, a cache mechanism is introduced to improve performance and reduce unnecessary bus traffic.
🧠 Problem¶
Without cache:
Issues:
- High latency
- Excessive bus usage (I2C/SPI)
- Poor scalability with multiple users
💡 Solution: Cache + Update Policy¶
🧩 Core Components¶
1. Cached Data¶
2. Timestamp¶
3. Update Interval¶
4. Valid Flag¶
⚙️ Update Logic¶
🔄 Data Flow¶
🔒 Thread Safety¶
- Protect cache with mutex
- Ensure atomic update + read
🧠 Design Trade-offs¶
| Strategy | Pros | Cons |
|---|---|---|
| Always read | Fresh data | Slow |
| Cache | Fast | Slightly stale |
🟡 Modes of Operation¶
1. No Cache¶
2. Lazy Cache (On-Demand)¶
✔ Most common approach
3. Background Polling¶
✔ Best performance
❌ More complex
⚠️ Important Distinction¶
Hardware Update vs Driver Cache¶
| Concept | Meaning |
|---|---|
| sensor update period | hardware refresh rate |
| driver cache interval | software read policy |
These must be treated separately.
🧠 Key Insight¶
Driver design is not only about accessing hardware.
It is also about:
- When to access hardware
- How often to access hardware
- How to balance performance vs accuracy
🚀 Application¶
This pattern is widely used in:
- hwmon drivers
- battery drivers
- thermal sensors
- ADC subsystems
- network statistics