-
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 2 commits
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 |
---|---|---|
|
@@ -18,14 +18,6 @@ | |
from .stats import CacheStatsTracker | ||
|
||
|
||
F = t.TypeVar("F", bound=t.Callable[..., t.Any]) | ||
T_DECORATOR = t.Callable[[F], F] | ||
T_TTL = t.Union[int, float] | ||
T_FILTER = t.Union[str, t.List[t.Hashable], t.Pattern, t.Callable] | ||
|
||
UNSET = object() | ||
|
||
|
||
class RemovalCause(Enum): | ||
""" | ||
An enum to represent the cause for the removal of a cache entry. | ||
|
@@ -46,6 +38,31 @@ class RemovalCause(Enum): | |
POPITEM = auto() | ||
|
||
|
||
F = t.TypeVar("F", bound=t.Callable[..., t.Any]) | ||
T_DECORATOR = t.Callable[[F], F] | ||
T_TTL = t.Union[int, float] | ||
T_FILTER = t.Union[str, t.List[t.Hashable], t.Pattern, t.Callable] | ||
ON_GET_CALLBACK = t.Optional[t.Callable[[t.Hashable, t.Any, bool], None]] | ||
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. Prefix these with |
||
""" | ||
Callback that will be executed when a cache entry is retrieved. | ||
|
||
It is called with arguments ``(key, value, exists)`` where `key` is the cache key, | ||
`value` is the value retrieved (could be the default), | ||
and `exists` is whether the cache key exists or not. | ||
""" | ||
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. Convert these to #: Callback that will be executed when a cache entry is retrieved. It is called with arguments
#: ``(key, value, exists)`` where `key` is the cache key, `value` is the value retrieved (could be
#: the default), and `exists` is whether the cache key exists or not.
T_ON_DELETE_CALLBACK = ... |
||
|
||
ON_DELETE_CALLBACK = t.Optional[t.Callable[[t.Hashable, t.Any, RemovalCause], None]] | ||
""" | ||
Callback that will be executed when a cache entry is removed. | ||
|
||
It is called with arguments ``(key, value, cause)`` where `key` is the cache key, | ||
`value` is the cached value at the time of deletion, | ||
and `cause` is the reason the key was removed (see :class:`RemovalCause` for enumerated causes). | ||
""" | ||
|
||
UNSET = object() | ||
|
||
|
||
class Cache: | ||
""" | ||
An in-memory, FIFO cache object. | ||
|
@@ -74,6 +91,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 retrieved. | ||
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. My thinking of putting the variable descriptions here was so that it would be prominent when viewing the class documentation. Does any of the details from the constant docstrings show up here in the generated docs? 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. I guess we could add a class reference so that there's at least a link to the variable docs using something like:
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.
Yes, it works. |
||
stats: Cache statistics. | ||
""" | ||
|
@@ -90,12 +108,14 @@ def __init__( | |
timer: t.Callable[[], T_TTL] = time.time, | ||
default: t.Any = None, | ||
enable_stats: bool = False, | ||
on_delete: t.Optional[t.Callable[[t.Hashable, t.Any, RemovalCause], None]] = None, | ||
on_get: ON_GET_CALLBACK = None, | ||
on_delete: ON_DELETE_CALLBACK = 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) | ||
|
||
|
@@ -255,6 +275,7 @@ def get(self, key: t.Hashable, default: t.Any = None) -> t.Any: | |
return self._get(key, default=default) | ||
|
||
def _get(self, key: t.Hashable, default: t.Any = None) -> t.Any: | ||
existed = True | ||
try: | ||
value = self._cache[key] | ||
|
||
|
@@ -263,6 +284,7 @@ def _get(self, key: t.Hashable, default: t.Any = None) -> t.Any: | |
raise KeyError | ||
self.stats.inc_hit_count() | ||
except KeyError: | ||
existed = False | ||
self.stats.inc_miss_count() | ||
if default is None: | ||
default = self.default | ||
|
@@ -273,6 +295,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, existed) | ||
|
||
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.
These should stay at the top right below the imports.