Overview
The tif1 cache system provides:- SQLite backend: Persistent storage with WAL mode for concurrency
- In-memory LRU cache: Fast access for frequently used data
- Thread-safe operations: Lock-free reads, protected writes
- Async support: Asynchronous cache operations
- Specialized telemetry cache: Optimized for telemetry data
Functions
get_cache()
Cache: Global cache instance
Cache Class
Constructor
cache_dir(Path, optional): Cache directory path. If None, uses:TIF1_CACHE_DIRenvironment variable- Configuration value from
get_config() - OS-specific default (see Default Cache Directory section)
Attributes
| Attribute | Type | Description |
|---|---|---|
cache_dir | Path | Directory where cache is stored |
db_path | Path | Path to SQLite database file |
read_only | bool | Whether cache is in read-only mode |
Methods
General Cache Operations
get()
key(str): Cache key to lookup
Any | None: Cached data or None if not found
get_async()
key(str): Cache key to lookup
Any | None: Cached data or None if not found
set()
key(str): Cache keydata(Any): Data to cache (must be JSON-serializable)
set_async()
key(str): Cache keydata(Any): Data to cache
Telemetry-Specific Operations
get_telemetry()
year(int): Season yeargp(str): Grand Prix identifiersession(str): Session type (e.g., ‘R’, ‘Q’, ‘FP1’)driver(str): Driver code (e.g., ‘VER’, ‘HAM’)lap(int): Lap number
Any | None: Cached telemetry data or None if not found
get_telemetry_async()
get_telemetry()
Returns:
Any | None: Cached telemetry data or None if not found
get_telemetry_batch()
year(int): Season yeargp(str): Grand Prix identifiersession(str): Session typedriver_laps(list[tuple[str, int]]): List of (driver_code, lap_number) tuples
dict[tuple[str, int], Any]: Dictionary mapping (driver, lap) to telemetry data
get_telemetry_batch_async()
get_telemetry_batch()
Returns:
dict[tuple[str, int], Any]: Dictionary mapping (driver, lap) to telemetry data
set_telemetry()
year(int): Season yeargp(str): Grand Prix identifiersession(str): Session typedriver(str): Driver codelap(int): Lap numberdata(Any): Telemetry data to cache
set_telemetry_async()
set_telemetry()
Cache Management
has_session_data()
year(int): Season yeargp(str): Grand Prix identifiersession(str): Session type
bool: True if session data exists in cache
clear()
close()
Default Cache Directory
The default cache directory is platform-specific:| Platform | Default Location |
|---|---|
| Windows | %LOCALAPPDATA%/Temp/tif1 |
| macOS | ~/Library/Caches/tif1 |
| Linux | ~/.cache/tif1 (or ~/.tif1 if ~/.cache doesn’t exist) |
TIF1_CACHE_DIRenvironment variable- Config file setting:
cache_dir - Constructor parameter:
Cache(cache_dir=...)
Cache Architecture
Two-Tier Caching
-
In-Memory LRU Cache:
- Lock-free reads for maximum concurrency
- Bounded by
memory_cache_max_items(default: 1024) - Separate cache for telemetry:
memory_telemetry_cache_max_items(default: 2048) - <1ms access time
-
SQLite Backend:
- Persistent storage with WAL mode for concurrency
- Two tables:
cache(general) andtelemetry_cache(specialized) - Batched commits every
cache_commit_intervalwrites (default: 25) - Thread-safe with fine-grained locking
Thread Safety
- Lock-free reads: Memory cache reads use no locks (GIL-protected in CPython)
- Separate locks: Different locks for memory cache and SQLite operations
- Minimal lock duration: Memory updates (<1ms) separate from SQLite writes
Performance Optimizations
- WAL (Write-Ahead Logging) mode for better SQLite concurrency
- Batched commits to reduce fsync overhead
- Memory cache updated before SQLite to minimize lock time
- Specialized batch operations for telemetry
Configuration
Cache behavior is controlled by configuration options:| Option | Default | Description |
|---|---|---|
cache_dir | Platform-specific | Cache directory location |
enable_cache | True | Enable/disable caching |
cache_commit_interval | 25 | Writes before SQLite commit |
sqlite_timeout | 30.0 | SQLite connection timeout (seconds) |
memory_cache_max_items | 1024 | Max items in memory cache |
memory_telemetry_cache_max_items | 2048 | Max telemetry items in memory |
Examples
Basic Usage
Async Operations
Telemetry Caching
Batch Telemetry Lookup
Check Session Data
Custom Cache Location
Clear Cache
Source Code
For the complete implementation, seecache.py:42-530 in the source code.