-
Notifications
You must be signed in to change notification settings - Fork 44
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-get callback #38
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||
---|---|---|---|---|---|---|---|---|---|---|
|
@@ -74,6 +74,7 @@ class Cache: | |||||||||
default: Default value or function to use in :meth:`get` when key is not found. If callable, | ||||||||||
it will be passed a single argument, ``key``, and its return value will be set for that | ||||||||||
cache key. | ||||||||||
on_get: Callback which will be executed when a cache entry is got. | ||||||||||
on_delete: Callback which will be executed when a cache entry is removed. | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would be good to include the excepted method signatures for There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Any example? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||
stats: Cache statistics. | ||||||||||
""" | ||||||||||
|
@@ -90,12 +91,14 @@ def __init__( | |||||||||
timer: t.Callable[[], T_TTL] = time.time, | ||||||||||
default: t.Any = None, | ||||||||||
enable_stats: bool = False, | ||||||||||
on_get: t.Optional[t.Callable[[t.Hashable, t.Any, bool], None]] = None, | ||||||||||
on_delete: t.Optional[t.Callable[[t.Hashable, t.Any, RemovalCause], None]] = None, | ||||||||||
): | ||||||||||
self.maxsize = maxsize | ||||||||||
self.ttl = ttl | ||||||||||
self.timer = timer | ||||||||||
self.default = default | ||||||||||
self.on_get = on_get | ||||||||||
self.on_delete = on_delete | ||||||||||
self.stats = CacheStatsTracker(self, enable=enable_stats) | ||||||||||
|
||||||||||
|
@@ -262,6 +265,9 @@ def _get(self, key: t.Hashable, default: t.Any = None) -> t.Any: | |||||||||
self._delete(key, RemovalCause.EXPIRED) | ||||||||||
raise KeyError | ||||||||||
self.stats.inc_hit_count() | ||||||||||
|
||||||||||
if self.on_get: | ||||||||||
self.on_get(key, value, True) | ||||||||||
except KeyError: | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There's an edge-case here where if |
||||||||||
self.stats.inc_miss_count() | ||||||||||
if default is None: | ||||||||||
|
@@ -273,6 +279,9 @@ def _get(self, key: t.Hashable, default: t.Any = None) -> t.Any: | |||||||||
else: | ||||||||||
value = default | ||||||||||
|
||||||||||
if self.on_get: | ||||||||||
self.on_get(key, value, False) | ||||||||||
|
||||||||||
return value | ||||||||||
|
||||||||||
def get_many(self, iteratee: T_FILTER) -> dict: | ||||||||||
|
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.
Instead of
is got
, maybeis retrieved
.