Memory Storage for node-ts-cache
node-ts-cache-storage-memory is a dedicated in-memory storage module for the node-ts-cache library, designed to provide fast, local caching capabilities within Node.js applications. It is currently stable, with version 4.4.0 being the latest as of April 2026, and follows an active release cadence addressing bug fixes and performance enhancements. A key differentiator is its seamless integration with the node-ts-cache decorator-based caching system and the introduction of an 'enqueue' feature in v4.3.4 to prevent thundering herd problems by ensuring the original method is called only once even under high concurrency. This package focuses solely on in-memory storage, contrasting with other node-ts-cache storage modules like Redis or file-based options, and is primarily intended for scenarios where persistent or distributed caching is not required.
Common errors
-
TypeError: Cannot read properties of undefined (reading 'registerStorage')
cause `node-ts-cache` (the main package) is not installed or not correctly imported/initialized, and `node-ts-cache-storage-memory` is attempting to register itself without the core library present.fixEnsure `node-ts-cache` is installed alongside `node-ts-cache-storage-memory` (`npm i node-ts-cache node-ts-cache-storage-memory`) and that `CacheContainer` is correctly instantiated with a `MemoryStorage` instance. -
ReferenceError: MemoryStorage is not defined
cause Attempting to use `MemoryStorage` in a CommonJS context without a proper `require` statement, or when the package itself is not installed.fixFor CommonJS, use `const { MemoryStorage } = require('node-ts-cache-storage-memory');`. For ESM, ensure `import { MemoryStorage } from 'node-ts-cache-storage-memory';` is present and the package is installed. -
Error: The "ttl" argument must be a number (received type undefined)
cause The `ttl` option in the `@Cache` decorator or `CacheContainer` configuration is missing, not a valid number, or its unit was misunderstood, especially after the `v4.3.4` fix for TTL units.fixEnsure the `ttl` option is a valid number representing *seconds*. If upgrading to `v4.3.4` or later, remember `ttl` in non-lazy mode now correctly means seconds, not milliseconds.
Warnings
- breaking TTL unit change in `v4.3.4` (non-lazy mode). Prior to `v4.3.4`, the `ttl` option in non-lazy caching mode was incorrectly interpreted as milliseconds. It is now correctly treated as *seconds*. Users upgrading should review and adjust existing `ttl` configurations to avoid significantly shorter cache durations.
- gotcha Concurrency handling improved with 'enqueue' feature in `v4.3.4`. This feature prevents the 'thundering herd' problem by ensuring that when multiple simultaneous calls request an uncached item, the original method is executed only once, with subsequent callers receiving the same result. While beneficial, be aware of this change in behavior if your application logic previously relied on specific timing or multiple concurrent executions of the cached method.
- gotcha This package is a storage implementation for `node-ts-cache`. It requires `node-ts-cache` to be installed and used for the caching decorators and container management. Using `node-ts-cache-storage-memory` standalone will result in runtime errors.
Install
-
npm install node-ts-cache-storage-memory -
yarn add node-ts-cache-storage-memory -
pnpm add node-ts-cache-storage-memory
Imports
- MemoryStorage
const MemoryStorage = require('node-ts-cache-storage-memory').MemoryStorageimport { MemoryStorage } from 'node-ts-cache-storage-memory' - Cache
import { Cache } from 'node-ts-cache-storage-memory'import { Cache } from 'node-ts-cache' - CacheContainer
const { CacheContainer } = require('node-ts-cache-storage-memory')import { CacheContainer } from 'node-ts-cache'
Quickstart
import { Cache, CacheContainer } from "node-ts-cache";
import { MemoryStorage } from "node-ts-cache-storage-memory";
const userCache = new CacheContainer(new MemoryStorage());
class MyService {
// Simulate a network call or database query
private async fetchUsersFromAPI(): Promise<string[]> {
console.log('Fetching users from API...');
return new Promise(resolve => setTimeout(() => resolve([`Max-${Date.now()}`, 'User']), 100));
}
@Cache(userCache, { ttl: 60 }) // Cache for 60 seconds
public async getUsers(): Promise<string[]> {
return this.fetchUsersFromAPI();
}
}
async function runExample() {
const service = new MyService();
console.log('First call:');
let users1 = await service.getUsers();
console.log(users1);
console.log('Second call (should be cached):');
let users2 = await service.getUsers();
console.log(users2);
await new Promise(resolve => setTimeout(resolve, 61 * 1000)); // Wait for cache to expire
console.log('Third call after TTL (should re-fetch):');
let users3 = await service.getUsers();
console.log(users3);
}
runExample();