pipefunc.cache module#
Provides pipefunc.cache module with cache classes for memoization and caching.
- class pipefunc.cache.HybridCache(max_size=128, access_weight=0.5, duration_weight=0.5, *, allow_cloudpickle=True, shared=True)[source]#
Bases:
_CacheBaseA hybrid cache implementation.
This uses a combination of Least Frequently Used (LFU) and Least Computationally Expensive (LCE) strategies for invalidating cache entries.
The cache invalidation strategy calculates a score for each entry based on its access frequency and computation duration. The entry with the lowest score will be invalidated when the cache reaches its maximum size.
- max_size#
The maximum number of entries the cache can store.
- access_weight#
The weight given to the access frequency in the score calculation.
- duration_weight#
The weight given to the computation duration in the score calculation.
- allow_cloudpickle#
Use cloudpickle for storing the data in memory if using shared memory.
Whether the cache should be shared between multiple processes.
- property computation_durations: dict[Hashable, float]#
Return the computation durations of the cache entries.
- get(key)[source]#
Retrieve a value from the cache by its key.
If the key is present in the cache, its access count is incremented.
- class pipefunc.cache.LRUCache(*, max_size=128, allow_cloudpickle=True, shared=True)[source]#
Bases:
_CacheBaseA shared memory LRU cache implementation.
- Parameters:
- class pipefunc.cache.SimpleCache[source]#
Bases:
_CacheBaseA simple cache without any eviction strategy.
- class pipefunc.cache.DiskCache(cache_dir, max_size=None, *, use_cloudpickle=True, with_lru_cache=True, lru_cache_size=128, lru_shared=True, permissions=None)[source]#
Bases:
_CacheBaseDisk cache implementation using pickle or cloudpickle for serialization.
- Parameters:
cache_dir (
str|Path) β The directory where the cache files are stored.max_size (
int|None) β The maximum number of cache files to store. If None, no limit is set.use_cloudpickle (
bool) β Use cloudpickle for storing the data in memory.with_lru_cache (
bool) β Use an in-memory LRU cache to prevent reading from disk too often.lru_cache_size (
int) β The maximum size of the in-memory LRU cache. Only used if with_lru_cache is True.lru_shared (
bool) β Whether the in-memory LRU cache should be shared between multiple processes.The file permissions to set for the cache files. If None, the default permissions are used. Some examples:
0o660 (read/write for owner and group, no access for others)
0o644 (read/write for owner, read-only for group and others)
0o777 (read/write/execute for everyone - generally not recommended)
0o600 (read/write for owner, no access for group and others)
None (use the systemβs default umask)
Return whether the cache is shared.
- pipefunc.cache.memoize(cache=None, key_func=None, *, fallback_to_pickle=True, unhashable_action='error')[source]#
A flexible memoization decorator that works with different cache types.
- Parameters:
cache (
HybridCache|LRUCache|SimpleCache|DiskCache|None) β An instance of a cache class (_CacheBase). If None, a SimpleCache is used.key_func (
Callable[...,Hashable] |None) β A function to generate cache keys. If None, the default key generation which attempts to make all arguments hashable.fallback_to_pickle (
bool) β IfTrue, unhashable objects will be pickled to bytes usingcloudpickleas a last resort. IfFalse, an exception will be raised for unhashable objects. Only used ifkey_funcis None.unhashable_action (
Literal['error','warning','ignore']) β Determines the behavior when encountering unhashable objects: - βerrorβ: Raise an UnhashableError (default). - βwarningβ: Log a warning and skip caching for that call. - βignoreβ: Silently skip caching for that call. Only used ifkey_funcis None.
- Return type:
Decorated function with memoization.
- Raises:
UnhashableError β If the object cannot be made hashable and
fallback_to_pickleisFalse.
Notes
This function creates a hashable representation of both positional and keyword arguments, allowing for effective caching of function calls with various argument types.
- pipefunc.cache.try_to_hashable(obj, fallback_to_pickle=True, unhashable_action='error', where='function')[source]#
Try to convert an object to a hashable representation.
Wrapper around
to_hashablethat allows for different actions when encountering unhashable objects.- Parameters:
obj (
Any) β The object to convert.fallback_to_pickle (
bool) β IfTrue, unhashable objects will be pickled to bytes usingcloudpickleas a last resort. IfFalse, an exception will be raised for unhashable objects.unhashable_action (
Literal['error','warning','ignore']) β Determines the behavior when encountering unhashable objects: -"error": Raise anUnhashableError(default). -"warning": Log a warning and skip caching for that call. -"ignore": Silently skip caching for that call. ReturnsUnhashableError.where (
str) β The location where the unhashable object was encountered. Used for warning or error messages.
- Return type:
- Returns:
A hashable representation of the input object.
- Raises:
UnhashableError β If the object cannot be made hashable and
fallback_to_pickleisFalse.
Notes
This function attempts to create a hashable representation of any input object. It handles most built-in Python types and some common third-party types like numpy arrays and pandas Series/DataFrames.
- pipefunc.cache.to_hashable(obj, fallback_to_pickle=True)[source]#
Convert any object to a hashable representation if not hashable yet.
- Parameters:
- Return type:
A hashable representation of the input object.
- Raises:
UnhashableError β If the object cannot be made hashable and fallback_to_pickle is False.
Notes
This function attempts to create a hashable representation of any input object. It handles most built-in Python types and some common third-party types like numpy arrays and pandas Series/DataFrames.