Skip to content

Add cost-aware cache eviction policy#94

Merged
luis-gasparschroeder merged 3 commits into
vcache-project:masterfrom
Safaael25:feat/cost-aware-eviction
Jul 18, 2026
Merged

Add cost-aware cache eviction policy#94
luis-gasparschroeder merged 3 commits into
vcache-project:masterfrom
Safaael25:feat/cost-aware-eviction

Conversation

@Safaael25

Copy link
Copy Markdown
Contributor

Cost-Aware Eviction Policy

Adds a new CostAwareEvictionPolicy that factors in how expensive a cached response was to generate (e.g. LLM inference latency) when deciding what to evict — not just recency like LRU.

Why: Existing policies (FIFO/LRU/MRU) treat every cache miss as equally costly to replace. In practice, some cached responses come from a slow/expensive model call and others are cheap — evicting the expensive ones just because they're stale wastes the work that produced them. This policy protects expensive-to-regenerate entries while still evicting cheap, stale ones.

How it works: each item gets a priority score blending normalized staleness and normalized (inverted) cost, weighted by a configurable cost_weight (0 = pure LRU, 1 = cost-only). Items with unknown cost default to "free," so it degrades gracefully to LRU with no special-casing.

What changed:

EmbeddingMetadataObj gains a cost field
Cache.add() / EmbeddingStore.add_embedding() thread cost through
VerifiedDecisionPolicy now measures real LLM inference latency and passes it as the cost
New CostAwareEvictionPolicy strategy + tests + README docs

Protects cached responses that were expensive to generate (e.g. slow
LLM calls) from eviction, blending generation cost with staleness
instead of relying on recency alone like LRU.
@Safaael25

Copy link
Copy Markdown
Contributor Author

@luis-gasparschroeder luis-gasparschroeder left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for contributing to vCache! The cost-aware policy makes sense; before merging, please

  1. extract normalization and priority calculation into helper methods,
  2. use min-max normalization, and
  3. replace time.time() with time.perf_counter(). Reason being that perf_counter() is monotonic and designed for measuring elapsed durations, whereas system clock adjustments can make time.time() produce inaccurate or even negative latency values.

- Extract normalization and priority calculation into helper methods
  (_staleness_seconds, _min_max_normalize, _compute_priority).
- Switch from max-only scaling to true min-max normalization, so the
  freshest/cheapest item in the cache maps to 0 instead of just the
  most stale/expensive item mapping to 1.
- Measure inference latency with time.perf_counter() instead of
  time.time(), since perf_counter is monotonic and immune to system
  clock adjustments that could otherwise produce inaccurate or
  negative latency values.

@Safaael25 Safaael25 left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@luis-gasparschroeder
Done!! thanks for the review. Extracted _staleness_seconds, _min_max_normalize, and _compute_priority as helpers, switched to true min-max normalization (min→0, max→1) with the README updated to match, and switched to time.perf_counter() for the three latency measurements this PR added. All existing tests still pass unchanged.

@luis-gasparschroeder luis-gasparschroeder left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Safaael25, looks good, and thank you for addressing the comments. Please run the ruff formatter and we are good to merge

@Safaael25

Copy link
Copy Markdown
Contributor Author

@luis-gasparschroeder Done! I ran the Ruff formatter. Thanks for the review! If there's anything else that needs to be changed or improved, please let me know and I'll be happy to work on it.

@luis-gasparschroeder luis-gasparschroeder left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good, thanks!

@luis-gasparschroeder
luis-gasparschroeder merged commit 29ce390 into vcache-project:master Jul 18, 2026
2 checks passed
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.

2 participants