Skip to the content.

Task 83 — CacheService stampede, eviction, and sliding expiration

Phase: 5 Initiative: 46 Status: done Owner: hercules-coder Started: 2026-08-16 Completed: 2026-08-16 Slug: cache-stampede-eviction

Goal

CacheService имеет три perf-проблемы:

  1. Cache stampede (H9): GetOrSet (CacheService.cs:36-77) non-atomic — между TryGetValue miss и write, N конкурентных запросов к тому же key все запускают factory (LLM/embedding compute). Нет dedup.
  2. O(n log n) eviction: EvictOne (:164-192) — full scan + sort всех keys на каждый insert сверх capacity.
  3. Sliding expiration allocation: :54 — каждый cache hit создаёт new CacheEntry (record with { ExpiresAt = ... }) + dictionary write → GC pressure на hot read path.

Acceptance criteria

Sub-tasks

Scope / Likely files

src/agent/Cache/CacheService.cs, src/agent/Cache/CacheEntry.cs (new, mutable), src/agent/Cache/DistributedCacheService.cs (new, optional), src/agent/Config/AppConfig.cs, src/agent/appsettings.json

Dependencies

Risks / Rollback

Lazy<Task> dedup может deadlock если factory re-enters cache. Stampede dedup добавляет await на concurrent misses. Rollback: вернуть non-atomic GetOrSet (но stampede останется).

Implementation notes (2026-08-16)

Behaviour delivered

Deferred (not done in this tick)

Components

Tests

Validation