Skip to content

Caching¤

Caches the latest value received from a Receiver.

It provides a way to look up the latest value in a stream whenever required, as long as there has been one value received.

LatestValueCache takes a Receiver as an argument and stores the latest value received by that receiver. As soon as a value is received, its has_value method returns True, and its get method returns the latest value received. The get method will raise an exception if called before any messages have been received from the receiver.

Example
import asyncio

from frequenz.channels import BroadcastChannel, LatestValueCache

sender, receiver = BroadcastChannel[int](name="lvc_test")

cache = LatestValueCache(receiver)

assert not cache.has_value()

await sender.send(5)
await asyncio.sleep(0)  # Let the cache process the message

assert cache.has_value()
assert cache.get() == 5