Skip to main content

FastF1 Compatibility Layer

The fastf1_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

The Cache 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:
  1. In-Memory LRU Cache: Lock-free reads for frequently accessed data (configurable size, default 1024 items)
  2. Telemetry-Specific Cache: Dedicated in-memory cache for telemetry data (default 2048 items)
  3. SQLite Persistence: WAL-mode SQLite database for durable storage with optimized concurrency
This multi-layer approach ensures that hot data is served from memory while maintaining persistence across sessions.

Cache.enable_cache

Enable caching with fastf1-compatible API. This method initializes tif1’s caching system and configures the cache directory. Once enabled, all data fetched through tif1 will be automatically cached for faster subsequent access.

Parameters

Returns

  • Cache: The configured global tif1 cache instance

Behavior Details

When enable_cache() is called:
  1. Directory Creation: Creates the cache directory if it does not exist, with restricted permissions (0o700 on Unix-like systems)
  2. SQLite Initialization: Sets up the SQLite database with WAL (Write-Ahead Logging) mode for better concurrency
  3. Configuration Update: Updates the global tif1 configuration to enable caching
  4. Instance Reset: Closes any existing cache instance and creates a fresh one
  5. Optional Clear: If force_renew=True, clears all cached data

Cache Directory Resolution

The cache directory is resolved in the following order of precedence:
  1. Explicit cache_dir parameter
  2. TIF1_CACHE_DIR environment variable
  3. Configuration file setting (~/.tif1rc)
  4. Platform-specific default:
    • Windows: %LOCALAPPDATA%/Temp/tif1
    • macOS: ~/Library/Caches/tif1
    • Linux/other POSIX: ~/.cache/tif1 (or ~/.tif1 if ~/.cache does not exist)

Examples

Basic usage with default location:
Custom cache directory:
Enable cache and clear existing data:
Environment variable configuration:

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

Clear cached data from the specified cache directory. This method removes all cached session data, lap times, telemetry, and other stored information. Use this method to force fresh data fetches or to troubleshoot cache-related issues.

Parameters

Behavior Details

The clearing process works as follows:
  1. Active Cache Detection: If cache_dir is None, uses the now active cache instance
  2. Directory Validation: Verifies the cache directory exists and is a valid directory
  3. SQLite Clearing: Executes DELETE statements on all cache tables
  4. Memory Cache Flush: Clears in-memory LRU caches
  5. Legacy Cleanup (if deep=True): Removes FastF1 HTTP cache files for users migrating from fastf1

Cache Tables Cleared

  • cache table: General session data, lap times, race control messages, weather data
  • telemetry_cache table: Telemetry data indexed by (year, GP, session, driver, lap)
  • In-memory caches: Both general and telemetry-specific LRU caches

Examples

Clear the active cache:
Clear a specific cache directory:
Deep clean (remove legacy FastF1 files):
Clear cache before important race weekend:

Migration from fastf1

The basic API is identical:

Error Handling

  • NotADirectoryError: Raised if the specified cache_dir exists but is not a directory
  • OSError: 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 DELETE operations 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

Set the logging level for tif1 logger. Parameters:
  • level: Python logging level (for example, logging.DEBUG, logging.INFO, logging.WARNING)
Example:
Migration from fastf1:

Complete migration example

Here’s a complete example showing how to migrate a fastf1 script to tif1: Original fastf1 code:
Migrated tif1 code (minimal changes):

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 automatically
  • Cache.enable_cache(force_renew): Use Cache.clear_cache() instead
  • Cache.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 fastf1 with import 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
When these features are needed, continue using fastf1 or open an issue on the tif1 GitHub repository.

Getting Help

When issues occur during migration from fastf1:
  1. Check the Migration Guide for detailed instructions
  2. Review the FAQ for common questions
  3. Open an issue on GitHub
Last modified on September 3, 2026