hephex / asyncache

Helpers to use cachetools with async functions
MIT License
90 stars 12 forks source link

Default key function for `cachemethod` decorator #19

Open eug-alekseev opened 1 year ago

eug-alekseev commented 1 year ago

Hello!

I've found a small discrepancy between cachetools.cahcmethod and asyncache.cachemethod behavior in regards of default key function. Perhaps it makes sense to use keys.methodkey instead of keys.hashkey there to exclude self being used for cache keys? This would neglect the requirement of a class instance to be hashable and overall just looks like a better way of caching instance method calls.

I don't have access to create PRs myself, here is the change snippet:

--- a/asyncache/__init__.py
+++ b/asyncache/__init__.py
@@ -117,7 +117,7 @@ def cachedmethod(
     cache: Callable[[Any], Optional[MutableMapping[_KT, Any]]],
     # ignoring the mypy error to be consistent with the type used
     # in https://github.com/python/typeshed/tree/master/stubs/cachetools
-    key: Callable[..., _KT] = keys.hashkey,  # type:ignore
+    key: Callable[..., _KT] = keys.methodkey,  # type:ignore
     lock: Optional[Callable[[Any], "AbstractContextManager[Any]"]] = None,