cache_bounds.py 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. """Bounds for long-lived in-process caches that would otherwise grow for the life of a gateway.
  2. Each helper sheds only what no reader can use (expired entries) or what nobody has touched in the
  3. longest time (least recently used, past a count far above any real working set). Callers hold
  4. their own lock where the cache is shared across threads; snapshots are taken before iterating.
  5. """
  6. from typing import Any, Callable
  7. def drop_expired(values: dict, stamps: dict, ttl: float, now: float) -> None:
  8. """Pop every ``values`` key whose stamp (missing = 0, like the readers) is ``ttl`` old."""
  9. for key in [k for k in list(values) if now - stamps.get(k, 0) >= ttl]:
  10. values.pop(key, None)
  11. stamps.pop(key, None)
  12. def evict_oldest(cache: dict, key: Any, stamp_of: Callable[[Any], float], limit: int, *siblings: dict) -> None:
  13. """Make room for a NEW ``key``: past ``limit``, drop the oldest-stamped key from ``cache`` and every sibling."""
  14. if key in cache or len(cache) < limit:
  15. return
  16. oldest = min(list(cache.items()), key=lambda item: stamp_of(item[1]))[0]
  17. for table in (cache, *siblings):
  18. table.pop(oldest, None)
  19. def publish_lru(table: dict, key: Any, value: Any, limit: int, *siblings: dict) -> list:
  20. """Store ``key`` as most recently used; past ``limit``, drop the least recently used keys from
  21. ``table`` and every sibling. Returns the evicted keys (for companions that are not dicts)."""
  22. table.pop(key, None)
  23. table[key] = value
  24. evicted = []
  25. while len(table) > limit:
  26. stale = next(iter(table))
  27. evicted.append(stale)
  28. for scoped in (table, *siblings):
  29. scoped.pop(stale, None)
  30. return evicted
  31. def live_loop_table(tables: dict, loop: Any, lock: Any) -> dict:
  32. """``loop``'s table in ``tables`` (keyed by event loop object); tables of loops closed since the last
  33. call are dropped, since a closed loop never runs again. Keys are strong on purpose: an asyncio
  34. primitive that ever made a caller wait binds its loop, so a weak key held by its own value never dies."""
  35. with lock:
  36. for stale in [other for other in tables if other is not loop and other.is_closed()]:
  37. del tables[stale]
  38. return tables.setdefault(loop, {})