whatisgithub

What is node-lru-cache?

isaacs/node-lru-cache — explained in plain English

Analysis updated 2026-06-26

5,873JavaScriptAudience · developerComplexity · 2/5Setup · easy

In one sentence

node-lru-cache is a fast JavaScript in-memory cache that automatically evicts the least recently used items when full, letting you keep frequently needed data close at hand without memory growing without bound.

Mindmap

mindmap
  root((repo))
    What It Does
      In-memory caching
      LRU eviction
      Automatic size limits
    Size Controls
      Max item count
      Max total size
      TTL expiry
    Key Features
      Any key type
      Dispose callback
      Async fetch mode
      Stale-while-revalidate
      Diagnostics tracing
    Environments
      Node.js
      Browser
    Use Cases
      API response cache
      Computed result cache
      Resource pooling
Click or tap to explore — scroll the page freely

Code map

Detail Auto

An interactive map of this repo's files and how they connect — its source is parsed live in your browser. Click Visualize to build it.

filefunction / class

What do people build with it?

USE CASE 1

Cache expensive API or database results in memory with automatic eviction when a count or size limit is reached

USE CASE 2

Set per-item TTLs so cached entries expire automatically after a given number of milliseconds

USE CASE 3

Use the async fetch mode to return stale cached data immediately while refreshing it in the background

USE CASE 4

Register a dispose callback to clean up file handles or connections when items are evicted from the cache

What is it built with?

JavaScriptNode.js

How does it compare?

isaacs/node-lru-cachew37fhy/quantumultxnirmalscaria/le-git-graph
Stars5,8735,8755,878
LanguageJavaScriptJavaScriptJavaScript
Setup difficultyeasymoderateeasy
Complexity2/51/51/5
Audiencedevelopergeneraldeveloper

Figures from each repo's GitHub metadata at analysis time.

How do you get it running?

Difficulty · easy Time to first run · 5min

So what is it?

node-lru-cache is a JavaScript library that gives you a fast in-memory cache with automatic eviction. LRU stands for "least recently used," which describes the eviction rule: when the cache reaches its limit, the item that has not been accessed for the longest time is removed to make room for new entries. You use it when you want to keep frequently needed data close at hand without memory growing without bound. You create a cache by passing a configuration object with at least one size constraint. The max option limits by item count and pre-allocates storage for that many slots when the cache is created, which makes reads and writes fast. The maxSize option limits by total storage, where each item provides its own size when stored, suitable when items vary significantly in size. You can also set a TTL in milliseconds, after which items are treated as absent when requested. The library supports any type of key, not just strings. Object keys work, but lookup requires the exact same object reference, not just an object with matching properties. You can register a dispose callback that fires whenever an item leaves the cache, letting you release external resources like file handles or connections. An async fetch mode lets you supply a function the cache calls when it needs to fill a missing entry. This supports stale-while-revalidate patterns, where a stale value is returned immediately while a fresh one is fetched in the background. Tracing support is built in via Node.js diagnostics channels, allowing monitoring tools to observe cache activity. The library is available on npm and works in both Node.js and browser environments. It does not store undefined values, as that value is used internally to signal a missing key.

Copy-paste prompts

Prompt 1
Show me how to create a node-lru-cache instance limited to 500 items, then add a value, retrieve it, and check what happens after the limit is exceeded.
Prompt 2
Write a Node.js snippet using node-lru-cache with a TTL of 60 seconds so cached API responses automatically expire and are refetched after one minute.
Prompt 3
How do I use node-lru-cache's async fetch option to implement a stale-while-revalidate pattern for a slow database query?
Prompt 4
Give me a node-lru-cache setup that limits by total size in bytes rather than item count, where each cached item reports its own byte size.
Prompt 5
Show me how to register a dispose callback on a node-lru-cache instance that closes a database connection whenever an entry is evicted.

Frequently asked questions

What is node-lru-cache?

node-lru-cache is a fast JavaScript in-memory cache that automatically evicts the least recently used items when full, letting you keep frequently needed data close at hand without memory growing without bound.

What language is node-lru-cache written in?

Mainly JavaScript. The stack also includes JavaScript, Node.js.

How hard is node-lru-cache to set up?

Setup difficulty is rated easy, with roughly 5min to a first successful run.

Who is node-lru-cache for?

Mainly developer.

Open on GitHub → Ask about another repo

This repo across BitVibe Labs

Verify against the repo before relying on details.