{"id":16150,"library":"node-ts-cache-storage-memory","title":"Memory Storage for node-ts-cache","description":"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.","status":"active","version":"4.4.0","language":"javascript","source_language":"en","source_url":"https://github.com/havsar/node-ts-cache","tags":["javascript","node","nodejs","cache","typescript","ts","caching","memcache","memory-cache"],"install":[{"cmd":"npm install node-ts-cache-storage-memory","lang":"bash","label":"npm"},{"cmd":"yarn add node-ts-cache-storage-memory","lang":"bash","label":"yarn"},{"cmd":"pnpm add node-ts-cache-storage-memory","lang":"bash","label":"pnpm"}],"dependencies":[{"reason":"This package is an implementation for node-ts-cache and requires the core library for decorators and container management.","package":"node-ts-cache","optional":false}],"imports":[{"note":"Primarily designed for TypeScript/ESM environments. CommonJS users should use `require` and access the named export.","wrong":"const MemoryStorage = require('node-ts-cache-storage-memory').MemoryStorage","symbol":"MemoryStorage","correct":"import { MemoryStorage } from 'node-ts-cache-storage-memory'"},{"note":"The `Cache` decorator is part of the core `node-ts-cache` package, not the storage module itself.","wrong":"import { Cache } from 'node-ts-cache-storage-memory'","symbol":"Cache","correct":"import { Cache } from 'node-ts-cache'"},{"note":"Like `Cache`, `CacheContainer` is a core component from `node-ts-cache` for managing cache instances.","wrong":"const { CacheContainer } = require('node-ts-cache-storage-memory')","symbol":"CacheContainer","correct":"import { CacheContainer } from 'node-ts-cache'"}],"quickstart":{"code":"import { Cache, CacheContainer } from \"node-ts-cache\";\nimport { MemoryStorage } from \"node-ts-cache-storage-memory\";\n\nconst userCache = new CacheContainer(new MemoryStorage());\n\nclass MyService {\n    // Simulate a network call or database query\n    private async fetchUsersFromAPI(): Promise<string[]> {\n        console.log('Fetching users from API...');\n        return new Promise(resolve => setTimeout(() => resolve([`Max-${Date.now()}`, 'User']), 100));\n    }\n\n    @Cache(userCache, { ttl: 60 }) // Cache for 60 seconds\n    public async getUsers(): Promise<string[]> {\n        return this.fetchUsersFromAPI();\n    }\n}\n\nasync function runExample() {\n    const service = new MyService();\n\n    console.log('First call:');\n    let users1 = await service.getUsers();\n    console.log(users1);\n\n    console.log('Second call (should be cached):');\n    let users2 = await service.getUsers();\n    console.log(users2);\n\n    await new Promise(resolve => setTimeout(resolve, 61 * 1000)); // Wait for cache to expire\n\n    console.log('Third call after TTL (should re-fetch):');\n    let users3 = await service.getUsers();\n    console.log(users3);\n}\n\nrunExample();","lang":"typescript","description":"Demonstrates how to set up and use `MemoryStorage` with `node-ts-cache` decorators, including basic caching and TTL expiration."},"warnings":[{"fix":"Adjust `ttl` values in your `@Cache` decorators or `CacheContainer` configurations to represent seconds instead of milliseconds.","message":"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.","severity":"breaking","affected_versions":">=4.3.4"},{"fix":"No fix required, this is a behavioral improvement. Review your application logic if it depends on specific timing or multiple simultaneous executions of a cached method.","message":"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.","severity":"gotcha","affected_versions":">=4.3.4"},{"fix":"Ensure both `node-ts-cache` and `node-ts-cache-storage-memory` are installed: `npm i node-ts-cache node-ts-cache-storage-memory`.","message":"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.","severity":"gotcha","affected_versions":"*"}],"env_vars":null,"last_verified":"2026-04-21T00:00:00.000Z","next_check":"2026-07-20T00:00:00.000Z","problems":[{"fix":"Ensure `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.","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.","error":"TypeError: Cannot read properties of undefined (reading 'registerStorage')"},{"fix":"For 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.","cause":"Attempting to use `MemoryStorage` in a CommonJS context without a proper `require` statement, or when the package itself is not installed.","error":"ReferenceError: MemoryStorage is not defined"},{"fix":"Ensure 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.","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.","error":"Error: The \"ttl\" argument must be a number (received type undefined)"}],"ecosystem":"npm"}