Cache scan result. Args: target: Target string modules: Modules that were run result: ScanResult to cache options: Module options used
(
self,
target: str,
modules: list[str],
result: ScanResult,
options: dict[str, Any] = None,
)
| 138 | |
| 139 | logger.debug(f"Cache MISS for {target}") |
| 140 | return None |
| 141 | |
| 142 | def set( |
| 143 | self, |
| 144 | target: str, |
| 145 | modules: list[str], |
| 146 | result: ScanResult, |
| 147 | options: dict[str, Any] = None, |
| 148 | ) -> None: |
| 149 | """Cache scan result. |
| 150 | |
| 151 | Args: |
| 152 | target: Target string |
| 153 | modules: Modules that were run |
| 154 | result: ScanResult to cache |
| 155 | options: Module options used |
| 156 | """ |
| 157 | checksum = self._compute_checksum(target, modules, options) |
| 158 | now = datetime.now(timezone.utc) |
| 159 | |
| 160 | entry = CacheEntry( |
| 161 | target=target, |
| 162 | modules=modules, |
| 163 | result_data=result.model_dump(), |
| 164 | created_at=now, |
| 165 | expires_at=now + timedelta(seconds=self.ttl), |
| 166 | checksum=checksum, |
| 167 | ) |
| 168 | |
| 169 | # Store in memory cache |
| 170 | self._memory_cache[checksum] = entry |
| 171 | |
| 172 | # Store on disk |
| 173 | cache_path = self._get_cache_path(checksum) |
| 174 | try: |
| 175 | with open(cache_path, 'w') as f: |
| 176 | json.dump(entry.model_dump(), f, default=str, indent=2) |
| 177 | logger.debug(f"Cached result for {target} (checksum={checksum[:8]})") |
| 178 | except Exception as e: |
| 179 | logger.error(f"Error writing cache file {cache_path}: {e}") |
| 180 |