Skip to content

Commit

Permalink
Merge branch 'master' into on-set-callback
Browse files Browse the repository at this point in the history
  • Loading branch information
uncle-lv authored Nov 14, 2023
2 parents 6a15b37 + 99d4782 commit 6bd8578
Showing 1 changed file with 24 additions and 22 deletions.
46 changes: 24 additions & 22 deletions src/cacheout/cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,35 @@


F = t.TypeVar("F", bound=t.Callable[..., t.Any])

#: Decorator type.
T_DECORATOR = t.Callable[[F], F]

#: Possible types for TTL (time to live) value.
T_TTL = t.Union[int, float]

#: Possible types that can be used to filter cache keys.
T_FILTER = t.Union[str, t.List[t.Hashable], t.Pattern, t.Callable]

#: 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_GET_CALLBACK = t.Optional[t.Callable[[t.Hashable, t.Any, bool], None]]

#: 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``).
T_ON_SET_CALLBACK = t.Optional[t.Callable[[t.Hashable, t.Any, t.Any], 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).
T_ON_DELETE_CALLBACK = t.Optional[t.Callable[[t.Hashable, t.Any, "RemovalCause"], None]]

#: Sentinel value to indicate that an argument was not set.
UNSET = object()


Expand All @@ -47,28 +71,6 @@ class RemovalCause(Enum):
_IGNORE = auto()


#: 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_GET_CALLBACK = t.Optional[t.Callable[[t.Hashable, t.Any, bool], None]]

#: 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``).
T_ON_SET_CALLBACK = t.Optional[t.Callable[[t.Hashable, t.Any, t.Any], 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).
T_ON_DELETE_CALLBACK = t.Optional[t.Callable[[t.Hashable, t.Any, RemovalCause], None]]


class Cache:
"""
An in-memory, FIFO cache object.
Expand Down

0 comments on commit 6bd8578

Please sign in to comment.