5151from synapse .config import cache as cache_config
5252from synapse .metrics .background_process_metrics import wrap_as_background_process
5353from synapse .util import Clock , caches
54- from synapse .util .caches import CacheMetric , EvictionReason
54+ from synapse .util .caches import CacheManager , CacheMetric , EvictionReason
5555from synapse .util .caches .treecache import (
5656 TreeCache ,
5757 iterate_tree_cache_entry ,
@@ -379,11 +379,44 @@ class LruCache(Generic[KT, VT]):
379379 If cache_type=TreeCache, all keys must be tuples.
380380 """
381381
382+ # If you're providing in the `cache_name`, then you must provide the `cache_manager`
383+ @overload
384+ def __init__ (
385+ self ,
386+ max_size : int ,
387+ * ,
388+ cache_name : str ,
389+ cache_manager : CacheManager ,
390+ metrics_collection_callback : Optional [Callable [[], None ]] = None ,
391+ cache_type : Type [Union [dict , TreeCache ]] = dict ,
392+ size_callback : Optional [Callable [[VT ], int ]] = None ,
393+ apply_cache_factor_from_config : bool = True ,
394+ clock : Optional [Clock ] = None ,
395+ prune_unread_entries : bool = True ,
396+ extra_index_cb : Optional [Callable [[KT , VT ], KT ]] = None ,
397+ ): ...
398+
399+ # If you're *not* providing in the `cache_name`, then you shouldn't provide the
400+ # `cache_manager` or the `metrics_collection_callback`
401+ @overload
402+ def __init__ (
403+ self ,
404+ max_size : int ,
405+ * ,
406+ cache_type : Type [Union [dict , TreeCache ]] = dict ,
407+ size_callback : Optional [Callable [[VT ], int ]] = None ,
408+ apply_cache_factor_from_config : bool = True ,
409+ clock : Optional [Clock ] = None ,
410+ prune_unread_entries : bool = True ,
411+ extra_index_cb : Optional [Callable [[KT , VT ], KT ]] = None ,
412+ ): ...
413+
382414 def __init__ (
383415 self ,
384416 max_size : int ,
385417 * ,
386418 cache_name : Optional [str ] = None ,
419+ cache_manager : Optional [CacheManager ] = None ,
387420 metrics_collection_callback : Optional [Callable [[], None ]] = None ,
388421 cache_type : Type [Union [dict , TreeCache ]] = dict ,
389422 size_callback : Optional [Callable [[VT ], int ]] = None ,
@@ -399,6 +432,9 @@ def __init__(
399432 cache_name: The name of this cache, for the prometheus metrics. If unset,
400433 no metrics will be reported on this cache.
401434
435+ cache_manager: The cache manager to handle metrics. If unset, no metrics will be
436+ reported on this cache.
437+
402438 Ignored if `cache_name` is `None`.
403439
404440 metrics_collection_callback:
@@ -463,17 +499,15 @@ def __init__(
463499 # do yet when we get resized.
464500 self ._on_resize : Optional [Callable [[], None ]] = None
465501
466- # TODO
467- # if cache_name is not None:
468- # metrics: Optional[CacheMetric] = cache_manager.register_cache(
469- # "lru_cache",
470- # cache_name,
471- # self,
472- # collect_callback=metrics_collection_callback,
473- # )
474- # else:
475- # metrics = None
476- metrics : Optional [CacheMetric ] = None
502+ if cache_name is not None :
503+ metrics : Optional [CacheMetric ] = cache_manager .register_cache (
504+ "lru_cache" ,
505+ cache_name ,
506+ self ,
507+ collect_callback = metrics_collection_callback ,
508+ )
509+ else :
510+ metrics = None
477511
478512 # this is exposed for access from outside this class
479513 self .metrics = metrics
0 commit comments