Skip to content

refactor(ai): three in-process caches use cachetools - #2611

Merged
dan-petty merged 2 commits into
mainfrom
refactor/1993-cachetools-caches
Oct 29, 2026
Merged

dan-petty merged 2 commits into
mainfrom
refactor/1993-cachetools-caches

Conversation

@dan-petty

Copy link
Copy Markdown
Owner

The embedding LRU cache (_EmbeddingLRUCache) and the webhook replay guard (DeliveryLRUCache) kept their keys in an OrderedDict with their own eviction. They now keep them in a cachetools.LRUCache of the same size, under the lock each one has. Their interfaces, the hit and miss counters, and the replay refusal stay the same.

The memory cache of CloudflareRadarClient was a dict with no bound and no expiry, so a long-running process kept each record and never asked Radar again. It is now a cachetools.TTLCache of at most 1,000 records that expire after the client's threat-intel TTL (valkey.threat_intel_ttl, 24 h by default). It reads time through the clock seam, so its test moves time with the clock fixture.

cachetools 7.1.8 is already a pinned dependency. The OrderedDict of the OSV client stays for #1146.

Closes #1993

Change: openspec/changes/archive/2026-10-29-1993-cachetools-caches/

The embedding LRU cache and the webhook replay guard stored their keys
in an OrderedDict with their own eviction. They now store them in a
cachetools LRUCache of the same size, under the lock each one has.

The memory cache of the Cloudflare Radar client was a dict with no bound
and no expiry. It is now a cachetools TTLCache of at most 1,000 records,
whose entries expire after the client's threat-intel TTL. It reads time
through the clock seam, so a test moves time with the clock fixture.
@dan-petty

Copy link
Copy Markdown
Owner Author

Test evidence:

  • uv run devops ci fail-to-pass --base origin/main: 2 failed on base. test_a_radar_record_expires_from_the_memory_tier_after_the_ttl (assert (1, 1) == (1, 2)) and test_a_full_radar_memory_tier_drops_the_least_recently_used_record (assert 1 == 2). The new replay-guard test pins today's behavior and passes on base and branch.
  • On the branch after the rebase onto 69330051: tests/test_security_threat_intel.py, tests/test_rag_embeddings.py and tests/test_server_service.py, 155 passed. Before the rebase, those three plus tests/test_http_egress.py: 369 passed; tests/invariants and tests/test_architectural_invariants.py: 183 passed. mypy passes on the three changed source files.
  • Checked in the installed cachetools 7.1.8: LRUCache.get and an overwrite refresh recency, an insert at maxsize evicts the least recently used key, and a TTLCache key reads as absent once timer() reaches its insert time plus ttl.

Pending a person: review the diff in tuicr (tuicr -r origin/main..HEAD).

@dan-petty
dan-petty marked this pull request as ready for review October 29, 2026 03:16
@dan-petty
dan-petty merged commit 0c30d2a into main Oct 29, 2026
16 checks passed
@dan-petty
dan-petty deleted the refactor/1993-cachetools-caches branch October 29, 2026 03:16
dan-petty pushed a commit that referenced this pull request Oct 29, 2026
🤖 I have created a release *beep* *boop*
---


##
[0.14.0](v0.13.2...v0.14.0)
(2026-10-29)


### Features

* **argo:** apps status checks each pinned image against its registry
([#2605](#2605))
([6933005](6933005)),
closes [#1997](#1997)
* **k8s:** push the Argo CD notifier App's IDs and key
([#2608](#2608))
([a408dee](a408dee))
* **release:** an urgent fix merges the release pull request
([#2596](#2596))
([20e8ac4](20e8ac4))
* **roadmap:** a webhook event runs intake of its one issue
([#2607](#2607))
([cf9f02c](cf9f02c)),
closes [#2009](#2009)
* **roadmap:** devops roadmap intake-eval scores duplicates
([#2601](#2601))
([22146d6](22146d6))


### Code Refactoring

* **ai:** three in-process caches use cachetools
([#2611](#2611))
([0c30d2a](0c30d2a)),
closes [#1993](#1993)

---
This PR was generated with [Release
Please](https://github.com/googleapis/release-please). See
[documentation](https://github.com/googleapis/release-please#release-please).

Co-authored-by: dan-petty-release[bot] <346998822+dan-petty-release[bot]@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

refactor(ai): the embedding LRU cache, the webhook replay guard and the Radar memory cache use cachetools, with a bound and a TTL

1 participant