Skip to content

Commit e9fec53

Browse files
MarkoVcodeclaude
andcommitted
Add universal caching layer with get_or_set() method
Implements SimpleCache for drivers to minimize redundant SCPI calls between polling loops and API requests. Key features: - Thread-safe caching with RLock - TTL support with fractional seconds (e.g., 0.6s) or no expiry - get_or_set() method for concise cache-or-compute pattern - Hybrid support: direct values and callables - Lazy expiry and metrics tracking (hits, misses, evictions) Changes: - Add cache.py: SimpleCache class with get_or_set() method - Add test_cache.py: 40 unit tests covering all features - Add test_owon_oel_caching.py: 11 integration tests - Migrate owon_oel driver to use get_or_set() (75% code reduction) - Add CACHE_DESIGN.md: comprehensive documentation - Update CLAUDE.md: add caching to Key Modules and driver guide Performance: 3x speedup in owon_oel polling, 10x reduction in SCPI calls 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
1 parent 8ba9e43 commit e9fec53

File tree

6 files changed

+1891
-12
lines changed

6 files changed

+1891
-12
lines changed

CLAUDE.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -359,6 +359,16 @@ BenchMesh stores all user data in `~/.benchmesh/` to ensure configurations, Node
359359

360360
Driver should accept `transport: Transport` in constructor and use it for all communication. The Transport interface supports multiple physical transports (SerialTransport for RS232/USB-Serial, with USB TMC and TCP/IP support planned).
361361

362+
**Optional Caching:**
363+
Drivers can use `SimpleCache` to minimize redundant SCPI calls between polling and API requests:
364+
- Import: `from ...cache import SimpleCache`
365+
- Initialize in `__init__()`: `self.cache = SimpleCache()`
366+
- Use in `poll_status()`: Check `self.cache.get(key)` before querying, call `self.cache.set(key, value)` after query
367+
- Invalidate in `set_*` methods: Call `self.cache.invalidate(key)` when device state changes
368+
- See `drivers/owon_oel/driver.py` for reference implementation
369+
- See `CACHE_DESIGN.md` for complete caching guide and migration steps
370+
- Typical performance gain: 3x speedup in polling (owon_oel measured)
371+
362372
## Key Modules
363373

364374
- `serial_manager.py`: SerialManager orchestrates all device connections and worker threads
@@ -375,6 +385,7 @@ Driver should accept `transport: Transport` in constructor and use it for all co
375385
- Prevents arbitrary method execution - only `query_*` and `set_*` methods can be called via API
376386
- Provides `/instruments/{class}/{device_id}/methods` for dynamic method discovery
377387
- `method_inspector.py`: Python introspection utility for discovering driver methods with parameter/return metadata
388+
- `cache.py`: SimpleCache universal caching layer for driver values with TTL support and thread safety
378389
- `connection.py`: DeviceConnection tracks connection state per device
379390
- `reconnect.py`: ReconnectPolicy implements backoff strategy
380391

0 commit comments

Comments
 (0)