Skip to content

Commit e16f3f0

Browse files
committed
Support clearing the latest value in the LatestValueCache
This can be the overall latest value or the latest value for a specific key. This can be used to clean up old keys for which no further messages are expected. Signed-off-by: Sahas Subramanian <[email protected]>
1 parent 6f24b25 commit e16f3f0

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

src/frequenz/channels/_latest_value_cache.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,22 @@ def has_value(self, key: HashableT | Sentinel = NO_KEY) -> bool:
194194
return key in self._latest_value_by_key
195195
return not isinstance(self._latest_value, Sentinel)
196196

197+
def clear(self, key: HashableT | Sentinel = NO_KEY) -> None:
198+
"""Clear the latest value or the latest value for a specific key.
199+
200+
If `key` is provided, it clears the latest value for that key. If no key is
201+
provided, it clears the latest value received overall.
202+
203+
Args:
204+
key: An optional key to clear the latest value for that key. If not
205+
provided, it clears the latest value received overall.
206+
"""
207+
if not isinstance(key, Sentinel):
208+
_ = self._latest_value_by_key.pop(key, None)
209+
return
210+
211+
self._latest_value = NO_VALUE_RECEIVED
212+
197213
async def _run(self) -> None:
198214
async for value in self._receiver:
199215
self._latest_value = value

0 commit comments

Comments
 (0)