Retrieve cached scan result if available and not expired. Args: target: Target string modules: Modules that were run options: Module options used Returns: Cached ScanResult if available and valid, None otherwise
(
self,
target: str,
modules: list[str],
options: dict[str, Any] = None,
)
| 86 | return self.cache_dir / f"{checksum}.json" |
| 87 | |
| 88 | def get( |
| 89 | self, |
| 90 | target: str, |
| 91 | modules: list[str], |
| 92 | options: dict[str, Any] = None, |
| 93 | ) -> ScanResult | None: |
| 94 | """Retrieve cached scan result if available and not expired. |
| 95 | |
| 96 | Args: |
| 97 | target: Target string |
| 98 | modules: Modules that were run |
| 99 | options: Module options used |
| 100 | |
| 101 | Returns: |
| 102 | Cached ScanResult if available and valid, None otherwise |
| 103 | """ |
| 104 | checksum = self._compute_checksum(target, modules, options) |
| 105 | |
| 106 | # Check memory cache first |
| 107 | if checksum in self._memory_cache: |
| 108 | entry = self._memory_cache[checksum] |
| 109 | if not entry.is_expired(): |
| 110 | logger.debug(f"Cache HIT (memory) for {target}") |
| 111 | return ScanResult(**entry.result_data) |
| 112 | else: |
| 113 | # Remove expired entry |
| 114 | del self._memory_cache[checksum] |
| 115 | logger.debug(f"Cache entry expired for {target}") |
| 116 | |
| 117 | # Check disk cache |
| 118 | cache_path = self._get_cache_path(checksum) |
| 119 | if cache_path.exists(): |
| 120 | try: |
| 121 | with open(cache_path) as f: |
| 122 | data = json.load(f) |
| 123 | entry = CacheEntry(**data) |
| 124 | |
| 125 | if not entry.is_expired(): |
| 126 | logger.debug(f"Cache HIT (disk) for {target}") |
| 127 | # Restore to memory cache |
| 128 | self._memory_cache[checksum] = entry |
| 129 | return ScanResult(**entry.result_data) |
| 130 | else: |
| 131 | # Remove expired file |
| 132 | cache_path.unlink() |
| 133 | logger.debug(f"Deleted expired cache file for {target}") |
| 134 | except Exception as e: |
| 135 | logger.warning(f"Error reading cache file {cache_path}: {e}") |
| 136 | |
| 137 | logger.debug(f"Cache MISS for {target}") |
| 138 | return None |
| 139 | |
| 140 | def set( |
| 141 | self, |