cache policy lists and increase general cache time

This commit is contained in:
Nils Büchner 2024-09-15 00:19:00 +02:00
parent a28942aadd
commit 7580c687e2

View file

@ -28,7 +28,7 @@ class InviteChecker:
self.use_allowlist = self.config.use_allowlist self.use_allowlist = self.config.use_allowlist
self.use_blocklist = self.config.use_blocklist self.use_blocklist = self.config.use_blocklist
self.cache_expiry_time = 60 self.cache_expiry_time = 300
self.cache_timestamp = 0 self.cache_timestamp = 0
self.blocklist = set() self.blocklist = set()
self.allowlist = set() self.allowlist = set()
@ -120,9 +120,9 @@ class InviteChecker:
self.blocklist = set(json_data.get('blocklist', [])) self.blocklist = set(json_data.get('blocklist', []))
self.allowlist = set(json_data.get('allowlist', [])) 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() 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() self.blocklist_room_ids = set()
for room_entry in json_data.get('blocklist_rooms', []): for room_entry in json_data.get('blocklist_rooms', []):
@ -137,6 +137,9 @@ class InviteChecker:
else: else:
logger.error(f"Failed to blocklist room: {room_entry}") 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.") logger.info(f"Updated blocklist with {len(self.blocklist)} entries and {len(self.blocklist_room_ids)} room IDs.")
else: else:
logger.error("Failed to update allowlist/blocklist due to missing JSON data.") logger.error("Failed to update allowlist/blocklist due to missing JSON data.")
@ -161,6 +164,7 @@ class InviteChecker:
def get_blocklist_allowlist(self): def get_blocklist_allowlist(self):
current_time = time.time() current_time = time.time()
# Update cache if expired
if current_time - self.cache_timestamp > self.cache_expiry_time: if current_time - self.cache_timestamp > self.cache_expiry_time:
yield self.update_blocklist_allowlist() yield self.update_blocklist_allowlist()
@ -168,6 +172,7 @@ class InviteChecker:
logger.info("Skipping allowlist/blocklist checks because of previous JSON fetch failure.") logger.info("Skipping allowlist/blocklist checks because of previous JSON fetch failure.")
returnValue((set(), set(), set())) returnValue((set(), set(), set()))
# Return cached blocklist, allowlist, and blocklist room IDs
returnValue((self.blocklist, self.allowlist, self.blocklist_room_ids)) returnValue((self.blocklist, self.allowlist, self.blocklist_room_ids))
@inlineCallbacks @inlineCallbacks