From 7580c687e2ff8d1fc09cfc2b7636002c707ff22d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nils=20B=C3=BCchner?= Date: Sun, 15 Sep 2024 00:19:00 +0200 Subject: [PATCH] cache policy lists and increase general cache time --- synapse_invite_checker/invite_checker.py | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/synapse_invite_checker/invite_checker.py b/synapse_invite_checker/invite_checker.py index 4d3da4e..a2694fe 100644 --- a/synapse_invite_checker/invite_checker.py +++ b/synapse_invite_checker/invite_checker.py @@ -28,7 +28,7 @@ class InviteChecker: self.use_allowlist = self.config.use_allowlist self.use_blocklist = self.config.use_blocklist - self.cache_expiry_time = 60 + self.cache_expiry_time = 300 self.cache_timestamp = 0 self.blocklist = set() self.allowlist = set() @@ -120,9 +120,9 @@ class InviteChecker: self.blocklist = set(json_data.get('blocklist', [])) self.allowlist = set(json_data.get('allowlist', [])) - # Fetch and merge the policy room ban lists from multiple rooms + # Fetch and cache the policy room ban lists policy_banlist = yield self.fetch_policy_room_banlist() - self.blocklist.update(policy_banlist) + self.blocklist.update(policy_banlist) # Merge policy bans into blocklist self.blocklist_room_ids = set() for room_entry in json_data.get('blocklist_rooms', []): @@ -137,6 +137,9 @@ class InviteChecker: else: logger.error(f"Failed to blocklist room: {room_entry}") + # Update the cache timestamp + self.cache_timestamp = time.time() + logger.info(f"Updated blocklist with {len(self.blocklist)} entries and {len(self.blocklist_room_ids)} room IDs.") else: logger.error("Failed to update allowlist/blocklist due to missing JSON data.") @@ -161,6 +164,7 @@ class InviteChecker: def get_blocklist_allowlist(self): current_time = time.time() + # Update cache if expired if current_time - self.cache_timestamp > self.cache_expiry_time: yield self.update_blocklist_allowlist() @@ -168,6 +172,7 @@ class InviteChecker: logger.info("Skipping allowlist/blocklist checks because of previous JSON fetch failure.") returnValue((set(), set(), set())) + # Return cached blocklist, allowlist, and blocklist room IDs returnValue((self.blocklist, self.allowlist, self.blocklist_room_ids)) @inlineCallbacks