Registry / storage / secondary-cache

secondary-cache

JSON →
library2.1.1jsnpmunverified

A two-tier cache library with a fixed-capacity first-level (memory-resident) cache and an LRU second-level cache. Version 2.1.1 is current, stable, and ships TypeScript definitions. Differentiators include configurable expiration, weight-based eviction, and event hooks (before_add, add, update, del). It is lightweight with no external dependencies, suitable for Node.js or browser environments.

npm install secondary-cache
INSTALL
IMPORT
SIG · SECONDARY-CACHE
S
secondary-cache
storagejavascriptv2.1.1
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.

Cache
import { Cache } from 'secondary-cache'
const Cache = require('secondary-cache').Cache
Both ES and CommonJS are supported, but named import is preferred.
LRUCache
import { LRUCache } from 'secondary-cache'
import LRUCache from 'secondary-cache'
LRUCache is a named export, not default.
type CacheOptions
import type { CacheOptions } from 'secondary-cache'
import { CacheOptions } from 'secondary-cache'
CacheOptions is a TypeScript interface, use type-only import when not needing values.
CacheEvent
import { CacheEvent } from 'secondary/cache'
CacheEvent is exported from a subpath. Ensure correct path.

Demonstrates basic usage of Cache with fixed and LRU tiers, expiration, events, and deletion.

import { Cache } from 'secondary-cache'; const cache = new Cache({ fixedCapacity: 10, capacity: 100 }); cache.set('key', 'value'); const val = cache.get('key'); console.log(val); // 'value' // With expiration cache.set('temp', 'data', 5000); // expires in 5 seconds // Listen to events cache.on('add', (key) => console.log(`Added ${key}`)); // Check existence if (cache.has('key')) { cache.delete('key'); }
Debug
Known issues
gotchaIf capacity is set to 0 or not provided, the LRU cache is disabled; only fixed cache operates.
fix
Ensure capacity > 0 to enable secondary LRU caching.
affects: >=2.0.0
gotchaThe 'expires' option is in milliseconds, not seconds.
fix
Multiply seconds by 1000 when providing expiration time.
affects: >=2.0.0
gotchacleanInterval expects seconds, not milliseconds.
fix
Provide cleanInterval in seconds (e.g., 60 for 1 minute).
affects: >=2.0.0
deprecatedMethod 'isExist' and 'isExists' are deprecated in favor of 'has'.
fix
Use cache.has(id) instead.
affects: >=2.0.0
gotchaEvents are emitted synchronously; adding listeners after cache operations will miss events.
fix
Attach event listeners before any cache.set() calls.
affects: >=2.0.0
breakingVersion 2.x changed the options structure: 'capacity' now refers to second-level LRU capacity; 'fixedCapacity' is for first-level.
fix
Update config objects: { fixedCapacity: N, capacity: M }.
affects: <2.0.0
Errors
Common errors & fixes
TypeError: cache.on is not a function
Events are an optional feature; you must check if cache is an EventEmitter.
fix
Cache instances extend EventEmitter; ensure you are using the correct import and the version supports events.
Error: maxWeight exceeded
An item's weight exceeds the configured maxWeight.
fix
Adjust maxWeight or override weightOf function to assign appropriate weights.
TypeError: Cannot read properties of undefined (reading 'set')
Cache instance not properly initialized or imported incorrectly.
fix
Use import { Cache } from 'secondary-cache' and instantiate with 'new Cache()'.
Warning: cleanInterval less than 1, disabling background cleanup.
cleanInterval set to 0 or negative.
fix
Set cleanInterval to a positive integer (in seconds) to enable background cleanup, or omit if not needed.
Upgrade
Version history
2.1.1latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
32 hits · last 30 days
node
30
OpenAI (training)
1
Resources
secondary-cache — npm install secondary-cache · libregistry