{"library":"node-cache","title":"In-memory Cache for Node.js","description":"node-cache is a simple, fast, and internal in-memory caching module for Node.js applications, designed to function similarly to memcached but within a single Node.js process. It supports setting key-value pairs with optional time-to-live (TTL) expiration, allowing for automatic invalidation and deletion of stale data. The current stable version is 5.1.2, actively maintained with regular updates addressing bug fixes and minor features, as evidenced by recent 5.x releases. A major breaking change in v5.0.0 removed callback support, shifting entirely to synchronous operations (with an opt-in legacy callback option for migration), and dropped the `lodash` dependency, improving performance and reducing the bundle size. It differentiates itself by its straightforward API and focus on local process caching, suitable for scenarios where a full-fledged external caching solution like Redis or Memcached is overkill. Keys can be strings or numbers, and the cache can be configured with standard TTLs, periodic cleanup, and an option to store references vs. clones of values, balancing performance and data isolation.","language":"javascript","status":"active","last_verified":"Sun Apr 19","install":{"commands":["npm install node-cache"],"cli":null},"imports":["import NodeCache from 'node-cache';","const NodeCache = require('node-cache');","import { NodeCacheEvents } from 'node-cache';\n// Usage: myCache.on(NodeCacheEvents.EXPIRED, ...)"],"auth":{"required":false,"env_vars":[]},"quickstart":{"code":"import NodeCache from 'node-cache';\n\nconst myCache = new NodeCache({ stdTTL: 60, checkperiod: 120, useClones: false });\n\nmyCache.set('myKey', 'myValue', 10); // Cache for 10 seconds\n\nlet value = myCache.get('myKey');\nif (value) {\n  console.log(`Retrieved: ${value}`);\n} else {\n  console.log('Key not found or expired.');\n}\n\n// Simulate an async operation and cache its result\nasync function fetchDataAndCache(key) {\n  let cachedData = myCache.get(key);\n  if (cachedData) {\n    return cachedData;\n  }\n  console.log('Fetching fresh data...');\n  const freshData = await new Promise(resolve => setTimeout(() => resolve({ id: 1, name: 'Fresh Data' }), 500));\n  myCache.set(key, freshData, 30);\n  return freshData;\n}\n\nfetchDataAndCache('asyncData').then(data => console.log('Async data:', data));\n\n// Get and delete a key with a single operation (v5.1.0+)\nconst takenValue = myCache.take('myKey');\nconsole.log(`Taken value: ${takenValue}`);\nconsole.log(`Is myKey still in cache? ${myCache.has('myKey')}`);","lang":"typescript","description":"Initializes NodeCache, sets and retrieves a value with a TTL, demonstrates handling missing keys, simulates caching an asynchronous operation, and shows the `take` method for atomic get-and-delete.","tag":null,"tag_description":null,"last_tested":null,"results":[]},"compatibility":null}