|
| 1 | +# License: MIT |
| 2 | +# Copyright © 2024 Frequenz Energy-as-a-Service GmbH |
| 3 | + |
| 4 | +"""The LatestValueCache caches the latest value in a receiver. |
| 5 | +
|
| 6 | +It provides a way to look up the latest value in a stream whenever required, as |
| 7 | +long as there has been one value received. |
| 8 | +
|
| 9 | +[LatestValueCache][frequenz.channels.LatestValueCache] takes a |
| 10 | +[Receiver][frequenz.channels.Receiver] as an argument and stores the latest |
| 11 | +value received by that receiver. As soon as a value is received, its |
| 12 | +[`has_value`][frequenz.channels.LatestValueCache.has_value] method returns |
| 13 | +`True`, and its [`get`][frequenz.channels.LatestValueCache.get] method returns |
| 14 | +the latest value received. The `get` method will raise an exception if called |
| 15 | +before any messages have been received from the receiver. |
| 16 | +
|
| 17 | +Example: |
| 18 | +```python |
| 19 | +from frequenz.channels import Broadcast, LatestValueCache |
| 20 | +
|
| 21 | +channel = Broadcast[int](name="lvc_test") |
| 22 | +
|
| 23 | +cache = LatestValueCache(channel.new_receiver()) |
| 24 | +sender = channel.new_sender() |
| 25 | +
|
| 26 | +assert not cache.has_value() |
| 27 | +
|
| 28 | +await sender.send(5) |
| 29 | +
|
| 30 | +assert cache.has_value() |
| 31 | +assert cache.get() == 5 |
| 32 | +``` |
| 33 | +""" |
| 34 | + |
| 35 | +import asyncio |
| 36 | +import typing |
| 37 | + |
| 38 | +from ._receiver import Receiver |
| 39 | + |
| 40 | +T_co = typing.TypeVar("T_co", covariant=True) |
| 41 | + |
| 42 | + |
| 43 | +class _Sentinel: |
| 44 | + """A sentinel to denote that no value has been received yet.""" |
| 45 | + |
| 46 | + def __str__(self) -> str: |
| 47 | + """Return a string representation of this sentinel.""" |
| 48 | + return "<no value received yet>" |
| 49 | + |
| 50 | + |
| 51 | +class LatestValueCache(typing.Generic[T_co]): |
| 52 | + """A cache that stores the latest value in a receiver. |
| 53 | +
|
| 54 | + It provides a way to look up the latest value in a stream without any delay, |
| 55 | + as long as there has been one value received. |
| 56 | + """ |
| 57 | + |
| 58 | + def __init__( |
| 59 | + self, receiver: Receiver[T_co], *, unique_id: str | None = None |
| 60 | + ) -> None: |
| 61 | + """Create a new cache. |
| 62 | +
|
| 63 | + Args: |
| 64 | + receiver: The receiver to cache. |
| 65 | + unique_id: A string to help uniquely identify this instance. If not |
| 66 | + provided, a unique identifier will be generated from the object's |
| 67 | + [`id()`][id]. It is used mostly for debugging purposes. |
| 68 | + """ |
| 69 | + self._receiver = receiver |
| 70 | + self._unique_id: str = hex(id(self)) if unique_id is None else unique_id |
| 71 | + self._latest_value: T_co | _Sentinel = _Sentinel() |
| 72 | + self._task = asyncio.create_task( |
| 73 | + self._run(), name=f"LatestValueCache«{self._unique_id}»" |
| 74 | + ) |
| 75 | + |
| 76 | + @property |
| 77 | + def unique_id(self) -> str: |
| 78 | + """The unique identifier of this instance.""" |
| 79 | + return self._unique_id |
| 80 | + |
| 81 | + def get(self) -> T_co: |
| 82 | + """Return the latest value that has been received. |
| 83 | +
|
| 84 | + This raises a `ValueError` if no value has been received yet. Use `has_value` to |
| 85 | + check whether a value has been received yet, before trying to access the value, |
| 86 | + to avoid the exception. |
| 87 | +
|
| 88 | + Returns: |
| 89 | + The latest value that has been received. |
| 90 | +
|
| 91 | + Raises: |
| 92 | + ValueError: If no value has been received yet. |
| 93 | + """ |
| 94 | + if isinstance(self._latest_value, _Sentinel): |
| 95 | + raise ValueError("No value has been received yet.") |
| 96 | + return self._latest_value |
| 97 | + |
| 98 | + def has_value(self) -> bool: |
| 99 | + """Check whether a value has been received yet. |
| 100 | +
|
| 101 | + Returns: |
| 102 | + `True` if a value has been received, `False` otherwise. |
| 103 | + """ |
| 104 | + return not isinstance(self._latest_value, _Sentinel) |
| 105 | + |
| 106 | + async def _run(self) -> None: |
| 107 | + async for value in self._receiver: |
| 108 | + self._latest_value = value |
| 109 | + |
| 110 | + async def stop(self) -> None: |
| 111 | + """Stop the cache.""" |
| 112 | + if not self._task.done(): |
| 113 | + self._task.cancel() |
| 114 | + try: |
| 115 | + await self._task |
| 116 | + except asyncio.CancelledError: |
| 117 | + pass |
| 118 | + |
| 119 | + def __repr__(self) -> str: |
| 120 | + """Return a string representation of this cache.""" |
| 121 | + return ( |
| 122 | + f"<LatestValueCache latest_value={self._latest_value!r}, " |
| 123 | + f"receiver={self._receiver!r}, unique_id={self._unique_id!r}>" |
| 124 | + ) |
| 125 | + |
| 126 | + def __str__(self) -> str: |
| 127 | + """Return the last value seen by this cache.""" |
| 128 | + return str(self._latest_value) |
0 commit comments