From e6e61089b63ed1ac20cb656d8c601bbd2577d3e4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rapha=C3=ABl=20Vinot?= Date: Thu, 23 Sep 2021 10:29:02 +0200 Subject: [PATCH] chg: Speedup cache initialization --- lookyloo/capturecache.py | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/lookyloo/capturecache.py b/lookyloo/capturecache.py index 98f7ca38..759716dd 100644 --- a/lookyloo/capturecache.py +++ b/lookyloo/capturecache.py @@ -86,6 +86,7 @@ class CapturesIndex(Mapping): self.redis = redis self.contextualizer = contextualizer self.__cache: Dict[str, CaptureCache] = {} + self._quick_init() def __getitem__(self, uuid: str) -> CaptureCache: if uuid in self.__cache: @@ -131,6 +132,22 @@ class CapturesIndex(Mapping): self.redis.flushdb() self.__cache = {} + def _quick_init(self) -> None: + '''Initialize the cache with a list of UUIDs, with less back and forth with redis. + Only get recent captures.''' + p = self.redis.pipeline() + for directory in self.redis.hvals('lookup_dirs'): + p.hgetall(directory) + for cache in p.execute(): + if not cache: + continue + try: + cc = CaptureCache(cache) + except LookylooException as e: + self.logger.warning(e) + continue + self.__cache[cc.uuid] = cc + def _get_capture_dir(self, uuid: str) -> Path: # Try to get from the recent captures cache in redis capture_dir = self.redis.hget('lookup_dirs', uuid)