make the cache global

This commit is contained in:
Jakob Ketterl 2019-05-30 18:54:45 +02:00
parent 908e3036e0
commit 2121739925

View File

@ -7,40 +7,54 @@ import threading
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
class DmrMetaEnricher(object): class DmrCache(object):
sharedInstance = None
@staticmethod
def getSharedInstance():
if DmrCache.sharedInstance is None:
DmrCache.sharedInstance = DmrCache()
return DmrCache.sharedInstance
def __init__(self): def __init__(self):
self.cache = {} self.cache = {}
self.threads = {}
self.cacheTimeout = timedelta(seconds = 86400) self.cacheTimeout = timedelta(seconds = 86400)
def cacheEntryValid(self, id): def isValid(self, key):
if not id in self.cache: return False if not key in self.cache: return False
entry = self.cache[id] entry = self.cache[key]
return entry["timestamp"] + self.cacheTimeout > datetime.now() return entry["timestamp"] + self.cacheTimeout > datetime.now()
def put(self, key, value):
self.cache[key] = {
"timestamp": datetime.now(),
"data": value
}
def get(self, key):
if not self.isValid(key): return None
return self.cache[key]["data"]
class DmrMetaEnricher(object):
def __init__(self):
self.threads = {}
def downloadRadioIdData(self, id): def downloadRadioIdData(self, id):
cache = DmrCache.getSharedInstance()
try: try:
logger.debug("requesting DMR metadata for id=%s", id) logger.debug("requesting DMR metadata for id=%s", id)
res = request.urlopen("https://www.radioid.net/api/dmr/user/?id={0}".format(id), timeout=5).read() res = request.urlopen("https://www.radioid.net/api/dmr/user/?id={0}".format(id), timeout=5).read()
data = json.loads(res.decode("utf-8")) data = json.loads(res.decode("utf-8"))
self.cache[id] = { cache.put(id, data)
"timestamp": datetime.now(),
"data": data
}
except json.JSONDecodeError: except json.JSONDecodeError:
self.cache[id] = { cache.put(id, None)
"timestamp": datetime.now(),
"data": None
}
del self.threads[id] del self.threads[id]
def enrich(self, meta): def enrich(self, meta):
if not PropertyManager.getSharedInstance()["digital_voice_dmr_id_lookup"]: return None if not PropertyManager.getSharedInstance()["digital_voice_dmr_id_lookup"]: return None
if not "source" in meta: return None if not "source" in meta: return None
id = meta["source"] id = meta["source"]
if not self.cacheEntryValid(id): cache = DmrCache.getSharedInstance()
if not cache.isValid(id):
if not id in self.threads: if not id in self.threads:
self.threads[id] = threading.Thread(target=self.downloadRadioIdData, args=[id]) self.threads[id] = threading.Thread(target=self.downloadRadioIdData, args=[id])
self.threads[id].start() self.threads[id].start()
return None return None
data = self.cache[id]["data"] data = cache.get(id)
if "count" in data and data["count"] > 0 and "results" in data: if "count" in data and data["count"] > 0 and "results" in data:
return data["results"][0] return data["results"][0]
return None return None