pydvl.utils.caching.memcached
¶
MemcachedClientConfig
dataclass
¶
MemcachedClientConfig(
server: Tuple[str, int] = ("localhost", 11211),
connect_timeout: float = 1.0,
timeout: float = 1.0,
no_delay: bool = True,
serde: PickleSerde = PickleSerde(pickle_version=PICKLE_VERSION),
)
Configuration of the memcached client.
PARAMETER | DESCRIPTION |
---|---|
server |
A tuple of (IP|domain name, port). |
connect_timeout |
How many seconds to wait before raising
TYPE:
|
timeout |
Duration in seconds to wait for send or recv calls on the socket connected to memcached.
TYPE:
|
no_delay |
If True, set the
TYPE:
|
serde |
Serializer / Deserializer ("serde"). The default
TYPE:
|
MemcachedCacheBackend
¶
MemcachedCacheBackend(config: MemcachedClientConfig = MemcachedClientConfig())
Bases: CacheBackend
Memcached cache backend for the distributed caching of functions.
Implements the CacheBackend interface for a memcached based cache. This allows sharing evaluations across processes and nodes in a cluster. You can run memcached as a service, locally or remotely, see the caching documentation.
PARAMETER | DESCRIPTION |
---|---|
config |
Memcached client configuration.
TYPE:
|
ATTRIBUTE | DESCRIPTION |
---|---|
config |
Memcached client configuration.
|
client |
Memcached client instance.
|
Example
Basic usage:
>>> from pydvl.utils.caching.memcached import MemcachedCacheBackend
>>> cache_backend = MemcachedCacheBackend()
>>> cache_backend.clear()
>>> value = 42
>>> cache_backend.set("key", value)
>>> cache_backend.get("key")
42
Callable wrapping:
>>> from pydvl.utils.caching.memcached import MemcachedCacheBackend
>>> cache_backend = MemcachedCacheBackend()
>>> 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 |
---|---|
config |
Memcached client configuration.
TYPE:
|
Source code in src/pydvl/utils/caching/memcached.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 value from memcached.
PARAMETER | DESCRIPTION |
---|---|
key |
Cache key.
TYPE:
|
RETURNS | DESCRIPTION |
---|---|
Optional[Any]
|
Cached value or None if not found or client disconnected. |
Source code in src/pydvl/utils/caching/memcached.py
set
¶
clear
¶
combine_hashes
¶
__getstate__
¶
__getstate__() -> Dict
Enables pickling after a socket has been opened to the memcached server, by removing the client from the stored data.