Cache Management¶
nflreadpy.clear_cache ¶
Clear cached data entries matching a pattern.
This is the main function for clearing nflreadpy's cache. It provides a simple interface to the underlying CacheManager functionality.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
pattern
|
str | None
|
Optional string pattern to match against cached data. If None, clears all cached data. Pattern matching is performed on cache keys, which typically contain URLs and parameters. |
None
|
Examples:
>>> import nflreadpy as nfl
>>> nfl.clear_cache() # Clear all cached data
>>> nfl.clear_cache("pbp_2023") # Clear 2023 play-by-play data
>>> nfl.clear_cache("roster") # Clear all roster data
Note
This affects both memory and filesystem cache depending on your cache configuration. See nflreadpy.config for cache settings.
See Also
Source code in src/nflreadpy/cache.py
nflreadpy.cache ¶
Caching functionality for nflreadpy.
This module provides intelligent caching capabilities for NFL data to improve performance and reduce network requests. It supports both memory and filesystem caching with configurable expiration and cache modes.
The caching system is designed to be transparent to users - data functions automatically check the cache before downloading and store results after successful downloads.
Key Features: - Memory caching for fast repeated access - Filesystem caching for persistence across sessions - Configurable cache duration and storage location - Automatic cache expiration and cleanup - Pattern-based cache clearing
Examples:
>>> import nflreadpy as nfl
>>>
>>> # Data is automatically cached
>>> pbp = nfl.load_pbp([2023])
>>>
>>> # Subsequent calls use cached data
>>> pbp_again = nfl.load_pbp([2023]) # Much faster!
>>>
>>> # Clear specific cached data
>>> nfl.clear_cache("pbp_2023")
>>>
>>> # Clear all cached data
>>> nfl.clear_cache()
CacheManager ¶
Manages caching for nflreadpy data.
The CacheManager handles both memory and filesystem caching of NFL data to improve performance and reduce network requests. It supports configurable cache modes and automatic expiration of cached data.
Attributes:
Name | Type | Description |
---|---|---|
_memory_cache |
dict[str, tuple[DataFrame, float]]
|
Internal dictionary storing cached DataFrames with timestamps. |
Initialize a new CacheManager instance.
Source code in src/nflreadpy/cache.py
clear ¶
Clear cache entries matching a pattern.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
pattern
|
str | None
|
Optional string pattern to match against cache keys. If None, clears all cache entries. |
None
|
Examples:
>>> cache_manager = get_cache_manager()
>>> cache_manager.clear() # Clear all cache
>>> cache_manager.clear("pbp_2023") # Clear entries containing "pbp_2023"
Note
Clears both memory and filesystem cache entries that match the pattern.
Source code in src/nflreadpy/cache.py
get ¶
Retrieve cached data if available and not expired.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
url
|
str
|
The data source URL. |
required |
**kwargs
|
str | int | float | bool
|
Additional parameters that were used when caching. |
{}
|
Returns:
Type | Description |
---|---|
DataFrame | None
|
Cached DataFrame if available and valid, None otherwise. |
Note
Checks memory cache first (if using MEMORY mode), then filesystem cache. Automatically removes expired cache entries.
Source code in src/nflreadpy/cache.py
set ¶
Store data in the cache.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
url
|
str
|
The data source URL. |
required |
data
|
DataFrame
|
The DataFrame to cache. |
required |
**kwargs
|
str | int | float | bool
|
Additional parameters that affect the data. |
{}
|
Note
Storage location depends on cache mode configuration: - MEMORY: Stores in memory with timestamp - FILESYSTEM: Saves as Parquet file with current timestamp - OFF: No caching performed
Source code in src/nflreadpy/cache.py
size ¶
Get cache size and entry count information.
Returns:
Type | Description |
---|---|
dict[str, int | float]
|
Dictionary containing cache statistics: |
dict[str, int | float]
|
|
dict[str, int | float]
|
|
dict[str, int | float]
|
|
Examples:
>>> cache_manager = get_cache_manager()
>>> stats = cache_manager.size()
>>> print(f"Memory entries: {stats['memory_entries']}")
>>> print(f"Disk entries: {stats.get('filesystem_entries', 0)}")
Source code in src/nflreadpy/cache.py
clear_cache ¶
Clear cached data entries matching a pattern.
This is the main function for clearing nflreadpy's cache. It provides a simple interface to the underlying CacheManager functionality.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
pattern
|
str | None
|
Optional string pattern to match against cached data. If None, clears all cached data. Pattern matching is performed on cache keys, which typically contain URLs and parameters. |
None
|
Examples:
>>> import nflreadpy as nfl
>>> nfl.clear_cache() # Clear all cached data
>>> nfl.clear_cache("pbp_2023") # Clear 2023 play-by-play data
>>> nfl.clear_cache("roster") # Clear all roster data
Note
This affects both memory and filesystem cache depending on your cache configuration. See nflreadpy.config for cache settings.
See Also
Source code in src/nflreadpy/cache.py
get_cache_manager ¶
Get the global cache manager instance.
Returns:
Type | Description |
---|---|
CacheManager
|
The singleton CacheManager instance used by all nflreadpy functions. |
Examples:
>>> cache_manager = get_cache_manager()
>>> cache_stats = cache_manager.size()
>>> cache_manager.clear("pbp_2023")