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

Commit e49e716

Browse files
committed
Add name argument to cached & cachedList descriptors
1 parent d2c07a2 commit e49e716

File tree

1 file changed

+19
-7
lines changed

1 file changed

+19
-7
lines changed

synapse/util/caches/descriptors.py

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,10 @@ def __init__(
7373
num_args: Optional[int],
7474
uncached_args: Optional[Collection[str]] = None,
7575
cache_context: bool = False,
76+
name: Optional[str] = None,
7677
):
7778
self.orig = orig
79+
self.name = name or orig.__name__
7880

7981
arg_spec = inspect.getfullargspec(orig)
8082
all_args = arg_spec.args
@@ -211,7 +213,7 @@ def __init__(
211213

212214
def __get__(self, obj: Optional[Any], owner: Optional[Type]) -> Callable[..., Any]:
213215
cache: LruCache[CacheKey, Any] = LruCache(
214-
cache_name=self.orig.__name__,
216+
cache_name=self.name,
215217
max_size=self.max_entries,
216218
)
217219

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

242244
wrapped = cast(_CachedFunction, _wrapped)
243245
wrapped.cache = cache
244-
obj.__dict__[self.orig.__name__] = wrapped
246+
obj.__dict__[self.name] = wrapped
245247

246248
return wrapped
247249

@@ -301,12 +303,14 @@ def __init__(
301303
cache_context: bool = False,
302304
iterable: bool = False,
303305
prune_unread_entries: bool = True,
306+
name: Optional[str] = None,
304307
):
305308
super().__init__(
306309
orig,
307310
num_args=num_args,
308311
uncached_args=uncached_args,
309312
cache_context=cache_context,
313+
name=name,
310314
)
311315

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

322326
def __get__(self, obj: Optional[Any], owner: Optional[Type]) -> Callable[..., Any]:
323327
cache: DeferredCache[CacheKey, Any] = DeferredCache(
324-
name=self.orig.__name__,
328+
name=self.name,
325329
max_entries=self.max_entries,
326330
tree=self.tree,
327331
iterable=self.iterable,
@@ -372,7 +376,7 @@ def _wrapped(*args: Any, **kwargs: Any) -> Any:
372376
wrapped.cache = cache
373377
wrapped.num_args = self.num_args
374378

375-
obj.__dict__[self.orig.__name__] = wrapped
379+
obj.__dict__[self.name] = wrapped
376380

377381
return wrapped
378382

@@ -393,6 +397,7 @@ def __init__(
393397
cached_method_name: str,
394398
list_name: str,
395399
num_args: Optional[int] = None,
400+
name: Optional[str] = None,
396401
):
397402
"""
398403
Args:
@@ -403,7 +408,7 @@ def __init__(
403408
but including list_name) to use as cache keys. Defaults to all
404409
named args of the function.
405410
"""
406-
super().__init__(orig, num_args=num_args, uncached_args=None)
411+
super().__init__(orig, num_args=num_args, uncached_args=None, name=name)
407412

408413
self.list_name = list_name
409414

@@ -525,7 +530,7 @@ def errback_all(f: Failure) -> None:
525530
else:
526531
return defer.succeed(results)
527532

528-
obj.__dict__[self.orig.__name__] = wrapped
533+
obj.__dict__[self.name] = wrapped
529534

530535
return wrapped
531536

@@ -577,6 +582,7 @@ def cached(
577582
cache_context: bool = False,
578583
iterable: bool = False,
579584
prune_unread_entries: bool = True,
585+
name: Optional[str] = None,
580586
) -> Callable[[F], _CachedFunction[F]]:
581587
func = lambda orig: DeferredCacheDescriptor(
582588
orig,
@@ -587,13 +593,18 @@ def cached(
587593
cache_context=cache_context,
588594
iterable=iterable,
589595
prune_unread_entries=prune_unread_entries,
596+
name=name,
590597
)
591598

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

594601

595602
def cachedList(
596-
*, cached_method_name: str, list_name: str, num_args: Optional[int] = None
603+
*,
604+
cached_method_name: str,
605+
list_name: str,
606+
num_args: Optional[int] = None,
607+
name: Optional[str] = None,
597608
) -> Callable[[F], _CachedFunction[F]]:
598609
"""Creates a descriptor that wraps a function in a `DeferredCacheListDescriptor`.
599610
@@ -628,6 +639,7 @@ def batch_do_something(self, first_arg, second_args):
628639
cached_method_name=cached_method_name,
629640
list_name=list_name,
630641
num_args=num_args,
642+
name=name,
631643
)
632644

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

0 commit comments

Comments
 (0)