Skip to content

Commit

Permalink
Add the keys.typedmethodkey decorator
Browse files Browse the repository at this point in the history
Closes: #296
  • Loading branch information
kuraga authored and tkem committed May 28, 2024
1 parent 449826a commit f425346
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 23 deletions.
8 changes: 7 additions & 1 deletion docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -491,7 +491,7 @@ functions with the :func:`cached` and :func:`cachedmethod` decorators:

.. autofunction:: methodkey

This function is equivalent to :func:`hashkey`, but ignores its
This function is similar to :func:`hashkey`, but ignores its
first positional argument, i.e. `self` when used with the
:func:`cachedmethod` decorator.

Expand All @@ -502,6 +502,12 @@ functions with the :func:`cached` and :func:`cachedmethod` decorators:
``typedkey(3)`` and ``typedkey(3.0)`` will return different
results.

.. autofunction:: typedmethodkey

This function is similar to :func:`typedkey`, but ignores its
first positional argument, i.e. `self` when used with the
:func:`cachedmethod` decorator.

These functions can also be helpful when implementing custom key
functions for handling some non-hashable arguments. For example,
calling the following function with a dictionary as its `env` argument
Expand Down
7 changes: 6 additions & 1 deletion src/cachetools/keys.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"""Key functions for memoizing decorators."""

__all__ = ("hashkey", "methodkey", "typedkey")
__all__ = ("hashkey", "methodkey", "typedkey", "typedmethodkey")


class _HashedTuple(tuple):
Expand Down Expand Up @@ -55,3 +55,8 @@ def typedkey(*args, **kwargs):
key += tuple(type(v) for v in args)
key += tuple(type(v) for _, v in sorted(kwargs.items()))
return key


def typedmethodkey(self, *args, **kwargs):
"""Return a typed cache key for use with cached methods."""
return typedkey(*args, **kwargs)
42 changes: 21 additions & 21 deletions tests/test_cachedmethod.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ def get(self, value):
self.count += 1
return count

@cachedmethod(lambda self: self.cache, key=keys.typedkey)
def get_typed(self, value):
@cachedmethod(lambda self: self.cache, key=keys.typedmethodkey)
def get_typedmethod(self, value):
count = self.count
self.count += 1
return count
Expand Down Expand Up @@ -67,16 +67,16 @@ def test_dict(self):
cached.cache.clear()
self.assertEqual(cached.get(1), 2)

def test_typed_dict(self):
def test_typedmethod_dict(self):
cached = Cached(LRUCache(maxsize=2))

self.assertEqual(cached.get_typed(0), 0)
self.assertEqual(cached.get_typed(1), 1)
self.assertEqual(cached.get_typed(1), 1)
self.assertEqual(cached.get_typed(1.0), 2)
self.assertEqual(cached.get_typed(1.0), 2)
self.assertEqual(cached.get_typed(0.0), 3)
self.assertEqual(cached.get_typed(0), 4)
self.assertEqual(cached.get_typedmethod(0), 0)
self.assertEqual(cached.get_typedmethod(1), 1)
self.assertEqual(cached.get_typedmethod(1), 1)
self.assertEqual(cached.get_typedmethod(1.0), 2)
self.assertEqual(cached.get_typedmethod(1.0), 2)
self.assertEqual(cached.get_typedmethod(0.0), 3)
self.assertEqual(cached.get_typedmethod(0), 4)

def test_lru(self):
cached = Cached(LRUCache(maxsize=2))
Expand All @@ -90,16 +90,16 @@ def test_lru(self):
cached.cache.clear()
self.assertEqual(cached.get(1), 2)

def test_typed_lru(self):
def test_typedmethod_lru(self):
cached = Cached(LRUCache(maxsize=2))

self.assertEqual(cached.get_typed(0), 0)
self.assertEqual(cached.get_typed(1), 1)
self.assertEqual(cached.get_typed(1), 1)
self.assertEqual(cached.get_typed(1.0), 2)
self.assertEqual(cached.get_typed(1.0), 2)
self.assertEqual(cached.get_typed(0.0), 3)
self.assertEqual(cached.get_typed(0), 4)
self.assertEqual(cached.get_typedmethod(0), 0)
self.assertEqual(cached.get_typedmethod(1), 1)
self.assertEqual(cached.get_typedmethod(1), 1)
self.assertEqual(cached.get_typedmethod(1.0), 2)
self.assertEqual(cached.get_typedmethod(1.0), 2)
self.assertEqual(cached.get_typedmethod(0.0), 3)
self.assertEqual(cached.get_typedmethod(0), 4)

def test_nospace(self):
cached = Cached(LRUCache(maxsize=0))
Expand Down Expand Up @@ -141,10 +141,10 @@ def __add__(self, other):
self.assertEqual(cached.get(1), 2)
self.assertEqual(cached.get(1.0), 2)

ref = cached.get_typed(1)
ref = cached.get_typedmethod(1)
self.assertEqual(ref, 3)
self.assertEqual(cached.get_typed(1), 3)
self.assertEqual(cached.get_typed(1.0), 4)
self.assertEqual(cached.get_typedmethod(1), 3)
self.assertEqual(cached.get_typedmethod(1.0), 4)

cached.cache.clear()
self.assertEqual(cached.get(1), 5)
Expand Down

0 comments on commit f425346

Please sign in to comment.