Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: on-set callback #39

Merged
merged 3 commits into from
Nov 17, 2023
Merged

feat: on-set callback #39

merged 3 commits into from
Nov 17, 2023

Conversation

uncle-lv
Copy link
Contributor

Usage

set on_set callback.

cache = Cache(on_set=on_set)

on_set is a callable object.

Callable[[key: Hashable, new_value: Any, old_value: Any],  None]

key is the cache key.

new_value is the value is set.

old_value is the value is replaced(if the key didn't exist before, it's UNSET).

@coveralls
Copy link

coveralls commented Nov 14, 2023

Coverage Status

coverage: 100.0%. remained the same
when pulling 73b54ee on uncle-lv:on-set-callback
into 99d4782 on dgilland:master.

Comment on lines +37 to +41
#: Callback that will be executed when a cache entry is set.

#: It is called with arguments ``(key, new_value, old_value)`` where `key` is the cache key,
#: `new_value` is the value is set,
#: and `old_value` is the value is replaced(if the key didn't exist before, it's ``UNSET``).
Copy link
Owner

Choose a reason for hiding this comment

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

Some minor changes to comment:

#: Callback that will be executed when a cache entry is set. It is called with arguments
#: ``(key, new_value, old_value)`` where `key` is the cache key, `new_value` is the value is set,
#: and `old_value` is the value it replaced (if the key didn't exist before, it's :const:`UNSET`).


# Delete key before setting it so that it moves to the end of the OrderedDict key list.
# Needed for cache strategies that rely on the ordering of when keys were last inserted.
self._delete(key, RemovalCause.SET)
self._delete(key, RemovalCause._IGNORE)
Copy link
Owner

Choose a reason for hiding this comment

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

Would rather make the cause argument cause: t.Optional[RemovalCause] = None instead of adding RemovalCause._IGNORE to the enum (it's not a valid value).

cache.on_set = on_set

cache.set("a", 1)
assert re.match(r"^a=1, old_value=<object object at 0x.*>$", log)
Copy link
Owner

Choose a reason for hiding this comment

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

I think we can simplify the assertion by not using re.match. One suggestion would be to make log a dictionary and set it to something like log = {"key": key, "new_value": new_value, "old_value": old_value}. Then can do assert log == {"key": "a", "new_value": 1, "old_value": UNSET}.

@dgilland dgilland merged commit 7b5cea8 into dgilland:master Nov 17, 2023
7 checks passed
@uncle-lv uncle-lv mentioned this pull request Nov 17, 2023
@uncle-lv uncle-lv deleted the on-set-callback branch November 17, 2023 01:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants