Refactor of caching & data storage part 7 #118

This commit is contained in:
Ian Renton
2026-08-01 10:34:20 +01:00
parent b7d2cf0fdb
commit 41aecc6007
10 changed files with 121 additions and 105 deletions
+12
View File
@@ -1,4 +1,5 @@
import logging
import re
from pathlib import Path
import diskcache
@@ -23,6 +24,7 @@ class DataStore:
self.spots = None
self.callsigns = None
self.dxcc_data = None
self.dxcc_lookup_by_call_regex = []
self.sigrefs = None
self.status_data = None
self._status = None
@@ -46,6 +48,7 @@ class DataStore:
# Standard disk cache for static reference and SIG ref data. Separate provider threads will repopulate these on
# a regular basis but there's no need for a TTL since old data is better than no data.
self.dxcc_data = diskcache.Cache(CACHE_DIR + "dxcc_data")
self.regenerateCallRegexToDXCCEntityMap()
# For SIG reference data specifically, we need to key on both SIG *and* reference, and trying to do two layers
# of dict in diskcache absolutely destroys performance with unpickling huge dicts, so we have an ugly "SIG:ref"
@@ -72,6 +75,15 @@ class DataStore:
snapshot_interval_sec=self._SPOT_ALERT_SNAPSHOT_INTERVAL_SEC)
logging.info(f"Loaded %d alerts from a previous run.", len(self.alerts.keys()))
def regenerateCallRegexToDXCCEntityMap(self):
"""DXCC entity data from K0SWE includes a regex which we can use to match a callsign, and determine which DXCC
entity it belongs to. But getting every DXCC entity data object out of DiskCache, iterating, compiling its regex
and testing the callsign every time is expensive. So instead we build a separate in-memory lookup of compiled
regex against DXCC entity code, as a list of tuples we can iterate through."""
for entry in [DATA_STORE.dxcc_data[key] for key in DATA_STORE.dxcc_data]:
self.dxcc_lookup_by_call_regex.append((re.compile(entry["prefixRegex"]), entry["entityCode"]))
def close(self):
self.spots.close()
self.alerts.close()