Skip to content

Conversation

@shsms
Copy link
Contributor

@shsms shsms commented Jun 16, 2025

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.

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]>
Copilot AI review requested due to automatic review settings June 16, 2025 11:53
@shsms shsms requested a review from a team as a code owner June 16, 2025 11:53
@shsms shsms added the cmd:skip-release-notes It is not necessary to update release notes for this PR label Jun 16, 2025
@shsms
Copy link
Contributor Author

shsms commented Jun 16, 2025

No release notes, because this is an enhancement to an unreleased feature.

Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

Adds a clear method to reset either the overall latest value or the value for a specific key.

  • Introduce clear(key) to remove a key-specific latest value from the cache.
  • Default clear() resets the overall latest value to NO_VALUE_RECEIVED.
Comments suppressed due to low confidence (1)

src/frequenz/channels/_latest_value_cache.py:197

  • Add unit tests to verify that clear() correctly resets the overall latest value and that clear(key) only removes the entry for that key.
def clear(self, key: HashableT | Sentinel = NO_KEY) -> None:

@github-actions github-actions bot added the part:tests Affects the unit, integration and performance (benchmarks) tests label Jun 16, 2025
llucax
llucax previously approved these changes Jun 16, 2025
@llucax
Copy link
Contributor

llucax commented Jun 16, 2025

No release notes, because this is an enhancement to an unreleased feature.

Not entirely true, LastValueCache existed already, and this extends the released implementation to allow for clearing the "global" cache.

@llucax llucax removed the cmd:skip-release-notes It is not necessary to update release notes for this PR label Jun 16, 2025
@llucax
Copy link
Contributor

llucax commented Jun 16, 2025

I removed the skip notes label, I'm on the edge here, it is still a pretty small change so not putting it into the release notes is not the end of the world, but technically we should. If you want to add the label again and merge, I won't complain.

Signed-off-by: Sahas Subramanian <[email protected]>
@github-actions github-actions bot added the part:docs Affects the documentation label Jun 16, 2025
@shsms
Copy link
Contributor Author

shsms commented Jun 16, 2025

True, updated.

@shsms shsms requested a review from llucax June 16, 2025 12:38
@shsms shsms enabled auto-merge June 16, 2025 12:42
Comment on lines +197 to +212
def clear(self, key: HashableT | Sentinel = NO_KEY) -> None:
"""Clear the latest value or the latest value for a specific key.
If `key` is provided, it clears the latest value for that key. If no key is
provided, it clears the latest value received overall.
Args:
key: An optional key to clear the latest value for that key. If not
provided, it clears the latest value received overall.
"""
if not isinstance(key, Sentinel):
_ = self._latest_value_by_key.pop(key, None)
return

self._latest_value = NO_VALUE_RECEIVED

Copy link
Contributor

@ela-kotulska-frequenz ela-kotulska-frequenz Jun 16, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why call clear() doesn't remove all keys and values in self._latest_value_by_key ?
Or shouldn't you remove _latest_value_by_key[self._key(self._latest_value)]], too?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh damn, because my usecase didn't need clearing all keys. I wonder if we should change the API slightly to keep the option open for a "clear everything" in the future.

any opinions @llucax ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Another option is to make the key parameter required, so that I can make progress and we can decide how to generalise later.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some options I can think of:

  1. clear_all(), clear() but it is true that clear() with no key looks ambiguous
  2. clear_all(), clear_key() maybe is more clear that without a key it clears the no-key?

I'm starting to find the use case for a "global" key a bit weird. I guess we need to support it for backwards compatibility, otherwise maybe we should always require a key and let the user use something like None if they don't want one? Not sure.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We haven't released it. I can actually pull out all the new stuff into experimental.GroupingLatestValueCache that doesn't have a global mode.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done here: #428

@shsms shsms disabled auto-merge June 16, 2025 12:59
@shsms
Copy link
Contributor Author

shsms commented Jun 16, 2025

This will go away once #428 is merged.

@shsms shsms marked this pull request as draft June 16, 2025 15:56
@shsms shsms closed this Jun 17, 2025
@shsms shsms deleted the clear-cache branch July 10, 2025 07:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

part:docs Affects the documentation part:tests Affects the unit, integration and performance (benchmarks) tests

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants