{"id":46684,"library":"ttl","title":"ttl","description":"Simple in-memory cache with TTL and capacity for JavaScript. Version 1.3.1 is the current stable release, with infrequent updates. It provides a lightweight cache with optional time-to-live per entry, capacity-limited eviction, and events for put, del, drop, hit, and miss. Differentiates from similar libraries (e.g., node-cache) by its simple API and event-driven architecture. Designed for Node.js environments, with no external dependencies.","status":"active","version":"1.3.1","language":"javascript","source_language":"en","source_url":"https://github.com/mrhooray/ttl","tags":["javascript","cache","caching","memory","in-memory","ttl"],"install":[{"cmd":"npm install ttl","lang":"bash","label":"npm"},{"cmd":"yarn add ttl","lang":"bash","label":"yarn"},{"cmd":"pnpm add ttl","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"CommonJS default export; no named exports.","wrong":"const Cache = require('ttl').default;","symbol":"default","correct":"const Cache = require('ttl');"},{"note":"ESM import; package does not export named symbols.","wrong":"import { Cache } from 'ttl';","symbol":"Cache","correct":"import Cache from 'ttl';"},{"note":"Dynamic import in Node.js ESM; default export is on the module namespace.","wrong":"const Cache = await import('ttl');","symbol":"Cache","correct":"const { default: Cache } = await import('ttl');"}],"quickstart":{"code":"const Cache = require('ttl');\nconst cache = new Cache({ ttl: 5000, capacity: 10 });\n\ncache.on('put', (key, val) => console.log(`Put: ${key}=${val}`));\ncache.on('del', (key, val) => console.log(`Del: ${key}`));\ncache.on('drop', (key, val) => console.log(`Drop: ${key}`));\ncache.on('hit', (key, val) => console.log(`Hit: ${key}`));\ncache.on('miss', key => console.log(`Miss: ${key}`));\n\ncache.put('greeting', 'hello');\ncache.put('farewell', 'bye', 10000);\n\nconsole.log(cache.get('greeting')); // 'hello'\nconsole.log(cache.get('missing'));  // undefined\n\nsetTimeout(() => {\n  console.log(cache.get('greeting')); // undefined after 5s\n}, 6000);","lang":"javascript","description":"Creates a cache with TTL and capacity, listens to events, and demonstrates put/get with expiry."},"warnings":[{"fix":"Use strings or primitives as keys; if using objects, ensure same reference.","message":"Cache keys are compared by reference for objects, not by value.","severity":"gotcha","affected_versions":">=0.0.0"},{"fix":"Understand eviction policy: oldest inserted items are dropped when capacity is exceeded.","message":"Capacity eviction drops the oldest entries (FIFO), not LRU.","severity":"gotcha","affected_versions":">=0.0.0"},{"fix":"Pass numbers in milliseconds (e.g., 5000 for 5 seconds).","message":"TTL is in milliseconds, not seconds or a string format like '5s'.","severity":"gotcha","affected_versions":">=0.0.0"},{"fix":"Install @types/ttl or declare module manually.","message":"No TypeScript type definitions are provided; user must create own or use @types/ttl.","severity":"deprecated","affected_versions":"<2.0.0"}],"env_vars":null,"search_vec":"'1.3.1':14 'api':60 'architectur':65 'cach':6,27,56,75,76 'capac':10,37 'capacity-limit':36 'current':17 'del':44 'depend':73 'design':66 'differenti':49 'driven':64 'drop':45 'e.g':53 'entri':35 'environ':69 'event':41,63 'event-driven':62 'evict':39 'extern':72 'hit':46 'in-memori':3,78 'infrequ':21 'javascript':12,74 'librari':52 'lightweight':26 'limit':38 'live':33 'memori':5,77,80 'miss':48 'node':55 'node-cach':54 'node.js':68 'option':29 'per':34 'provid':24 'put':43 'releas':19 'similar':51 'simpl':2,59 'stabl':18 'time':31 'time-to-l':30 'ttl':1,8,81 'updat':22 'version':13","created_at":"2026-06-07T13:01:07.370698+00:00","updated_at":"2026-06-07T13:01:07.370698+00:00","problems":[{"fix":"Use const Cache = require('ttl'); in CommonJS or import Cache from 'ttl'; in ESM.","cause":"Trying to import incorrectly or using wrong export name.","error":"TypeError: Cache is not a constructor"},{"fix":"Use import Cache from 'ttl'; instead of require.","cause":"Running in ESM context where require is not available.","error":"ReferenceError: require is not defined"},{"fix":"Run npm install ttl --save.","cause":"Package not installed.","error":"Cannot find module 'ttl'"},{"fix":"Use const cache = new Cache({ ... }); with the 'new' keyword.","cause":"Cache object not instantiated correctly, e.g., missing 'new'.","error":"cache.put(...) is not a function"}],"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/mrhooray/ttl","github":"https://github.com/mrhooray/ttl","docs":null,"changelog":null,"pypi":null,"npm":"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}}