# lru-cache — card for AI agents

Efficient least-recently-used cache implementation for Node.js applications.

Category: Utilities · npm package `lru-cache` · latest version 11.5.3 (checked 2026-11-01)
Install: `npm install lru-cache`
License: BlueOak-1.0.0 (permissive)

In your own product (generator, app, hosted service): **yes**. Permissive license: ship it inside your own product, closed or open source, and keep its license and copyright notice.

## Numbers (AgentGid data)
- Gid Score: 70.0 (#387 of all tools, 40 of 408 in its category)
- Used in 47 public projects set up for AI coding agents (0.8%)
- Downloads last week: 830,690,728 (as of 2026-11-24), +39.1% over 4 weeks
- GitHub stars: 5,920 (as of 2026-11-24)
- Last push: 2026-09-18

## Often used with
chalk 72.3%, Anthropic SDK 66.0%, Fuse.js 66.0%, Zod 80.9%, dotenv 61.7%, Express 57.4%

## Links
- AgentGid page: https://agentgid.com/tools/lru-cache/
- Homepage: https://github.com/isaacs/node-lru-cache#readme
- Repository: https://github.com/isaacs/node-lru-cache


## lru-cache: remember recent results in memory

lru-cache is a free JavaScript package that keeps recent results in your app's memory, so a slow lookup does not run again for every visitor. You set how many items it may hold; when it is full, it drops the ones nobody used for the longest time. Items can also expire after a set time.

### Where to use it
- Stop repeating slow lookups: Many visitors ask for the same product price or weather; the first request asks the slow source and the next ones get the saved answer.
- Keep memory use bounded: Your server caches per-user data; with a limit of 500 items it never grows past that, and the least recently used ones go first.
- Answers that expire: Exchange rates or a list of open slots may be reused for a few minutes, then must be looked up fresh.
- Cap the cache by size: Cached pages or images differ a lot in size; give each item a size and a total limit, so a few big items cannot fill memory.
- Clean up what falls out: When an item leaves the cache, a function you give it runs, for example to close a file or a connection held by that item.
- Test expiry with a fake clock: Your tests move a fake clock forward instead of waiting, to check that old answers really expire.

### How to start
- Install lru-cache in a project on Node.js 20, or 22 and newer.

```
npm install lru-cache --save
```

- Load it with import or require; both work.

```
// hybrid module, either works
import { LRUCache } from 'lru-cache'
// or:
const { LRUCache } = require('lru-cache')
```

- Create a cache with options that include a limit (max, ttl or maxSize), then save and read items.

```
const cache = new LRUCache(options)

cache.set('key', 'value')
cache.get('key') // "value"
```


### License
Free for any use, also in paid and closed-source products, under the Blue Oak Model License. If you pass on its files, include the license text or a link to it.

### Good to know
- Expiry is a soft rule: expired items count as missing and are removed when they are next asked for, not right away.
- It cannot store an empty (undefined) value; saving one deletes the item instead.
- Extra features such as size limits, expiry and clean-up functions make it a bit slower; turn them on only when needed.
- Version 11 needs Node.js 20, or 22 and newer.

Checked 2026-11-10 against: https://github.com/isaacs/node-lru-cache/blob/main/README.md, https://github.com/isaacs/node-lru-cache/blob/main/LICENSE.md, https://github.com/isaacs/node-lru-cache/blob/main/package.json, https://github.com/isaacs/node-lru-cache/blob/main/src/index.ts, https://registry.npmjs.org/lru-cache

### Mistakes to avoid
- Don't: import LRU from 'lru-cache'; const cache = new LRU(100) → Do: import { LRUCache } from 'lru-cache'; const cache = new LRUCache({ max: 100 }) (Since version 9 there is no default export, and options are passed as an object.)
- Don't: new LRUCache({}) with no limit → Do: Set max (best for speed), maxSize or ttl (An unbounded cache is refused; in our run new LRUCache({}) threw "At least one of max, maxSize, or ttl is required".)
- Don't: cache.set(key, undefined) to remember "nothing found" → Do: Store a marker of your own, such as Symbol('none'), and turn it back into undefined when reading (Setting undefined deletes the key, so has(key) returns false (our run printed false).)
- Don't: A test fake clock that starts at 0 (let myClockTime = 0) → Do: Start the fake clock above 0 and set ttlResolution 0 (In our run an item saved at time 0 never expired, and the cache rechecks the time at most once per millisecond by default.)
- Don't: Using a fresh copy of an object as the key to read an item → Do: Keep and reuse the same key object, or use a string key such as an id (Object keys match by identity, not by content.)

### Questions
- Does it work with require() and with import? Yes. The package ships both forms, and since version 9 you take the named LRUCache export.
- Can I use objects as keys? Yes, but only the very same object finds the item again; a copy with the same contents does not.
- Do expired items disappear on their own? Not by default. They count as missing and are deleted when asked for; the ttlAutopurge option removes them early.

---
AgentGid's catalog is curated: every entry was checked against the maker's official pages (registry, repository, license and pricing pages) on the date shown; a license value shows the quote it stands on wherever AgentGid has one. Usage numbers come from AgentGid's own scan of public repositories set up for AI coding agents. Claims by makers are reported, not tested by AgentGid.
