FastF1 Compatibility Layer
Thefastf1_compat module provides a compatibility layer for applications migrating from fastf1 to tif1. This module maintains API compatibility while using tif1 performance optimizations, including async data fetching, HTTP/2 multiplexing, and caching strategies.
For new projects: Use the native tif1 API directly for the best experience. This compatibility layer is specifically designed to ease migration from existing fastf1 codebases with minimal code changes.
Performance benefit: While maintaining API compatibility, tif1 delivers significant performance improvements:
- Faster cold starts (first-time data loads) through lazy, file-level fetching
- Fast warm starts (cached data) through the memory and SQLite cache tiers
- Async-first architecture for parallel data fetching
- HTTP/2 multiplexing for efficient network usage
- SQLite-backed caching with in-memory LRU layer
Cache Management
TheCache class provides fastf1-compatible cache management with improved performance through a multi-layer caching strategy. tif1 uses a caching system that combines in-memory LRU caches with SQLite persistence. This structure gives faster data access than the fastf1 HTTP cache approach.
Architecture Overview
tif1’s caching system consists of three layers:- In-Memory LRU Cache: Lock-free reads for frequently accessed data (configurable size, default 1024 items)
- Telemetry-Specific Cache: Dedicated in-memory cache for telemetry data (default 2048 items)
- SQLite Persistence: WAL-mode SQLite database for durable storage with optimized concurrency
Cache.enable_cache
Parameters
Returns
Cache: The configured global tif1 cache instance
Behavior Details
Whenenable_cache() is called:
- Directory Creation: Creates the cache directory if it does not exist, with restricted permissions (0o700 on Unix-like systems)
- SQLite Initialization: Sets up the SQLite database with WAL (Write-Ahead Logging) mode for better concurrency
- Configuration Update: Updates the global tif1 configuration to enable caching
- Instance Reset: Closes any existing cache instance and creates a fresh one
- Optional Clear: If
force_renew=True, clears all cached data
Cache Directory Resolution
The cache directory is resolved in the following order of precedence:- Explicit
cache_dirparameter TIF1_CACHE_DIRenvironment variable- Configuration file setting (
~/.tif1rc) - Platform-specific default:
- Windows:
%LOCALAPPDATA%/Temp/tif1 - macOS:
~/Library/Caches/tif1 - Linux/other POSIX:
~/.cache/tif1(or~/.tif1if~/.cachedoes not exist)
- Windows:
Examples
Basic usage with default location:Migration from fastf1
The API is identical, so migration needs few changes:Performance Considerations
- First call overhead: The first
enable_cache()call creates the SQLite database and initializes tables (~10-50ms) - Subsequent calls: Reusing an existing cache directory is nearly instant
- Memory usage: In-memory caches use approximately 50-100 bytes per cached item
- Disk usage: SQLite database grows with cached data; typical session data is 1-5 MB
Thread Safety
enable_cache() is thread-safe and can be called from multiple threads. However, it is recommended to call it once during application initialization to avoid unnecessary overhead.
Cache.clear_cache
Parameters
Behavior Details
The clearing process works as follows:- Active Cache Detection: If
cache_dirisNone, uses the now active cache instance - Directory Validation: Verifies the cache directory exists and is a valid directory
- SQLite Clearing: Executes
DELETEstatements on all cache tables - Memory Cache Flush: Clears in-memory LRU caches
- Legacy Cleanup (if
deep=True): Removes FastF1 HTTP cache files for users migrating from fastf1
Cache Tables Cleared
cachetable: General session data, lap times, race control messages, weather datatelemetry_cachetable: Telemetry data indexed by (year, GP, session, driver, lap)- In-memory caches: Both general and telemetry-specific LRU caches
Examples
Clear the active cache:Migration from fastf1
The basic API is identical:Error Handling
NotADirectoryError: Raised if the specifiedcache_direxists but is not a directoryOSError: May be raised if file permissions prevent deletion (logged as warning for legacy files)
Performance Impact
- Clearing time: Typically 10-100ms depending on cache size
- Disk I/O: Minimal - SQLite
DELETEoperations are fast with WAL mode - Memory: In-memory caches are cleared instantly
When to Clear Cache
Consider clearing the cache in these scenarios:- Data corruption: When cached data is suspect or invalid
- Schema changes: After updating tif1 to a version with data format changes
- Disk space: When cache directory grows too large
- Testing: To ensure tests fetch fresh data
- Season updates: At the start of a new F1 season to remove old data
Thread Safety
clear_cache() is thread-safe but should not be called concurrently with data fetching operations, as this may lead to inconsistent cache states.
Logging Functions
set_log_level
level: Python logging level (for example,logging.DEBUG,logging.INFO,logging.WARNING)
Complete migration example
Here’s a complete example showing how to migrate a fastf1 script to tif1: Original fastf1 code:API Differences
While tif1 maintains API compatibility, there are some differences to be aware of:Supported Features
Ignored Parameters
Some fastf1 parameters are ignored in tif1 because they are handled automatically:Cache.enable_cache(ignore_version): tif1 handles versioning automaticallyCache.enable_cache(force_renew): UseCache.clear_cache()insteadCache.enable_cache(use_requests_cache): tif1 uses SQLite cache
Performance Improvements
tif1 provides significant performance improvements while maintaining API compatibility:- Async loading:
session.laps_async()for parallel data fetching - Ultra-cold start: Minimal latency for first-time loads
- HTTP/2 multiplexing: Faster parallel requests
- Optimized caching: SQLite-backed multi-layer cache
Compatibility Checklist
When migrating from fastf1 to tif1:- Replace
import fastf1withimport tif1 - Update
Cache.enable_cache()calls (API is identical) - Update
set_log_level()calls (API is identical) - Test that
get_session()works with the year/GP/session combinations in use - Verify DataFrame column names match (tif1 uses fastf1 naming conventions)
- Consider using async methods (
laps_async(),get_fastest_laps_tels_async()) for better performance - Update any custom caching logic (tif1 handles caching automatically)
Known Limitations
The following fastf1 features are not yet supported in tif1:- Ergast API integration: tif1 uses TracingInsights CDN exclusively
- Live timing: tif1 focuses on historical data
- Custom data sources: tif1 uses a fixed CDN structure
Getting Help
When issues occur during migration from fastf1:- Check the Migration Guide for detailed instructions
- Review the FAQ for common questions
- Open an issue on GitHub