{"id":45396,"library":"memory-cache-ttl","title":"memory-cache-ttl","description":"A simple, performant in-memory cache with configurable Time To Live (TTL) for Node.js. Current stable version is 1.0.52. It provides global and per-key TTL, automatic expiry cleanup via configurable interval, optional TTL randomization, extend-on-hit behavior, and an async onInterval callback to refresh cache entries. The package has zero dependencies and uses CommonJS. It is lightweight and minimal compared to more feature-rich caches like node-cache or lru-cache.","status":"active","version":"1.0.52","language":"javascript","source_language":"en","source_url":"ssh://git@github.com/tiborv/memory-cache-ttl","tags":["javascript","cache","simple","ttl","memory"],"install":[{"cmd":"npm install memory-cache-ttl","lang":"bash","label":"npm"},{"cmd":"yarn add memory-cache-ttl","lang":"bash","label":"yarn"},{"cmd":"pnpm add memory-cache-ttl","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"Package uses CommonJS only; no ESM exports.","wrong":"import cache from 'memory-cache-ttl';","symbol":"default","correct":"const cache = require('memory-cache-ttl');"},{"note":"TTL must be a number (seconds).","wrong":"cache.init({ ttl: '5' });","symbol":"init","correct":"cache.init({ ttl: 5 });"},{"note":"Optional TTL must be a number. If not provided, global TTL is used.","wrong":"cache.set('key', 'value', '10');","symbol":"set","correct":"cache.set('key', 'value', 10);"},{"note":"onInterval receives cache ID as argument, not event-like callback.","wrong":"cache.init({ onInterval: fetchNewValue });","symbol":"onInterval","correct":"cache.init({ onInterval: (id) => fetchNewValue(id) });"}],"quickstart":{"code":"const cache = require('memory-cache-ttl');\n\ncache.init({ ttl: 3, interval: 1, randomize: false });\n\ncache.set('asd', 'dsa');\n\nconsole.log(cache.check('asd')); // true\nconsole.log(cache.get('asd'));   // 'dsa'\n\nsetTimeout(() => {\n  console.log(cache.check('asd')); // false\n  console.log(cache.get('asd'));   // undefined\n}, 5000);","lang":"javascript","description":"Initialize cache with 3-second TTL, set and get a value, then verify expiry after 5 seconds."},"warnings":[{"fix":"Use a positive integer for TTL. If you want no expiry, set TTL to Infinity or very large number.","message":"Setting TTL to 0 or negative disables expiry, causing entries to never expire.","severity":"gotcha","affected_versions":"*"},{"fix":"Always provide a global TTL or per-key TTL in set() to enable automatic expiry.","message":"If global TTL is not set (undefined), entries will never expire unless per-key TTL is provided.","severity":"gotcha","affected_versions":"*"},{"fix":"Ensure onInterval returns a Promise that resolves to the new value or undefined to keep old value.","message":"The onInterval callback must return a Promise; if no value is returned, the old value persists.","severity":"gotcha","affected_versions":"*"},{"fix":"Set interval to a value less than or equal to the smallest TTL for timely expiry.","message":"interval setting controls cleanup interval, not per-entry TTL check; entries are only removed on interval ticks.","severity":"gotcha","affected_versions":"*"}],"env_vars":null,"search_vec":"'1.0.52':24 'async':49 'automat':33 'behavior':46 'cach':3,11,54,75,79,83,85 'callback':51 'cleanup':35 'commonj':63 'compar':69 'configur':13,37 'current':20 'depend':60 'entri':55 'expiri':34 'extend':43 'extend-on-hit':42 'featur':73 'feature-rich':72 'global':27 'hit':45 'in-memori':8 'interv':38 'javascript':84 'key':31 'lightweight':66 'like':76 'live':16 'lru':82 'lru-cach':81 'memori':2,10,88 'memory-cache-ttl':1 'minim':68 'node':78 'node-cach':77 'node.js':19 'oninterv':50 'option':39 'packag':57 'per':30 'per-key':29 'perform':7 'provid':26 'random':41 'refresh':53 'rich':74 'simpl':6,86 'stabl':21 'time':14 'ttl':4,17,32,40,87 'use':62 'version':22 'via':36 'zero':59","created_at":"2026-06-07T12:54:48.683522+00:00","updated_at":"2026-06-07T12:54:48.683522+00:00","problems":[{"fix":"Use const cache = require('memory-cache-ttl'); (CommonJS) or use default import if ESM wrapper exists (check package).","cause":"Incorrect import; package exports default object, not named export.","error":"cache.init is not a function"},{"fix":"Ensure cache is required as default: const cache = require('memory-cache-ttl'); then call cache.init() before set/get.","cause":"Imported default incorrectly or forgot to call init() first.","error":"TypeError: cache.set is not a function"},{"fix":"Set interval to a reasonable value (e.g., 1 second). Ensure cache.init({ interval: 1 }) is called.","cause":"Interval not set or set too high; cleanup may not have run yet.","error":"Value not expiring after TTL"}],"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/tiborv/memory-cache-ttl#readme","github":"ssh://git@github.com/tiborv/memory-cache-ttl","docs":null,"changelog":null,"pypi":null,"npm":"memory-cache-ttl","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}}