MCPcopy Create free account
hub / github.com/PierreGode/Ragnar / load_threat_cache

Method load_threat_cache

threat_intelligence.py:278–320  ·  view source on GitHub ↗

Load cached threat intelligence data

(self)

Source from the content-addressed store, hash-verified

276 self.logger.error(f"Error saving threat intelligence configuration: {e}")
277
278 def load_threat_cache(self):
279 """Load cached threat intelligence data"""
280 try:
281 if os.path.exists(self.threat_cache_file):
282 # Check if file is empty before trying to parse JSON
283 if os.path.getsize(self.threat_cache_file) == 0:
284 self.logger.debug("Threat cache file is empty, initializing with empty cache")
285 self.threat_cache = {}
286 return
287
288 with open(self.threat_cache_file, 'r') as f:
289 content = f.read().strip()
290 if not content:
291 self.logger.debug("Threat cache file contains no content, initializing with empty cache")
292 self.threat_cache = {}
293 return
294
295 cache_data = json.loads(content)
296 if not isinstance(cache_data, dict):
297 self.logger.warning("Threat cache file contains invalid data format, initializing with empty cache")
298 self.threat_cache = {}
299 return
300
301 self.threat_cache = cache_data
302
303 self.logger.info(f"Loaded threat intelligence cache with {len(self.threat_cache)} entries")
304 else:
305 self.logger.debug("Threat cache file does not exist, initializing with empty cache")
306 self.threat_cache = {}
307 except json.JSONDecodeError as e:
308 self.logger.error(f"Invalid JSON in threat cache file: {e}")
309 self.logger.info("Backing up corrupted cache file and initializing with empty cache")
310 try:
311 # Backup corrupted file
312 backup_file = f"{self.threat_cache_file}.backup.{int(time.time())}"
313 os.rename(self.threat_cache_file, backup_file)
314 self.logger.info(f"Corrupted cache file backed up to: {backup_file}")
315 except Exception as backup_error:
316 self.logger.error(f"Failed to backup corrupted cache file: {backup_error}")
317 self.threat_cache = {}
318 except Exception as e:
319 self.logger.error(f"Error loading threat intelligence cache: {e}")
320 self.threat_cache = {}
321
322 def save_threat_cache(self):
323 """Save threat intelligence cache"""

Callers 2

set_storage_rootMethod · 0.95

Calls 6

debugMethod · 0.80
warningMethod · 0.80
infoMethod · 0.80
errorMethod · 0.80
readMethod · 0.45
renameMethod · 0.45

Tested by

no test coverage detected