From 95ab0ad9c001b7e045cddeefdcd299052b61bb35 Mon Sep 17 00:00:00 2001 From: Jason Little Date: Sat, 29 Jul 2023 17:13:48 -0500 Subject: [PATCH] Add metrics tracking for eviction based on time to ResponseCache --- synapse/util/caches/response_cache.py | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/synapse/util/caches/response_cache.py b/synapse/util/caches/response_cache.py index 340e5e914533..784081a996e9 100644 --- a/synapse/util/caches/response_cache.py +++ b/synapse/util/caches/response_cache.py @@ -36,7 +36,7 @@ ) from synapse.util import Clock from synapse.util.async_helpers import AbstractObservableDeferred, ObservableDeferred -from synapse.util.caches import register_cache +from synapse.util.caches import register_cache, EvictionReason logger = logging.getLogger(__name__) @@ -167,7 +167,7 @@ def on_complete(r: RV) -> RV: # the should_cache bit, we leave it in the cache for now and schedule # its removal later. if self.timeout_sec and context.should_cache: - self.clock.call_later(self.timeout_sec, self.unset, key) + self.clock.call_later(self.timeout_sec, self.entry_timeout, key) else: # otherwise, remove the result immediately. self.unset(key) @@ -187,6 +187,16 @@ def unset(self, key: KV) -> None: """ self._result_cache.pop(key, None) + def evict_because(self, key: KV, reason: EvictionReason) -> None: + """Basically the same as unset, but update reason why evicting for metrics""" + self._metrics.inc_evictions(reason) + self.unset(key) + + def entry_timeout(self, key: KV) -> None: + """For the all later to remove from the cache""" + logger.debug(f"Expiring from [{self._name}] - {key}") + self.evict_because(key, EvictionReason.time) + async def wrap( self, key: KV,