{"id":46683,"library":"ttl-mem-cache","title":"ttl-mem-cache","description":"An in-memory TTL key/value cache with streaming support. Current stable version is 4.1.0. It does not proactively prune expired items; instead, items expire when touched, avoiding setTimeout overhead. There is no maximum item limit, making it suitable for small to medium datasets. Key differentiators include per-item TTL, stale data delivery option, change event streaming via a Duplex stream, and an optional instance ID for origin tracking. It is a Node.js-only library, not browser-compatible.","status":"active","version":"4.1.0","language":"javascript","source_language":"en","source_url":"ssh://git@github.com/trygve-lie/ttl-mem-cache","tags":["javascript","cache","memcache","memory cache","ttl","time to live","performance","expire cache","expiring"],"install":[{"cmd":"npm install ttl-mem-cache","lang":"bash","label":"npm"},{"cmd":"yarn add ttl-mem-cache","lang":"bash","label":"yarn"},{"cmd":"pnpm add ttl-mem-cache","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"Package exports a single default class; named destructure fails. Use default import.","wrong":"const { Cache } = require('ttl-mem-cache');","symbol":"default (the Cache class)","correct":"import Cache from 'ttl-mem-cache';"},{"note":"CommonJS style works fine for older code; no named exports.","wrong":"const cache = new Cache(); // missing require","symbol":"Cache (via require)","correct":"const Cache = require('ttl-mem-cache');"},{"note":"Namespace import gives an object with default property; use default import instead.","wrong":"import * as Cache from 'ttl-mem-cache';","symbol":"Cache (ESM with namespace)","correct":"import Cache from 'ttl-mem-cache';"}],"quickstart":{"code":"const Cache = require('ttl-mem-cache');\n\nconst cache = new Cache({ ttl: 60000 }); // 1 minute default TTL\n\n// Set a value with default TTL\ncache.set('example', { message: 'Hello, world!' });\n\n// Set a value with custom TTL\ncache.set('ephemeral', 'data', 5000); // expires in 5 seconds\n\n// Get value\nconst value = cache.get('example');\nconsole.log(value); // { message: 'Hello, world!' }\n\n// Get after TTL expiration\nsetTimeout(() => {\n  const expiredValue = cache.get('example');\n  console.log(expiredValue); // null (if not stale)\n}, 61000);\n\n// Get stale value\nconst staleCache = new Cache({ ttl: 10000, stale: true });\nstaleCache.set('staleItem', 'old data');\nsetTimeout(() => {\n  const stale = staleCache.get('staleItem');\n  console.log(stale); // 'old data' (even though expired, returned before removal)\n}, 11000);","lang":"javascript","description":"Shows basic instantiation with TTL options, setting/getting values, and stale mode behavior."},"warnings":[{"fix":"Update instantiation to new Cache({ ttl: ... }) instead of new Cache(ttl, stale).","message":"In v2.0.0, the constructor signature changed: options object is required instead of separate arguments.","severity":"breaking","affected_versions":"<2.0.0"},{"fix":"Replace cache.remove(key) with cache.del(key).","message":"As of v3.0.0, .remove() method is deprecated; use .del() instead.","severity":"deprecated","affected_versions":">=3.0.0"},{"fix":"Periodically call .get() or .entries() to trigger pruning, or implement a separate cleanup loop.","message":"The cache does not proactively expire items; expired items are only removed when accessed. Memory may grow if items are never touched.","severity":"gotcha","affected_versions":">=0.0.0"},{"fix":"If using stream mode, ensure you consume the readable side or handle backpressure properly.","message":"Streaming events (set, del, expire) are emitted on the Duplex stream; forgetting to pipe or listen can cause memory backpressure issues.","severity":"gotcha","affected_versions":">=4.0.0"},{"fix":"Use a sufficiently large number (e.g., Number.MAX_SAFE_INTEGER) if you want a clear expiration boundary.","message":"Setting ttl to Infinity caches forever, but the item can still be overwritten or deleted. This can be unexpected if you assume no changes.","severity":"gotcha","affected_versions":">=0.0.0"}],"env_vars":null,"search_vec":"'4.1.0':19 'avoid':32 'browser':83 'browser-compat':82 'cach':4,11,86,89,96 'chang':60 'compat':84 'current':15 'data':57 'dataset':48 'deliveri':58 'differenti':50 'duplex':65 'event':61 'expir':25,29,95,97 'id':71 'in-memori':6 'includ':51 'instanc':70 'instead':27 'item':26,28,39,54 'javascript':85 'key':49 'key/value':10 'librari':80 'limit':40 'live':93 'make':41 'maximum':38 'medium':47 'mem':3 'memcach':87 'memori':8,88 'node.js':78 'option':59,69 'origin':73 'overhead':34 'per':53 'per-item':52 'perform':94 'proactiv':23 'prune':24 'settimeout':33 'small':45 'stabl':16 'stale':56 'stream':13,62,66 'suitabl':43 'support':14 'time':91 'touch':31 'track':74 'ttl':2,9,55,90 'ttl-mem-cach':1 'version':17 'via':63","created_at":"2026-06-07T13:01:06.691764+00:00","updated_at":"2026-06-07T13:01:06.691764+00:00","problems":[{"fix":"Use default import: const Cache = require('ttl-mem-cache'); or import Cache from 'ttl-mem-cache';","cause":"Importing incorrectly; using named import instead of default.","error":"TypeError: cache.get is not a function"},{"fix":"Instantiate first: const cache = new Cache(); then cache.set(key, value);","cause":"Trying to call method on wrong object, e.g., calling set on the class instead of an instance.","error":"TypeError: cache.set is not a function"},{"fix":"Use import Cache from 'ttl-mem-cache'; instead of require.","cause":"Using CommonJS require in an ES module environment.","error":"Uncaught ReferenceError: require is not defined"}],"ecosystem":"npm","meta_description":null,"install_score":null,"quickstart_score":null,"quickstart_tag":null,"pypi_latest":null,"cli_name":null,"cli_version":null,"type":"library","homepage":"https://github.com/trygve-lie/ttl-mem-cache#readme","github":"ssh://git@github.com/trygve-lie/ttl-mem-cache","docs":null,"changelog":null,"pypi":null,"npm":"ttl-mem-cache","openapi_spec":null,"status_page":null,"smithery":null,"categories":["storage"],"base_url":null,"auth_type":null,"provenance":{"verified_status":null,"verified_at":null,"last_verified":"2026-06-07","next_check":"2026-09-05","install_tag":null}}