Skip to content

Commit 3e68eea

Browse files
committed
Add a user-guide entry for LatestValueCache
Signed-off-by: Sahas Subramanian <[email protected]>
1 parent e5f661e commit 3e68eea

File tree

2 files changed

+38
-3
lines changed

2 files changed

+38
-3
lines changed
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# Caching
2+
3+
::: frequenz.channels._latest_value_cache
4+
options:
5+
inherited_members: []
6+
members: []
7+
show_bases: false
8+
show_root_heading: false
9+
show_root_toc_entry: false
10+
show_source: false

src/frequenz/channels/_latest_value_cache.py

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,35 @@
11
# License: MIT
22
# Copyright © 2024 Frequenz Energy-as-a-Service GmbH
33

4-
"""A cache that stores the latest value in a receiver.
4+
"""The LatestValueCache caches the latest value in a receiver.
55
6-
It provides a way to look up the latest value in a stream without any delay,
7-
as long as there has been one value received.
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+
```
833
"""
934

1035
import asyncio

0 commit comments

Comments
 (0)