3333 List ,
3434 Mapping ,
3535 Optional ,
36- Protocol ,
3736 Sequence ,
3837 Tuple ,
3938 Type ,
4443from weakref import WeakValueDictionary
4544
4645import attr
47- from typing_extensions import Concatenate , ParamSpec
4846
4947from twisted .internet import defer
5048from twisted .python .failure import Failure
5149
5250from synapse .logging .context import make_deferred_yieldable , preserve_fn
5351from synapse .util import unwrapFirstError
5452from synapse .util .async_helpers import delay_cancellation
55- from synapse .util .caches import CacheManager
5653from synapse .util .caches .deferred_cache import DeferredCache
5754from synapse .util .caches .lrucache import LruCache
5855
@@ -186,7 +183,6 @@ def foo(self, key, cache_context):
186183
187184 Args:
188185 orig:
189- cache_manager: The cache manager to handle metrics
190186 max_entries:
191187 num_args: number of positional arguments (excluding ``self`` and
192188 ``cache_context``) to use as cache keys. Defaults to all named
@@ -200,14 +196,11 @@ def foo(self, key, cache_context):
200196 prune_unread_entries: If True, cache entries that haven't been read recently
201197 will be evicted from the cache in the background. Set to False to opt-out
202198 of this behaviour.
203- name: Will default to the `__name__` of the `orig` function.
204199 """
205200
206201 def __init__ (
207202 self ,
208203 orig : Callable [..., Any ],
209- * ,
210- cache_manager : CacheManager ,
211204 max_entries : int = 1000 ,
212205 num_args : Optional [int ] = None ,
213206 uncached_args : Optional [Collection [str ]] = None ,
@@ -224,7 +217,6 @@ def __init__(
224217 cache_context = cache_context ,
225218 name = name ,
226219 )
227- self .cache_manager = cache_manager
228220
229221 if tree and self .num_args < 2 :
230222 raise RuntimeError (
@@ -241,7 +233,6 @@ def __get__(
241233 ) -> Callable [..., "defer.Deferred[Any]" ]:
242234 cache : DeferredCache [CacheKey , Any ] = DeferredCache (
243235 name = self .name ,
244- cache_manager = self .cache_manager ,
245236 max_entries = self .max_entries ,
246237 tree = self .tree ,
247238 iterable = self .iterable ,
@@ -496,12 +487,10 @@ class _CachedFunctionDescriptor:
496487 iterable : bool
497488 prune_unread_entries : bool
498489 name : Optional [str ]
499- cache_manager : CacheManager
500490
501491 def __call__ (self , orig : F ) -> CachedFunction [F ]:
502492 d = DeferredCacheDescriptor (
503493 orig ,
504- cache_manager = self .cache_manager ,
505494 max_entries = self .max_entries ,
506495 num_args = self .num_args ,
507496 uncached_args = self .uncached_args ,
@@ -514,15 +503,6 @@ def __call__(self, orig: F) -> CachedFunction[F]:
514503 return cast (CachedFunction [F ], d )
515504
516505
517- P = ParamSpec ("P" )
518- R = TypeVar ("R" )
519-
520-
521- class HasCacheManager (Protocol ):
522- # Used to handle registering the caches
523- cache_manager : CacheManager
524-
525-
526506def cached (
527507 * ,
528508 max_entries : int = 1000 ,
@@ -533,55 +513,17 @@ def cached(
533513 iterable : bool = False ,
534514 prune_unread_entries : bool = True ,
535515 name : Optional [str ] = None ,
536- ) -> Callable [[Callable [P , Awaitable [R ]]], Callable [P , Awaitable [R ]]]:
537- """Decorate an async method with a `Measure` context manager.
538-
539- The Measure is created using `self.cache_manager`; it should only be used to decorate
540- methods in classes defining an instance-level `clock` attribute.
541-
542- Usage:
543-
544- @measure_func()
545- async def foo(...):
546- ...
547-
548- Which is analogous to:
549-
550- async def foo(...):
551- with Measure(...):
552- ...
553-
554- """
555-
556- def wrapper (
557- func : Callable [Concatenate [HasCacheManager , P ], Awaitable [R ]],
558- ) -> Callable [P , Awaitable [R ]]:
559- # block_name = func.__name__ if name is None else name
560-
561- @functools .wraps (func )
562- async def cached_func (
563- self : HasCacheManager , * args : P .args , ** kwargs : P .kwargs
564- ) -> R :
565- return _CachedFunctionDescriptor (
566- max_entries = max_entries ,
567- num_args = num_args ,
568- uncached_args = uncached_args ,
569- tree = tree ,
570- cache_context = cache_context ,
571- iterable = iterable ,
572- prune_unread_entries = prune_unread_entries ,
573- name = name ,
574- # Grab this attribute from the instance
575- cache_manager = self .cache_manager ,
576- )
577-
578- # There are some shenanigans here, because we're decorating a method but
579- # explicitly making use of the `self` parameter. The key thing here is that the
580- # return type within the return type for `measure_func` itself describes how the
581- # decorated function will be called.
582- return cached_func # type: ignore[return-value]
583-
584- return wrapper # type: ignore[return-value]
516+ ) -> _CachedFunctionDescriptor :
517+ return _CachedFunctionDescriptor (
518+ max_entries = max_entries ,
519+ num_args = num_args ,
520+ uncached_args = uncached_args ,
521+ tree = tree ,
522+ cache_context = cache_context ,
523+ iterable = iterable ,
524+ prune_unread_entries = prune_unread_entries ,
525+ name = name ,
526+ )
585527
586528
587529@attr .s (auto_attribs = True , slots = True , frozen = True )
0 commit comments