Skip to content
This repository has been archived by the owner on Apr 26, 2024. It is now read-only.

Commit

Permalink
Add name argument to cached & cachedList descriptors
Browse files Browse the repository at this point in the history
  • Loading branch information
Fizzadar committed Aug 23, 2022
1 parent d2c07a2 commit e49e716
Showing 1 changed file with 19 additions and 7 deletions.
26 changes: 19 additions & 7 deletions synapse/util/caches/descriptors.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,10 @@ def __init__(
num_args: Optional[int],
uncached_args: Optional[Collection[str]] = None,
cache_context: bool = False,
name: Optional[str] = None,
):
self.orig = orig
self.name = name or orig.__name__

arg_spec = inspect.getfullargspec(orig)
all_args = arg_spec.args
Expand Down Expand Up @@ -211,7 +213,7 @@ def __init__(

def __get__(self, obj: Optional[Any], owner: Optional[Type]) -> Callable[..., Any]:
cache: LruCache[CacheKey, Any] = LruCache(
cache_name=self.orig.__name__,
cache_name=self.name,
max_size=self.max_entries,
)

Expand Down Expand Up @@ -241,7 +243,7 @@ def _wrapped(*args: Any, **kwargs: Any) -> Any:

wrapped = cast(_CachedFunction, _wrapped)
wrapped.cache = cache
obj.__dict__[self.orig.__name__] = wrapped
obj.__dict__[self.name] = wrapped

return wrapped

Expand Down Expand Up @@ -301,12 +303,14 @@ def __init__(
cache_context: bool = False,
iterable: bool = False,
prune_unread_entries: bool = True,
name: Optional[str] = None,
):
super().__init__(
orig,
num_args=num_args,
uncached_args=uncached_args,
cache_context=cache_context,
name=name,
)

if tree and self.num_args < 2:
Expand All @@ -321,7 +325,7 @@ def __init__(

def __get__(self, obj: Optional[Any], owner: Optional[Type]) -> Callable[..., Any]:
cache: DeferredCache[CacheKey, Any] = DeferredCache(
name=self.orig.__name__,
name=self.name,
max_entries=self.max_entries,
tree=self.tree,
iterable=self.iterable,
Expand Down Expand Up @@ -372,7 +376,7 @@ def _wrapped(*args: Any, **kwargs: Any) -> Any:
wrapped.cache = cache
wrapped.num_args = self.num_args

obj.__dict__[self.orig.__name__] = wrapped
obj.__dict__[self.name] = wrapped

return wrapped

Expand All @@ -393,6 +397,7 @@ def __init__(
cached_method_name: str,
list_name: str,
num_args: Optional[int] = None,
name: Optional[str] = None,
):
"""
Args:
Expand All @@ -403,7 +408,7 @@ def __init__(
but including list_name) to use as cache keys. Defaults to all
named args of the function.
"""
super().__init__(orig, num_args=num_args, uncached_args=None)
super().__init__(orig, num_args=num_args, uncached_args=None, name=name)

self.list_name = list_name

Expand Down Expand Up @@ -525,7 +530,7 @@ def errback_all(f: Failure) -> None:
else:
return defer.succeed(results)

obj.__dict__[self.orig.__name__] = wrapped
obj.__dict__[self.name] = wrapped

return wrapped

Expand Down Expand Up @@ -577,6 +582,7 @@ def cached(
cache_context: bool = False,
iterable: bool = False,
prune_unread_entries: bool = True,
name: Optional[str] = None,
) -> Callable[[F], _CachedFunction[F]]:
func = lambda orig: DeferredCacheDescriptor(
orig,
Expand All @@ -587,13 +593,18 @@ def cached(
cache_context=cache_context,
iterable=iterable,
prune_unread_entries=prune_unread_entries,
name=name,
)

return cast(Callable[[F], _CachedFunction[F]], func)


def cachedList(
*, cached_method_name: str, list_name: str, num_args: Optional[int] = None
*,
cached_method_name: str,
list_name: str,
num_args: Optional[int] = None,
name: Optional[str] = None,
) -> Callable[[F], _CachedFunction[F]]:
"""Creates a descriptor that wraps a function in a `DeferredCacheListDescriptor`.
Expand Down Expand Up @@ -628,6 +639,7 @@ def batch_do_something(self, first_arg, second_args):
cached_method_name=cached_method_name,
list_name=list_name,
num_args=num_args,
name=name,
)

return cast(Callable[[F], _CachedFunction[F]], func)
Expand Down

0 comments on commit e49e716

Please sign in to comment.