FABulous.caching#
Caching utilities for FABulous.
Provides: - Cache: singleton file-backed cache (implemented via __new__) - cache_with: dependency-aware caching decorator
- Usage:
# Initialize the cache directory Cache(“/path/to/cache”)
# Use the decorator to cache a function @cache_with(files=lambda self: [self.config_path]) def build(self):
…
Classes#
- class Cache(cache_dir=None)[source]#
Singleton file-backed cache for function results.
The first instantiation may specify cache_dir. Subsequent instantiations return the same instance; if cache_dir is provided it updates the path.
Caching can be globally enabled/disabled using enable() and disable() methods, or temporarily controlled using the temporarily_disabled() context manager.
- Parameters:
cache_dir (str | None) – The cache directory path.
instance. (Create or return the singleton Cache)
:param : :type : param cache_dir: The cache directory path. If provided on first call, sets the cache dir. :param : :type : type cache_dir: str | None :param : :type : returns: The singleton instance. :param : :type : rtype: Self :param : :type : raises RuntimeError: If attempting to reinitialize with a different cache_dir.
Properties:
- property cache_dir[source]#
The cache directory path.
- Returns:
The path to the cache directory.
- Return type:
Methods:
- cache_disabled()[source]#
Context manager to temporarily disable caching.
- Yields:
None
- Return type:
Generator[None, None, None]
Examples
>>> # Caching is normally enabled >>> result1 = cached_function() # Uses cache >>> with Cache.cache_disabled(): ... result2 = cached_function() # Bypasses cache >>> result3 = cached_function() # Uses cache again
- cache_enabled()[source]#
Context manager to temporarily enable caching.
Useful when caching is globally disabled but you want to enable it for a specific code block.
- Yields:
None
- Return type:
Generator[None, None, None]
Examples
>>> Cache.disable() # Disable globally >>> with Cache.cache_enabled(): ... result = cached_function() # Uses cache
- disable()[source]#
Disable caching globally.
When disabled, all cache checks will return cache miss, forcing functions to re- execute every time.
- Return type:
None
- is_cache_hit(cache_key, files, params=None)[source]#
Return True when stored metadata indicates dependencies are unchanged.
- Parameters:
- Returns:
True if the cache is valid (hit), False if a cache miss
- Return type:
- is_enabled()[source]#
Check if caching is currently enabled.
- Returns:
True if enabled, False otherwise.
- Return type:
- class DiscoveryHelper[source]#
Helper class for discovering file dependencies in config files.
Methods:
- expand_config_files(files)[source]#
Expand list of files by recursively extracting paths from config files.
For each configuration file in the input list, this method will: 1. Include the config file itself 2. Recursively extract and include any file paths referenced within it 3. Cache the expansion results to avoid recomputation
- extract_file_paths_from_value(value, base_dir, visited)[source]#
Recursively extract file paths from a configuration value.
- class HashHelper[source]#
Helper class for hashing files and parameters.
Methods:
- hash_file_content(file_path)[source]#
Return a SHA256 hash of a file’s contents.
Uses hashlib.file_digest() for efficient file hashing without loading the entire file into memory.
- Parameters:
file_path (Path) – The path to the file to hash.
- Returns:
The SHA256 hash as a hexadecimal string.
- Return type:
- Raises:
FileNotFoundError – If the file does not exist.
- hash_files(files)[source]#
Return a combined hash for a list of files.
The files are sorted by path to ensure stable ordering.
Functions#
Return a dependency-aware caching decorator.
- param param:
Whether to include parameters in cache key. Defaults to True.
- type param:
bool
- param files:
Function to get file dependencies.
- type files:
Callable[…, list[Path | str]] | None
- param return_cached:
Whether to return cached value. Defaults to False.
- type return_cached:
bool
- returns:
The decorator function.
- rtype:
Callable[[Callable], Callable]
Examples
>>> class Worker: ... def __init__(self, path: Path): ... self.path = path ... ... @cache_with(files=lambda self: [self.path]) ... def build(self): ... ...