node-cache
Simple in-memory key-value cache for Node.js with TTL, clone-on-read and periodic deletion.
Official Documentation ↗node-cache v5.x / Node 18 LTS. Check the official docs for the latest API surface.Installation
npm install node-cachepnpm add node-cacheyarn add node-cacheMethods
No methods match this filter.
Overview
node-cache is a small in-memory store with second-based TTLs and optional cloning. It is a straightforward choice when you want expiry without pulling in a larger caching library.
Core Methods
set, get, del and ttl cover the common operations, while getStats, keys and mget help with introspection and batch reads. Events such as set, del and expired report lifecycle changes.
Usage Notes
Set a stdTTL for the whole cache and override per key where needed. Use mget for batch reads and listen to expire events to release related resources.
Common Pitfalls
Without stdTTL entries never expire, so memory grows unbounded. The store is process local and lost on restart. Returning huge values from get stresses memory, so cache summaries where possible.
Errors
The following points are quoted verbatim from the official documentation. 来源:https://cdn.jsdelivr.net/gh/node-cache/node-cache@latest/README.md · 取证方式:HTTP GET 官方 README/文档页(HTML 去标签) · 抓取时间:2026-11-07T13:51:08.940Z. Any error type, throw condition or recovery step not stated there is left unspecified; this page does not infer exception classes or return values.
- If a new item is set and the cache is full, an error is thrown and the key will not be saved in the cache. (https://cdn.jsdelivr.net/gh/node-cache/node-cache@latest/README.md)
- See #49| |3.1.0|2016-01-29|Added option errorOnMissing to throw/callback an error o a miss during a .get( “key” ). (https://cdn.jsdelivr.net/gh/node-cache/node-cache@latest/README.md)
- Thanks to Qix for the pull.| |2.1.0|2015-04-17|Changed get miss to return undefined instead of an error. (https://cdn.jsdelivr.net/gh/node-cache/node-cache@latest/README.md)
FAQ
- What is the default TTL?
- By default values never expire unless you configure a stdTTL in the constructor or pass a per-key TTL. Set stdTTL to avoid stale data lingering forever.
- Does node-cache clone stored values?
- It clones object values depending on the clone option, which is on in newer versions. Cloning protects cached objects from external mutation at some CPU cost.
- How are expired keys removed?
- A periodic timer checks for expired entries using checkperiod. You can also call delete or flushAll, and listen to the expired and del events for cleanup hooks.
Report a problem with this API docOpens a pre-filled issue on GitHub.