pydvl.utils.caching.disk
¶
DiskCacheBackend
¶
Bases: CacheBackend
Disk cache backend that stores results in files.
Implements the CacheBackend interface for a disk-based cache. Stores cache entries as pickled files on disk, keyed by cache key. This allows sharing evaluations across processes in a single node/computer.
PARAMETER | DESCRIPTION |
---|---|
cache_dir |
Base directory for cache storage. |
ATTRIBUTE | DESCRIPTION |
---|---|
cache_dir |
Base directory for cache storage.
|
Example
Basic usage:
>>> from pydvl.utils.caching.disk import DiskCacheBackend
>>> cache_backend = DiskCacheBackend()
>>> cache_backend.clear()
>>> value = 42
>>> cache_backend.set("key", value)
>>> cache_backend.get("key")
42
Callable wrapping:
>>> from pydvl.utils.caching.disk import DiskCacheBackend
>>> cache_backend = DiskCacheBackend()
>>> cache_backend.clear()
>>> value = 42
>>> def foo(x: int):
... return x + 1
...
>>> wrapped_foo = cache_backend.wrap(foo)
>>> wrapped_foo(value)
43
>>> wrapped_foo.stats.misses
1
>>> wrapped_foo.stats.hits
0
>>> wrapped_foo(value)
43
>>> wrapped_foo.stats.misses
1
>>> wrapped_foo.stats.hits
1
PARAMETER | DESCRIPTION |
---|---|
cache_dir |
Base directory for cache storage. If not provided, this defaults to a newly created temporary directory. |
Source code in src/pydvl/utils/caching/disk.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
¶
Get a value from the cache.
PARAMETER | DESCRIPTION |
---|---|
key |
Cache key.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Optional[Any]
|
Cached value or None if not found. |