Registry / storage / safe-memory-cache

safe-memory-cache

JSON →
library3.0.0jsnpmunverified

A lightweight, secure in-memory cache library for Node.js and browsers, currently at version 3.0.0. It limits the number of stored items without using JavaScript's `delete` operator to avoid memory leaks and hidden class changes, and instead uses a bucket-based eviction strategy that removes the oldest bucket when full. It is immune to prototype poisoning attacks via malicious keys like `__proto__` and avoids individual timeout timers per cached item. Main differentiators include defensive coding against prototype pollution, gradual memory freeing, and minimal overhead.

npm install safe-memory-cache
INSTALL
IMPORT
SIG · SAFE-MEMORY-CACHE
S
safe-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.

safeMemoryCache
import { safeMemoryCache } from 'safe-memory-cache'
const safeMemoryCache = require('safe-memory-cache')
ESM-only since v3; no default export.
safeMemoryCache
const { safeMemoryCache } = require('safe-memory-cache')
const safeMemoryCache = require('safe-memory-cache').default
CommonJS require is still supported in v3, but only named import works.
SafeMemoryCache
import type { SafeMemoryCache } from 'safe-memory-cache'
import { SafeMemoryCache } from 'safe-memory-cache'
Type-only import; symbol is not a value export.

Create a cache with a limit of 100 items and a 60-second TTL, then set, get, and clear a key.

import { safeMemoryCache } from 'safe-memory-cache'; const cache = safeMemoryCache({ limit: 100, maxTTL: 60000 }); cache.set('key', 'value'); console.log(cache.get('key')); // 'value' cache.clear(); console.log(cache.get('key')); // undefined
Debug
Known issues
breakingDefault export removed in v3; only named export `safeMemoryCache` is available.
fix
Use `import { safeMemoryCache } from 'safe-memory-cache'` instead of `import safeMemoryCache from ...`.
affects: >=3.0.0
deprecatedOptions `cleanupListener` and `retainUsed` are deprecated and will be removed in v4.
fix
Avoid using `cleanupListener` and `retainUsed`; no direct replacement yet.
affects: >=3.0.0
gotchaCache does not support custom TTL per item; maxTTL is global.
fix
Set maxTTL at creation; items may be removed earlier if cache is full.
affects: all
gotchaThe `limit` option is required; omitting it throws an error.
fix
Always provide `{ limit: number }` when calling `safeMemoryCache`.
affects: all
Errors
Common errors & fixes
Cannot find module 'safe-memory-cache'
Package not installed or import path incorrect.
fix
Run `npm install safe-memory-cache` and ensure import uses correct name.
safeMemoryCache is not a function
Incorrect import; used default import instead of named import in ESM.
fix
Use `import { safeMemoryCache } from 'safe-memory-cache'`.
TypeError: Cannot read properties of undefined (reading 'set')
Cache creation failed due to missing `limit` option.
fix
Call `safeMemoryCache({ limit: 100 })` with a valid limit.
Upgrade
Version history
3.0.0latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
20 hits · last 30 days
node
18
OpenAI (training)
1
Resources
safe-memory-cache — npm install safe-memory-cache · libregistry