Why Use Caching?
Without caching:- Every script run downloads data from F1 APIs
- Processing and parsing must be repeated
- You may hit API rate limits
- Scripts take much longer to run
- Data is downloaded once and reused
- Processing is done once and stored
- Scripts run 10-100x faster after first run
- Reduced load on F1 APIs
It is highly recommended to always enable caching. The performance improvement is substantial, and it helps prevent exceeding API rate limits.
Quick Start
Enable caching at the start of your script:Enabling the Cache
enable_cache()
str
required
Path to the directory for storing cached data. The directory must already exist.Supports:
- Absolute paths:
'/home/user/fastf1_cache' - Home directory:
'~/fastf1_cache' - Environment variables:
'%LOCALAPPDATA%/fastf1_cache'(Windows)
bool
default:"False"
If
True, uses cached data even if it was created with a different FastF1 version.bool
default:"False"
If
True, ignores all existing cached data and downloads fresh data.bool
default:"True"
Enable caching of raw HTTP requests (stage 1 cache).
Example Usage
Cache Location
You can specify the cache location in three ways (in order of precedence):1. Explicit Configuration
Callenable_cache() with a path:
2. Environment Variable
Set theFASTF1_CACHE environment variable:
3. Default Location
If neither is specified, FastF1 uses an OS-dependent default:Windows
Windows
%LOCALAPPDATA%\Temp\fastf1Typically: C:\Users\YourName\AppData\Local\Temp\fastf1macOS
macOS
~/Library/Caches/fastf1Full path: /Users/YourName/Library/Caches/fastf1Linux
Linux
~/.cache/fastf1 (if ~/.cache exists)Otherwise: ~/.fastf1How Caching Works
FastF1 uses a two-stage caching system:Stage 1: HTTP Request Cache
Raw GET and POST requests are cached in a SQLite database:- File:
fastf1_http_cache.sqlitein the cache directory - Caches raw API responses
- Applies cache control and expiration (12 hours by default)
- Reduces network requests
Stage 2: Parsed Data Cache
Processed and parsed data is cached as pickle files:- Files:
*.ff1pklin session-specific subdirectories - Caches Python objects after parsing
- Saves computation time
- Organized by year/event/session
Cache Information
Get information about the current cache:Managing the Cache
Clearing the Cache
Delete all cached data:You can call
clear_cache() without enabling the cache first.Manual Cache Management
You can manually delete cache files:Cache Size Management
The cache can grow large over time. Typical sizes:- Single qualifying session: 5-15 MB
- Single race: 20-50 MB
- Full season (all sessions): 3-8 GB
- Clear old/unused data regularly
- Use selective loading (disable telemetry if not needed)
- Store cache on a drive with sufficient space
Temporarily Disabling Cache
Disable caching for specific code sections:Using Context Manager
Manual Control
Offline Mode
Work with cached data without internet:- Working without internet connection
- Ensuring reproducible results
- Preventing accidental data updates
Performance Benefits
Real-world performance comparison:Without Cache
With Cache (First Run)
With Cache (Subsequent Runs)
Advanced Features
CI Mode
For continuous integration environments:Version Handling
When FastF1 is updated, cached data from older versions may be incompatible:Best Practices
Always Enable Caching in Scripts
Always Enable Caching in Scripts
Start every script with cache enablement:
Use a Dedicated Cache Directory
Use a Dedicated Cache Directory
Don’t mix cache files with your project files:
Create Cache Directory First
Create Cache Directory First
Ensure the directory exists before enabling cache:
Periodically Clear Old Data
Periodically Clear Old Data
Clean up cache for sessions you no longer need:
Monitor Cache Size
Monitor Cache Size
Check cache size periodically:
Troubleshooting
Cache Not Working
If caching doesn’t seem to work:-
Verify directory exists
-
Check cache is enabled
-
Verify files are created
Version Mismatch Errors
If you see version mismatch warnings:Permission Errors
If you get permission errors:Related Topics
- Loading Data - Understanding what data is cached
- Sessions and Events - Basic session loading
