pydvl.utils.caching.base
¶
CacheStats
dataclass
¶
CacheStats(
sets: int = 0,
misses: int = 0,
hits: int = 0,
timeouts: int = 0,
errors: int = 0,
reconnects: int = 0,
)
Class used to store statistics gathered by cached functions.
ATTRIBUTE | DESCRIPTION |
---|---|
sets |
Number of times a value was set in the cache.
TYPE:
|
misses |
Number of times a value was not found in the cache.
TYPE:
|
hits |
Number of times a value was found in the cache.
TYPE:
|
timeouts |
Number of times a timeout occurred.
TYPE:
|
errors |
Number of times an error occurred.
TYPE:
|
reconnects |
Number of times the client reconnected to the server.
TYPE:
|
CacheResult
dataclass
¶
CacheBackend
¶
Bases: ABC
Abstract base class for cache backends.
Defines interface for cache access including wrapping callables, getting/setting results, clearing cache, and combining cache keys.
ATTRIBUTE | DESCRIPTION |
---|---|
stats |
Cache statistics tracker.
|
Source code in src/pydvl/utils/caching/base.py
wrap
¶
wrap(
func: Callable, *, config: Optional[CachedFuncConfig] = None
) -> CachedFunc
Wraps a function to cache its results.
PARAMETER | DESCRIPTION |
---|---|
func |
The function to wrap.
TYPE:
|
config |
Optional caching options for the wrapped function.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
CachedFunc
|
The wrapped cached function. |
Source code in src/pydvl/utils/caching/base.py
get
abstractmethod
¶
get(key: str) -> Optional[CacheResult]
Abstract method to retrieve a cached result.
Implemented by subclasses.
PARAMETER | DESCRIPTION |
---|---|
key |
The cache key.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Optional[CacheResult]
|
The cached result or None if not found. |
Source code in src/pydvl/utils/caching/base.py
set
abstractmethod
¶
set(key: str, value: CacheResult) -> None
Abstract method to set a cached result.
Implemented by subclasses.
PARAMETER | DESCRIPTION |
---|---|
key |
The cache key.
TYPE:
|
value |
The result to cache.
TYPE:
|
clear
abstractmethod
¶
CachedFunc
¶
CachedFunc(
func: Callable[..., float],
*,
cache_backend: CacheBackend,
config: Optional[CachedFuncConfig] = None
)
Caches callable function results with a provided cache backend.
Wraps a callable function to cache its results using a provided an instance of a subclass of CacheBackend.
This class is heavily inspired from that of joblib.memory.MemorizedFunc.
This class caches calls to the wrapped callable by generating a hash key based on the wrapped callable's code, the arguments passed to it and the optional hash_prefix.
Warning
This class only works with hashable arguments to the wrapped callable.
PARAMETER | DESCRIPTION |
---|---|
func |
Callable to wrap. |
cache_backend |
Instance of CacheBackendBase that handles setting and getting values.
TYPE:
|
config |
Configuration for wrapped function.
TYPE:
|