Registry / storage / tinycache

tinycache

JSON →
library1.1.2jsnpmunverified

TinyCache is a minimal in-memory cache library for Node.js and the browser (~1.5 KB minified). Current stable version is 1.1.2. It provides a simple key-value store with optional TTL expiration, cache size/memory size tracking, hit/miss counters, and a shared singleton cache. The API is straightforward, inspired by node-cache but smaller in footprint. Heavier alternatives include node-cache or lru-cache which offer more features like LRU eviction and persistent backends. TinyCache is ideal for lightweight caching needs where simplicity and size are priorities.

npm install tinycache
INSTALL
IMPORT
SIG · TINYCACHE
T
tinycache
storagejavascriptv1.1.2
harness data pending
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

TinyCache
import TinyCache from 'tinycache'
const TinyCache = require('tinycache')
ESM import since v1.1.0; default export is the class. In CJS, require returns the same.
shared
import { shared } from 'tinycache'
const shared = require('tinycache').shared
Shared cache instance as named export in ESM; also accessible via TinyCache.shared.
TinyCache (CJS)
const TinyCache = require('tinycache')
const TinyCache = require('tinycache').default
CJS require returns the default export (class). No .default needed.

Demonstrates creation, put/get with TTL, shared cache, deletion, clearing, and stats.

import TinyCache from 'tinycache'; const cache = new TinyCache(); // store a value with default TTL (no expiry) cache.put('key', 'value'); // store a value with TTL of 100 ms cache.put('shortlived', 'bye', 100); // retrieve a value console.log(cache.get('key')); // 'value' // after 200ms, shortlived is auto-removed setTimeout(() => { console.log(cache.get('shortlived')); // undefined }, 200); // shared cache usage import { shared } from 'tinycache'; shared.put('sharedKey', 42); console.log(shared.get('sharedKey')); // 42 // get all keys (returns internal object if no key) console.log(cache.get()); // { key: 'value' } // delete a key cache.del('key'); // clear all cache.clear(); // stats console.log(cache.size); // 0 console.log(cache.hits); // number of successful gets console.log(cache.misses); // number of failed gets
Debug
Known issues
breakingIn version 1.0.0, size, memsize, hits, and misses changed from methods to properties (getters). Using .size() or .memsize() will throw an error.
fix
Access as properties: cache.size, cache.memsize, cache.hits, cache.misses.
affects: >=1.0.0
gotchaCache.put with time=0 or negative values may not expire as expected; time is passed directly to setTimeout.
fix
Use time > 0 for expiry. To store without expiry, omit time argument.
affects: *
gotchaCalling cache.get() with no arguments returns the internal cache object instead of undefined. This can lead to accidental global state exposure.
fix
Always provide a key argument to get(). For debugging, explicitly use cache.get() only when intended.
affects: >=1.1.0
deprecatedThe library has been in maintenance mode since at least 2015; no new features or bug fixes expected.
fix
Consider migrating to actively maintained alternatives like node-cache or lru-cache for new projects.
affects: >=1.1.2
Errors
Common errors & fixes
TypeError: cache.size is not a function
In v1.0.0+, size is a getter, not a method. Calling cache.size() fails.
fix
Use cache.size (no parentheses).
Cannot find module 'js-sizeof'
Missing dependency js-sizeof which is required for memsize calculation.
fix
Run npm install js-sizeof or npm install tinycache (it should install as a dependency).
TinyCache is not a constructor
Using ESM import with CJS-style require in a module environment where default export is not expected.
fix
Use import TinyCache from 'tinycache' in ESM, or const TinyCache = require('tinycache') in CJS.
TypeError: shared.put is not a function
Importing shared as default instead of named export.
fix
Use import { shared } from 'tinycache'.
Upgrade
Version history
1.1.2latest on npm
Audit
Dependencies
js-sizeofoptionalUsed for rough memory size estimation
Agent activity
28 hits · last 30 days
node
26
Resources
tinycache — npm install tinycache · libregistry