API Reference

node-cache

Simple in-memory key-value cache for Node.js with TTL, clone-on-read and periodic deletion.

Official Documentation ↗
CompatibilityThis documentation covers version node-cache v5.x / Node 18 LTS. Check the official docs for the latest API surface.

Installation

npmnpm install node-cache
pnpmpnpm add node-cache
yarnyarn add node-cache

Methods

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.

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.