-
Notifications
You must be signed in to change notification settings - Fork 45
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
Conversation
#: 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``). |
There was a problem hiding this comment.
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`).
src/cacheout/cache.py
Outdated
|
||
# 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) |
There was a problem hiding this comment.
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).
tests/test_cache.py
Outdated
cache.on_set = on_set | ||
|
||
cache.set("a", 1) | ||
assert re.match(r"^a=1, old_value=<object object at 0x.*>$", log) |
There was a problem hiding this comment.
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}
.
Usage
set
on_set
callback.on_set
is a callable object.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'sUNSET
).