Overview
FastF1’s caching system significantly speeds up data loading and prevents exceeding API rate limits. The cache has two stages:- Stage 1: Raw HTTP GET/POST request caching (using SQLite)
- Stage 2: Parsed data caching (using pickle files)
Cache Class
TheCache class provides class-level methods for configuring and managing the cache.
enable_cache()
Enables the API cache. This should be called at the beginning of your script, right after imports.Parameters
Path to the directory for storing cached data. The directory must exist. Supports:
- Environment variables:
%LOCALAPPDATA%,$HOME, etc. - User home expansion:
~or~user
Ignore if cached data was created with a different version of the API parser.
Ignore existing cached data and re-download everything, updating the cache.
Enable stage 1 caching (raw HTTP requests). Disable if you only want stage 2 caching.
Example
clear_cache()
Clears all cached data by deleting cache files.Parameters
Path to the cache directory to clear. If
None, uses the default cache directory.Also clear the stage 1 requests cache (SQLite database). If
False, only clears stage 2 pickle files.Example
Can be called without enabling the cache first.
get_cache_info()
Returns information about the cache directory and its size.Returns
Path to the cache directory, or
None if cache is not configuredTotal size of the cache in bytes, or
None if cache is not configuredExample
disabled()
Returns a context manager that temporarily disables the cache.Returns
Context manager objectThe context manager is not multithreading-safe.
set_disabled()
Disables the cache while keeping the configuration intact.set_enabled() to re-enable.
Prefer using
disabled() context manager for temporary disabling.set_enabled()
Re-enables the cache after it was disabled withset_disabled().
offline_mode()
Enables or disables offline mode.Parameters
True to enable offline mode, False to disable- No actual requests are sent to APIs
- Only cached data is returned
- Useful for working with unstable internet or freezing cache state
Must be called after
enable_cache() when using a custom cache directory.Example
ci_mode()
Enables or disables CI (Continuous Integration) mode.Parameters
True to enable CI mode, False to disable- Cached requests are reused even if expired
- Every request is only made once and cached indefinitely
- Stage 2 (pickle) cache is disabled
- Useful for test environments to reduce API calls and increase predictability
Must be called after
enable_cache() when using a custom cache directory.Cache Configuration
The cache directory is determined in order of precedence:- Explicit call to
enable_cache() FASTF1_CACHEenvironment variable- OS-dependent default location
Default Cache Locations
Environment Variable
Set theFASTF1_CACHE environment variable to configure the default cache location:
This value is ignored if
enable_cache() is called explicitly.Cache Structure
The cache directory contains:Manual Cache Management
You can manually delete specific events or sessions:- Navigate to the cache directory
- Find the year/event/session folder
- Delete the folder
Helper Functions
While not part of the Cache class, these module-level functions work with the cache:enable_cache()
Shorthand forCache.enable_cache():
Examples
Basic Setup
Force Refresh
Temporary Cache Disable
Check Cache Size
Working Offline
Best Practices
Always enable caching
Always enable caching
Caching dramatically improves performance and prevents hitting API rate limits. Always call
enable_cache() at the start of your script.Use version checking
Use version checking
Keep
ignore_version=False (default) to avoid loading incompatible cached data after FastF1 updates.Manage cache size
Manage cache size
The cache can grow large over time. Periodically check and clear it:
Don't commit cache to version control
Don't commit cache to version control
Add your cache directory to
.gitignore:Notes
Cached requests do not count towards API rate limits.
HTTP cache (stage 1) uses cache control headers and refreshes periodically (every 12 hours by default).
See Also
- Caching Guide - Detailed caching concepts
- Ergast API - Uses the cache system
- Live Timing - Also benefits from caching
