4444from synapse .util .async_helpers import delay_cancellation
4545from synapse .util .caches .deferred_cache import DeferredCache
4646from synapse .util .caches .lrucache import LruCache
47+ from synapse .util .caches .redis_caches import redisCachedList
4748
4849logger = logging .getLogger (__name__ )
4950
@@ -371,6 +372,7 @@ def _wrapped(*args: Any, **kwargs: Any) -> Any:
371372 wrapped .invalidate_all = cache .invalidate_all
372373 wrapped .cache = cache
373374 wrapped .num_args = self .num_args
375+ wrapped .tree = self .tree
374376
375377 obj .__dict__ [self .orig .__name__ ] = wrapped
376378
@@ -418,6 +420,8 @@ def __init__(
418420 % (self .list_name , cached_method_name )
419421 )
420422
423+ self .orig_wrapped = self .orig
424+
421425 def __get__ (
422426 self , obj : Optional [Any ], objtype : Optional [Type ] = None
423427 ) -> Callable [..., "defer.Deferred[Dict[Hashable, Any]]" ]:
@@ -509,7 +513,7 @@ def errback_all(f: Failure) -> None:
509513
510514 # dispatch the call, and attach the two handlers
511515 defer .maybeDeferred (
512- preserve_fn (self .orig ), ** args_to_call
516+ preserve_fn (self .orig_wrapped ), ** args_to_call
513517 ).addCallbacks (complete_all , errback_all )
514518
515519 if cached_defers :
@@ -525,6 +529,29 @@ def errback_all(f: Failure) -> None:
525529 else :
526530 return defer .succeed (results )
527531
532+ def enable_redis_cache (external_sharded_cache ):
533+ if getattr (cached_method , "redis_enabled" , False ):
534+ return
535+
536+ self .orig_wrapped = redisCachedList (
537+ external_sharded_cache ,
538+ self .cached_method_name ,
539+ self .list_name ,
540+ )(self .orig )
541+
542+ # Tree caches cannot be backed by Redis
543+ assert not cached_method .tree
544+ # Cache context is not supported yet
545+ assert not self .add_cache_context
546+
547+ # Cache invalidations are not allowed with Redis backed caches (currently)
548+ def block_invalidate ():
549+ raise Exception ("Cannot invalidate Redis backed @cachedList" )
550+ cached_method .invalidate = block_invalidate
551+ cached_method .invalidate_all = block_invalidate
552+ cached_method .redis_enabled = True
553+
554+ wrapped .enable_redis_cache = enable_redis_cache
528555 obj .__dict__ [self .orig .__name__ ] = wrapped
529556
530557 return wrapped
0 commit comments