{"id":44568,"library":"clean-cache","title":"clean-cache","description":"Clean Cache is a minimal in-memory JavaScript cache with TTL (time-to-live) support, packaged as a single file with zero dependencies. Current stable version is 1.0.2. It offers a simple API with `add`, `retrieve`, `count`, and `tidy` methods, and validates keys to prevent null/undefined. Unlike larger caching libraries (e.g., node-cache, lru-cache), it keeps dependencies to a minimum and is fully tested. Suitable for small Node.js projects needing a lightweight, ephemeral cache.","status":"active","version":"1.0.2","language":"javascript","source_language":"en","source_url":"https://github.com/kuper-adrian/clean-cache","tags":["javascript","cache","caching","memory"],"install":[{"cmd":"npm install clean-cache","lang":"bash","label":"npm"},{"cmd":"yarn add clean-cache","lang":"bash","label":"yarn"},{"cmd":"pnpm add clean-cache","lang":"bash","label":"pnpm"}],"dependencies":[],"imports":[{"note":"Package is CommonJS-only. Named export, not default.","wrong":"import Cache from 'clean-cache'","symbol":"Cache","correct":"const { Cache } = require('clean-cache')"},{"note":"Even in ESM, named import is required.","wrong":"import Cache from 'clean-cache'","symbol":"Cache","correct":"import { Cache } from 'clean-cache'"},{"note":"Require of the package returns an object with Cache property, not the class directly.","wrong":"const Cache = require('clean-cache')","symbol":"Cache","correct":"const { Cache } = require('clean-cache')"}],"quickstart":{"code":"const { Cache } = require('clean-cache');\n\nconst myCache = new Cache(30000); // TTL 30 seconds\nmyCache.add('user1', { name: 'Alice' });\nconsole.log(myCache.retrieve('user1')); // { name: 'Alice' }\nconsole.log(myCache.count()); // 1\nsetTimeout(() => {\n  console.log(myCache.retrieve('user1')); // null after 30s\n}, 31000);","lang":"javascript","description":"Basic usage: create a cache with TTL, add and retrieve an item, then see it expire."},"warnings":[{"fix":"Always provide a valid string or number key.","message":"Passing null or undefined as key to add/retrieve throws an error.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"Use milliseconds. e.g., 5000 for 5 seconds.","message":"TTL is in milliseconds, not seconds. Default TTL is 60000ms (60 seconds).","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"Use require or use a bundler that handles CJS.","message":"Package uses CommonJS module system, not ESM. Import directly with require.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"Check for null explicitly.","message":"retrieve returns null for non-existent or expired keys. Do not expect undefined.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"Call myCache.tidy() periodically to free memory.","message":"Cache does not automatically remove expired items; use tidy() to clean them.","severity":"gotcha","affected_versions":">=1.0.0"},{"fix":"To avoid unintended expiration, pass a very large number or rely on default behavior.","message":"add with no TTL argument uses the cache default TTL, not infinite.","severity":"gotcha","affected_versions":">=1.0.0"}],"env_vars":null,"search_vec":"'1.0.2':33 'add':40 'api':38 'cach':3,5,13,54,59,62,82,84,85 'clean':2,4 'clean-cach':1 'count':42 'current':29 'depend':28,65 'e.g':56 'ephemer':81 'file':25 'fulli':71 'in-memori':9 'javascript':12,83 'keep':64 'key':48 'larger':53 'librari':55 'lightweight':80 'live':19 'lru':61 'lru-cach':60 'memori':11,86 'method':45 'minim':8 'minimum':68 'need':78 'node':58 'node-cach':57 'node.js':76 'null/undefined':51 'offer':35 'packag':21 'prevent':50 'project':77 'retriev':41 'simpl':37 'singl':24 'small':75 'stabl':30 'suitabl':73 'support':20 'test':72 'tidi':44 'time':17 'time-to-l':16 'ttl':15 'unlik':52 'valid':47 'version':31 'zero':27","created_at":"2026-06-07T12:50:46.915990+00:00","updated_at":"2026-06-07T12:50:46.915990+00:00","problems":[{"fix":"Use named import: `const { Cache } = require('clean-cache')`","cause":"Imported Cache class incorrectly (e.g., `import Cache from 'clean-cache'` instead of `const { Cache } = require('clean-cache')`)","error":"TypeError: myCache.add is not a function"},{"fix":"Always check for null: `const val = myCache.retrieve('key'); if (val !== null) { ... }`","cause":"retrieve returned null because key expired or doesn't exist","error":"TypeError: Cannot read properties of null (reading 'foo')"},{"fix":"Pass a valid string or number. Example: `myCache.add('validKey', data)`","cause":"Passed null or undefined as key to add or retrieve","error":"Error: Invalid key"},{"fix":"Use `const { Cache } = require('clean-cache')`","cause":"Required the package as default export instead of named export","error":"Cache is not a constructor"}],"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/kuper-adrian/clean-cache#readme","github":"https://github.com/kuper-adrian/clean-cache","docs":null,"changelog":null,"pypi":null,"npm":"clean-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}}