Skip to content
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

Add the keys.typedmethodkey decorator #305

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

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
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reading the tests alone, this makes much more sense than previously. So 👍, thanks for your commitment (sorry for the bad joke, couldn't resist)!

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
Loading