Add FileCache with atomic writes and TTL

Aperta
#146 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
4/5
Tempo stimato
3-5 giorni
Idoneità per principianti
52/100
Tipo di issue
Funzionalità
Chiarezza
Specificata chiaramente
Stato di attività
Attiva
Stack tecnologico
python
Ambito
backend

Direzione di ricerca

Start with lettucedetect/detectors/cache.py and the ResponseCache and SyncCacheBase contracts from issues #144 and #145; run uv run --extra dev pytest tests/test_golden_factory_cache_pytest.py -q. Implement and export FileCache with legacy file reading, TTL, locking, and atomic writes, then cover the sync and async behaviors in the acceptance criteria. Done means the cache tests pass, including cancellation, concurrent writes, failed replacement, and construction without disk access.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

ready-for-agent

Parent: #144 · Epic: #126 · Blocked by: #145
Branch from stateless_core; open the PR against it.

Problem

CacheManager creates its directory in the constructor, rewrites the whole file with a plain write_text (a crash mid-write corrupts it), and has no TTL. The lock only covers one process.

Current behavior

  • lettucedetect/detectors/cache.py:17 self.path.parent.mkdir(parents=True, exist_ok=True) in the constructor.
  • cache.py:39 and :46 self.path.write_text(json.dumps(...)) for set and delete.
  • Entries are stored as key -> value; no expiry.

Steps

  1. Add FileCache(path) implementing ResponseCache. The constructor touches no disk; the directory is created on the first write.
  2. Read files written by CacheManager (key to string). An entry with a TTL is stored as {"value": ..., "expires_at": ...} and reads as None once expired.
  3. Write to a temp file in the same directory and os.replace it over the file.
  4. Guard with a lock (one process); document that it is single-process.
  5. FileCache builds on SyncCacheBase (#145), so aget, aset (with ttl_seconds) and adelete run the blocking file work, including the lock and the atomic rename, in a worker thread and return what the sync methods return. An error raised by the file work (for example a failed write) comes out of the awaited call unchanged.
  6. Run the cache contract suite from #145, plus: a failed write (patch os.replace to raise) leaves the previous file readable; a CacheManager-written file reads back; construction touches no disk.

Acceptance criteria

  • Each async method equals its sync pair on FileCache, across the sync/async boundary (written by set, read by aget, and the reverse), and aset with ttl_seconds expires like set.
  • asyncio.gather over many aset calls for different keys leaves valid JSON with every key; a failed write via aset leaves the previous file readable and raises from the await.
  • Cancelling an aset task (and a wait_for timeout) while the write is in progress leaves the file valid JSON holding the old or the new content, never a partial file, no temporary file is left behind once the thread finishes, and later reads and writes work.
  • The file work runs off the event loop's thread, and the loop keeps running while it waits.
  • All items above covered by tests; threads writing different keys lose none.
  • Exported from lettucedetect.detectors; docstring says single process.
  • No detector changes; existing tests pass; follows CONTRIBUTING "Errors and logging".

Start here

uv run --extra dev pytest tests/test_golden_factory_cache_pytest.py -q
Lingua principale
Python
Stelle
615
Fork
60
Merge medio
1g 8h
PR unite (30g)
10

Preparare l'ambiente

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di KRLabsOrg/LettuceDetect

Tutte le issue di KRLabsOrg/LettuceDetect

Issue simili

Altre issue su Python

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.