latest.ipv8.requestcache
Module Contents
Classes
A cache for state information that is uniquely identified by a prefix and a number. |
|
A cache with a randomly generated number. |
|
All caches with a |
|
A NumberCache with a |
|
A RandomNumberCache with a |
|
Manager for NumberCache caches. |
Attributes
- class latest.ipv8.requestcache.NumberCache(request_cache: RequestCache, prefix: str, number: int)
A cache for state information that is uniquely identified by a prefix and a number.
- property managed_futures: list[tuple[asyncio.Future, object | None]]
Get a list of all managed futures.
- register_future(future: asyncio.Future, on_timeout: object | None = None) None
Register a future for this Cache that will be canceled when this Cache times out.
- Parameters:
future – the future to register for this instance
on_timeout – The value to which the future is to be set when a timeout occurs. If the value is an instance of Exception, future.set_exception will be called instead of future.set_result
- class latest.ipv8.requestcache.RandomNumberCache(request_cache: RequestCache, prefix: str)
Bases:
NumberCache
A cache with a randomly generated number.
- classmethod find_unclaimed_identifier(request_cache: RequestCache, prefix: str) int
Generate a random number for use with this cache.
- class latest.ipv8.requestcache.CacheWithName
Bases:
typing_extensions.Protocol
All caches with a
name
attribute.
- latest.ipv8.requestcache.CacheTypeVar
- latest.ipv8.requestcache.ACT
- class latest.ipv8.requestcache.NumberCacheWithName(request_cache: RequestCache, prefix: str, number: int)
Bases:
NumberCache
,CacheWithName
,abc.ABC
A NumberCache with a
name
attribute.
- class latest.ipv8.requestcache.RandomNumberCacheWithName(request_cache: RequestCache, prefix: str)
Bases:
RandomNumberCache
,CacheWithName
,abc.ABC
A RandomNumberCache with a
name
attribute.
- class latest.ipv8.requestcache.RequestCache
Bases:
latest.ipv8.taskmanager.TaskManager
Manager for NumberCache caches.
- _timeout_override: float | None
If not None, this specifies the timeout to use instead of the one defined in the
timeout_delay
of aNumberCache
. This is used internally forpassthrough()
, don’t modify this directly!
- _timeout_filters: Iterable[type[NumberCache]] | None
If not None, this specifies the
NumberCache
(sub)classes to apply the timeout override for. This is used internally forpassthrough()
, don’t modify this directly!
- add(cache: ACT) ACT | None
Add CACHE into this RequestCache instance.
Returns CACHE when CACHE.identifier was not yet added, otherwise returns None.
- has(prefix: str, number: int) bool
- has(prefix: type[CacheTypeVar], number: int) bool
Returns True when IDENTIFIER is part of this RequestCache.
- get(prefix: str, number: int) NumberCache | None
- get(prefix: type[CacheTypeVar], number: int) CacheTypeVar | None
Returns the Cache associated with IDENTIFIER when it exists, otherwise returns None.
- pop(prefix: str, number: int) NumberCache
- pop(prefix: type[CacheTypeVar], number: int) CacheTypeVar
Returns the Cache associated with IDENTIFIER, and removes it from this RequestCache, when it exists, otherwise raises a KeyError exception.
- passthrough(cls_filter: type[NumberCache] | None = None, *filters: type[NumberCache], timeout: float = 0.0) Generator
A contextmanager that overwrites the timeout_delay of added NumberCaches in its scope. This can be used to shorten or eliminate timeouts of external code.
— Example 1: Eliminating timeouts, regardless of
cache.timeout_delay
—with request_cache.passthrough(): request_cache.add(cache) # This will instantly timeout (once the main thread is yielded). with request_cache.passthrough(): # Any internal call to request_cache.add() will also be instantly timed out. await some_function_that_uses_request_cache()
— Example 2: Modifying timeouts, regardless of
cache.timeout_delay
—with request_cache.passthrough(timeout=0.1): request_cache.add(cache) # This will timeout after 0.1 seconds.
— Example 3: Filtering for specific classes —
# Only MyCacheClass, MyOtherCacheClass, YetAnotherCacheClass will have their timeout changed to 4 seconds. with request_cache.passthrough(MyCacheClass, MyOtherCacheClass, YetAnotherCacheClass, timeout=4.0): request_cache.add(cache)
- Parameters:
cls_filter – An optional class filter to specify which classes the timeout override needs to apply to.
filters – Additional class filters to specify which classes the timeout override needs to apply to.
timeout – The timeout in seconds to use for the
NumberCache
instances this applies to.
- Returns:
A context manager (compatible with
with
).
- _on_timeout(cache: NumberCache) None
Called CACHE.timeout_delay seconds after CACHE was added to this RequestCache.
_on_timeout is called for every Cache, except when it has been popped before the timeout expires. When called _on_timeout will CACHE.on_timeout().
- clear() list[asyncio.Future]
Clear the cache, canceling all pending tasks.