Repolode
isaacs

isaacs/node-lru-cache

A JavaScript LRU cache that evicts the least recently used items once a configured max count, size, or TTL is reached.

Stars
5,918
Forks
380
Open issues
15
Last commit
last month

What is it?

lru-cache is a JavaScript cache object that keeps a set number of the most recently accessed items and removes the least recently used ones when new entries push it over the limit. It works in Node.js and in web browsers, and can be installed from npm.

Beyond basic LRU eviction, the cache supports optional limits on total storage size via a size calculation function, per-entry or cache-wide TTL expiration, a dispose callback for cleanup when items are evicted, and an async fetch method with stale-while-revalidate behavior. Non-string keys are supported by object identity. The library is primarily an LRU cache rather than a TTL cache, and it does not prune expired items proactively unless configured to do so.

The package provides TypeScript types, diagnostics channel instrumentation on supported platforms, and a documented performance section that compares trade-offs against simpler alternatives.

Key features

  • 01Evicts least recently used entries when the max count is exceeded.
  • 02Supports total storage limits through a custom size calculation.
  • 03Offers optional TTL expiration for the whole cache or individual entries.
  • 04Provides a dispose callback for cleaning up evicted items.
  • 05Includes an async fetch method with stale-while-revalidate support.
  • 06Ships with TypeScript types.

Use cases

  • Caching results of expensive computations or API calls in a Node.js service.
  • Limiting memory use of an in-process cache by total entry size.
  • Caching fetched data in a browser app with bounded memory.

Quick start

  1. 1

    Install the package with npm

    npm install lru-cache --save
  2. 2

    Import LRUCache and create a cache with a max size

Things to know

  • Not primarily a TTL cache; expired items are not preemptively pruned by default.
  • Set operations are somewhat slower than some alternatives, and the README advises avoiding dispose, size tracking, and TTL features unless needed.

Topics

FAQ

What is node-lru-cache?
A JavaScript LRU cache that evicts the least recently used items once a configured max count, size, or TTL is reached. lru-cache is a JavaScript cache object that keeps a set number of the most recently accessed items and removes the least recently used ones when new entries push it over the limit. It works in Node.js and in web browsers, and can be installed from npm.
Is node-lru-cache free to use? What license does it have?
node-lru-cache is open source under the BlueOak-1.0.0 license. Check the license terms before using it in commercial products.
Is node-lru-cache still maintained?
Updated within 6 months. The last commit to node-lru-cache was on Sep 18, 2026.
What can I use node-lru-cache for?
Caching results of expensive computations or API calls in a Node.js service. Limiting memory use of an in-process cache by total entry size. Caching fetched data in a browser app with bounded memory.
How do I install node-lru-cache?
Install the package with npm: npm install lru-cache --save Import LRUCache and create a cache with a max size
Where can I get node-lru-cache?
The source code and installation instructions are on GitHub: https://github.com/isaacs/node-lru-cache

Summary generated by AI from the project's README. GitHub stats refresh daily.

Related projects