Disk
DiskCacheBackend(cache_dir=None)
¶
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.
|
Examples
>>> 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
>>> 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(func, *, config=None)
¶
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(key)
¶
Get a value from the cache.
PARAMETER | DESCRIPTION |
---|---|
key |
Cache key.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Optional[Any]
|
Cached value or None if not found. |