Registry / storage / cache-ts

cache-ts

JSON →
library1.0.0jsnpmunverified

cache-ts v1.0.0 is a lightweight, TypeScript-native cache implementation that extends the built-in Map class with automatic capacity control and LRU-like eviction. It provides a simple constructor taking a maximum capacity, and standard set/get operations that promote recently accessed entries. Unlike heavier caching libraries, it has zero dependencies and ships inline TypeScript declarations. Currently stable with low release cadence; suitable for small to medium caching needs where simplicity and type safety are prioritized over advanced features like TTL or persistence.

npm install cache-ts
INSTALL
IMPORT
SIG · CACHE-TS
C
cache-ts
storagejavascriptv1.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.

Cache
import { Cache } from 'cache-ts'
const Cache = require('cache-ts')
ESM-only; default export not available
Cache
import type { Cache } from 'cache-ts'
TypeScript type import is supported
Cache
const { Cache } = require('cache-ts')
const cache = require('cache-ts')
CJS destructuring works in Node, but ESM is preferred

Demonstrates cache creation, setting entries, access-triggered promotion, and automatic eviction when capacity is exceeded.

import { Cache } from 'cache-ts'; // Create a cache with capacity 5 const cache = new Cache<string, number>(5); // Add entries cache.set('a', 1); cache.set('b', 2); cache.set('c', 3); cache.set('d', 4); cache.set('e', 5); // Access 'a' to promote it cache.get('a'); // Adding another entry evicts the least recently used (which is 'b') cache.set('f', 6); console.log(cache.get('b')); // undefined (evicted) console.log(cache.get('a')); // 1 (still present)
Debug
Known issues
gotchacache.get(key) returns undefined for missing keys — distinct from 'value undefined' explicitly set
fix
Use cache.has(key) to check existence before .get() if you need to distinguish missing from undefined values.
affects: >=1.0.0
gotchaCache extends Map, so Map.prototype methods (e.g., forEach, keys, values) are inherited but may not preserve custom eviction order
fix
Iterate using cache.entries() or cache.keys() which return entries in insertion order (not access order).
affects: >=1.0.0
gotchaConstructor does not validate capacity; negative or non-integer values may cause unexpected behavior
fix
Ensure capacity is a positive integer when constructing: new Cache(Math.max(1, Math.floor(capacity)))
affects: 1.0.0
Errors
Common errors & fixes
TypeError: cache_ts_1.default is not a function
Attempting to default-import the package (import Cache from 'cache-ts') but the package exports a named export, not default.
fix
Use named import: import { Cache } from 'cache-ts'
Error: cache constructor requires a capacity argument
Missing or undefined capacity passed to the Cache constructor.
fix
Provide a valid number: new Cache(100)
ReferenceError: Cache is not defined
Using const Cache = require('cache-ts') which returns the module object, not the Cache class.
fix
Use destructuring: const { Cache } = require('cache-ts')
Upgrade
Version history
1.0.0latest on npm
Audit
Dependencies

No dependency data recorded yet.

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