Skip to content

fix(cache): evict least recently used entries in MemoryCacheStore - #5869

Merged
mcollina merged 1 commit into
nodejs:mainfrom
jasnell:jasnell/fixup-memory-cache-store-eviction
Sep 24, 2026
Merged

mcollina merged 1 commit into
nodejs:mainfrom
jasnell:jasnell/fixup-memory-cache-store-eviction

Conversation

@jasnell

@jasnell jasnell commented Sep 23, 2026 •

Copy link
Copy Markdown
Member

This relates to...

Cache

Rationale

Since #5737, overflowing maxSize or maxCount evicted ceil(n/2) entries from every key. With one entry per key, the common case, that wiped the whole cache, including the entry just written.

Found while performing an audit of the current implementation.

Changes

Evict least recently used entries store-wide until the store is at half of its limits, as documented, keeping the entry just written unless it alone exceeds a limit. Writes and get() hits mark a key as recently used.

Also replace an expired entry on rewrite instead of adding a duplicate that is never evicted until the store overflows.

Features

N/A

Bug Fixes

N/A

Breaking Changes and Deprecations

N/A

Status

@jasnell

jasnell commented Sep 23, 2026

Copy link
Copy Markdown
Member Author

@mcollina ... this updates the changes you made in #5737 ... worth taking a look to ensure it still addresses the original issue that was fixing.

@jasnell
jasnell requested review from mcollina and ronag September 23, 2026 04:43
Since nodejs#5737, overflowing maxSize or maxCount evicted ceil(n/2) entries
from every key. With one entry per key, the common case, that wiped the
whole cache, including the entry just written.

Evict least recently used entries store-wide until the store is at half
of its limits, as documented, keeping the entry just written unless it
alone exceeds a limit. Writes and get() hits mark a key as recently used.

Also replace an expired entry on rewrite instead of adding a duplicate
that is never evicted until the store overflows.

Signed-off-by: James M Snell <jasnell@gmail.com>
@jasnell
jasnell force-pushed the jasnell/fixup-memory-cache-store-eviction branch from b65c898 to c04f4f1 Compare September 23, 2026 04:47
@codecov-commenter

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 93.85%. Comparing base (77d7b3f) to head (c04f4f1).

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #5869   +/-   ##
=======================================
  Coverage   93.85%   93.85%           
=======================================
  Files         110      110           
  Lines       39879    39914   +35     
=======================================
+ Hits        37428    37463   +35     
  Misses       2451     2451           

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@mcollina mcollina left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

lgtm

@mcollina
mcollina merged commit 47c2493 into nodejs:main Sep 24, 2026
38 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.

4 participants