返回本地 TLE 缓存目录的状态摘要。 返回 ---- dict 包含以下字段: - ``cache_dir`` (str): 缓存目录绝对路径 - ``entries`` (list): 每个缓存文件的摘要列表 - ``total_groups`` (int): 缓存的星座/分组总数 - ``total_tles`` (int): 缓存的 TLE 条目总数
()
| 322 | # --------------------------------------------------------------------------- |
| 323 | |
| 324 | def get_cache_status() -> dict: |
| 325 | """返回本地 TLE 缓存目录的状态摘要。 |
| 326 | |
| 327 | 返回 |
| 328 | ---- |
| 329 | dict |
| 330 | 包含以下字段: |
| 331 | |
| 332 | - ``cache_dir`` (str): 缓存目录绝对路径 |
| 333 | - ``entries`` (list): 每个缓存文件的摘要列表 |
| 334 | - ``total_groups`` (int): 缓存的星座/分组总数 |
| 335 | - ``total_tles`` (int): 缓存的 TLE 条目总数 |
| 336 | """ |
| 337 | _ensure_cache_dir() |
| 338 | entries = [] |
| 339 | total_tles = 0 |
| 340 | |
| 341 | for meta_file in sorted(CACHE_DIR.glob("*.meta.json")): |
| 342 | try: |
| 343 | meta = json.loads(meta_file.read_text(encoding="utf-8")) |
| 344 | group = meta.get("group", meta_file.stem.replace(".meta", "")) |
| 345 | fetched_at = meta.get("fetched_at", "") |
| 346 | entry_count = int(meta.get("entry_count", 0)) |
| 347 | fetched_at_unix = float(meta.get("fetched_at_unix", 0)) |
| 348 | age_seconds = int(time.time() - fetched_at_unix) |
| 349 | is_fresh = age_seconds < _CACHE_MAX_AGE_SECONDS |
| 350 | |
| 351 | cache_file = _cache_path(group) |
| 352 | file_size = cache_file.stat().st_size if cache_file.exists() else 0 |
| 353 | |
| 354 | entries.append({ |
| 355 | "group": group, |
| 356 | "fetched_at": fetched_at, |
| 357 | "age_seconds": age_seconds, |
| 358 | "is_fresh": is_fresh, |
| 359 | "entry_count": entry_count, |
| 360 | "file_size_bytes": file_size, |
| 361 | "cache_file": str(cache_file), |
| 362 | }) |
| 363 | total_tles += entry_count |
| 364 | except Exception as exc: |
| 365 | logger.debug("读取缓存元数据失败: %s, error=%s", meta_file, exc) |
| 366 | |
| 367 | return { |
| 368 | "cache_dir": str(CACHE_DIR), |
| 369 | "entries": entries, |
| 370 | "total_groups": len(entries), |
| 371 | "total_tles": total_tles, |
| 372 | "cache_ttl_seconds": _CACHE_MAX_AGE_SECONDS, |
| 373 | } |
| 374 | |
| 375 | |
| 376 | def inject_tle_into_constellation( |
no test coverage detected