Manages caching of scan results.
| 34 | # than repaired, which also keeps __eq__/__hash__ consistent (pydantic |
| 35 | # supplies field equality). |
| 36 | |
| 37 | |
| 38 | class ScanCache: |
| 39 | """Manages caching of scan results.""" |
| 40 | |
| 41 | DEFAULT_TTL = 24 * 3600 # 24 hours in seconds |
| 42 | |
| 43 | def __init__(self, ttl_seconds: int = DEFAULT_TTL): |
| 44 | """Initialize cache. |
| 45 | |
| 46 | Args: |
| 47 | ttl_seconds: Time-to-live for cache entries in seconds |
| 48 | """ |
| 49 | self.ttl = ttl_seconds |
| 50 | self.cache_dir = get_settings().data_dir / "cache" |
| 51 | self.cache_dir.mkdir(parents=True, exist_ok=True) |
| 52 | self._memory_cache: dict[str, CacheEntry] = {} |
| 53 | logger.info(f"Initialized scan cache with TTL={ttl_seconds}s at {self.cache_dir}") |
| 54 | |
| 55 | @staticmethod |
| 56 | def _compute_checksum( |
| 57 | target: str, |
| 58 | modules: list[str], |
| 59 | options: dict[str, Any] = None, |
| 60 | ) -> str: |
| 61 | """Compute checksum for cache key. |
| 62 | |
| 63 | Args: |
| 64 | target: Target string |
| 65 | modules: Modules to run |
| 66 | options: Module options |
| 67 | |
| 68 | Returns: |
| 69 | Hex checksum string |
| 70 | """ |
| 71 | key_data = { |
| 72 | "target": target, |
| 73 | "modules": sorted(modules), |
| 74 | "options": options or {}, |
| 75 | } |
| 76 | key_str = json.dumps(key_data, sort_keys=True) |
| 77 | return hashlib.sha256(key_str.encode()).hexdigest() |
| 78 | |
| 79 | def _get_cache_path(self, checksum: str) -> Path: |
| 80 | """Get cache file path for checksum. |
| 81 | |
| 82 | Args: |
| 83 | checksum: Cache checksum |
| 84 | |
| 85 | Returns: |
| 86 | Path to cache file |
| 87 | """ |
| 88 | return self.cache_dir / f"{checksum}.json" |
| 89 | |
| 90 | def get( |
| 91 | self, |
| 92 | target: str, |
| 93 | modules: list[str], |
no outgoing calls