Registry / storage / fast-memory-cache

fast-memory-cache

JSON →
library3.0.0jsnpmunverified

A lightweight, zero-dependency in-memory cache for Node.js with TypeScript support. Version 3.0.0 provides a simple API for get, set, delete, and clear operations, with optional TTL (time-to-live) in seconds. It is ideal for caching API responses, session data, or memoizing function results. Unlike heavier alternatives like node-cache, it has no serialization overhead and no external dependencies, making it extremely fast and memory efficient.

npm install fast-memory-cache
INSTALL
IMPORT
SIG · FAST-MEMORY-CACHE
F
fast-memory-cache
storagejavascriptv3.0.0
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.

MemoryCache
import MemoryCache from 'fast-memory-cache'
const { MemoryCache } = require('fast-memory-cache')
Default export is a class. CommonJS users must use default import via require('fast-memory-cache').default or as shown in README.
MemoryCache
const MemoryCache = require('fast-memory-cache')
import { MemoryCache } from 'fast-memory-cache'
Named import not supported; default export only.
MemoryCache
import MemoryCache from 'fast-memory-cache'
import default as MemoryCache from 'fast-memory-cache'
Direct default import as shown is correct.
MemoryCache
import MemoryCache from 'fast-memory-cache'
import { default as MemoryCache } from 'fast-memory-cache'
Unnecessary; default import works directly.
MemoryCache
const MemoryCache = require('fast-memory-cache').default
const MemoryCache = require('fast-memory-cache')
In CJS with TypeScript or bundlers, the default might be under .default. Check your setup.

Demonstrates instantiation, set/get with and without TTL, delete, and clear operations in TypeScript.

import MemoryCache from 'fast-memory-cache'; const cache = new MemoryCache(); // Store a value with no expiration cache.set('greeting', 'Hello, world!'); console.log(cache.get('greeting')); // 'Hello, world!' // Store with TTL (5 seconds) cache.set('ephemeral', 'I will vanish', 5); console.log(cache.get('ephemeral')); // 'I will vanish' // Wait 6 seconds and check (simulate async) setTimeout(() => { console.log(cache.get('ephemeral')); // undefined (expired) }, 6000); // Delete a key cache.delete('greeting'); console.log(cache.get('greeting')); // undefined // Clear all cache cache.set('a', 1); cache.set('b', 2); cache.clear(); console.log(cache.get('a')); // undefined console.log(cache.get('b')); // undefined
Debug
Known issues
gotchaTTL is in seconds, not milliseconds.
fix
Always pass TTL in seconds (e.g., 300 for 5 minutes). If you pass milliseconds (e.g., 5000), the value will expire after ~83 minutes instead of 5 seconds.
affects: >=0.0.0
gotchaset() with expireTime: 0 is treated as no expiration (same as omitting).
fix
If you want a value to expire immediately, do not use this library—consider using a TTL of 1 second and immediately calling delete, or use a different approach.
affects: >=0.0.0
gotchaget() returns undefined for expired keys, not null.
fix
Always check against undefined, not falsy. If your values can be null, treat them carefully.
affects: >=0.0.0
gotchaThe cache does not support complex keys (objects, arrays). Only strings are accepted.
fix
Convert non-string keys to strings before calling set/get (e.g., JSON.stringify).
affects: >=0.0.0
Errors
Common errors & fixes
Cannot find module 'fast-memory-cache' or its corresponding type declarations.
Missing installation or incorrect import path.
fix
Run 'npm install fast-memory-cache' and ensure your tsconfig.json includes 'node_modules' in moduleResolution.
TypeError: MemoryCache is not a constructor
Using named import { MemoryCache } instead of default import.
fix
Use 'import MemoryCache from 'fast-memory-cache'' or in CJS: 'const MemoryCache = require('fast-memory-cache').default'.
Property 'set' does not exist on type 'typeof import(...)'
Importing the module itself instead of an instance.
fix
Instantiate with 'new MemoryCache()' before calling methods.
Upgrade
Version history
3.0.0latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
25 hits · last 30 days
node
22
Amazon
1
OpenAI (training)
1
Resources
fast-memory-cache — npm install fast-memory-cache · libregistry