{"id":45758,"library":"petty-cache","title":"petty-cache","description":"Two-level cache for Node.js (in-memory + Redis) with automatic serialization, jitter, double-checked locking to prevent cache stampedes and thundering herds. Version 3.7.0 released under MIT license with monthly releases. Key differentiators: built-in distributed mutex and semaphore primitives, jittered TTLs (default 30-60s Redis, 2-5s in-memory), and double-checked locking on cache miss functions. ESM-only since v3.0.0.","status":"active","version":"3.7.0","language":"javascript","source_language":"en","source_url":"https://github.com/stores-com/petty-cache","tags":["javascript","cache","lock","mutex","redis","semaphore"],"install":[{"cmd":"npm install petty-cache","lang":"bash","label":"npm"},{"cmd":"yarn add petty-cache","lang":"bash","label":"yarn"},{"cmd":"pnpm add petty-cache","lang":"bash","label":"pnpm"}],"dependencies":[{"reason":"Required for Redis-based distributed caching and locking. Must be at least v2.6.0.","package":"redis","optional":false}],"imports":[{"note":"Default import. ESM-only since v3.0.0. CommonJS require() will fail.","wrong":"const PettyCache = require('petty-cache')","symbol":"PettyCache","correct":"import PettyCache from 'petty-cache'"},{"note":"Mutex and Semaphore are in separate subpath exports. They are not exported from the main entry.","wrong":"import { Mutex } from 'petty-cache'","symbol":"Mutex","correct":"import { Mutex } from 'petty-cache/mutex'"},{"note":"Semaphore is in a subpath export, not from the main entry.","wrong":"import { Semaphore } from 'petty-cache'","symbol":"Semaphore","correct":"import { Semaphore } from 'petty-cache/semaphore'"}],"quickstart":{"code":"import PettyCache from 'petty-cache';\n\nconst cache = new PettyCache({\n  host: process.env.REDIS_HOST ?? '127.0.0.1',\n  port: parseInt(process.env.REDIS_PORT ?? '6379'),\n  auth_pass: process.env.REDIS_PASS ?? ''\n});\n\n// Fetch with cache miss function\ncache.fetch('mykey', (cb) => {\n  fetchFromDatabase((err, data) => {\n    if (err) return cb(err);\n    cb(null, JSON.stringify(data));\n  });\n}, (err, value) => {\n  if (err) console.error(err);\n  else console.log('Cached value:', value);\n});\n\nfunction fetchFromDatabase(callback) {\n  // Simulate async DB call\n  setTimeout(() => callback(null, { foo: 'bar' }), 100);\n}","lang":"typescript","description":"Shows creation of PettyCache client with environment-based Redis config, and usage of fetch() with a cache miss function."},"warnings":[{"fix":"Use import PettyCache from 'petty-cache' instead of require().","message":"ESM-only since v3.0.0. CommonJS require() will throw a MODULE_NOT_FOUND error.","severity":"breaking","affected_versions":">=3.0.0"},{"fix":"Use new PettyCache({ host, port }) or pass the RedisClient directly.","message":"Constructor signature changed in v3.0.0: port, host, options as object or RedisClient directly. The old positional arguments are deprecated.","severity":"breaking","affected_versions":">=3.0.0"},{"fix":"Ensure your cache miss function properly serializes complex objects if needed. PettyCache automatically serializes/deserializes, but custom serialization may be required for non-JSON-safe types.","message":"The cacheMissFunction in fetch() must call callback with (error, value). If value is not a string, it will be serialized with JSON.stringify().","severity":"gotcha","affected_versions":"*"}],"env_vars":null,"search_vec":"'-5':56 '-60':52 '2':55 '3.7.0':30 '30':51 'automat':15 'built':41 'built-in':40 'cach':3,7,24,67,76 'check':20,64 'default':50 'differenti':39 'distribut':43 'doubl':19,63 'double-check':18,62 'esm':71 'esm-on':70 'function':69 'herd':28 'in-memori':10,58 'javascript':75 'jitter':17,48 'key':38 'level':6 'licens':34 'lock':21,65,77 'memori':12,60 'miss':68 'mit':33 'month':36 'mutex':44,78 'node.js':9 'petti':2 'petty-cach':1 'prevent':23 'primit':47 'redi':13,54,79 'releas':31,37 'semaphor':46,80 'serial':16 'sinc':73 'stamped':25 'thunder':27 'ttls':49 'two':5 'two-level':4 'v3.0.0':74 'version':29","created_at":"2026-06-07T12:56:35.415603+00:00","updated_at":"2026-06-07T12:56:35.415603+00:00","problems":[{"fix":"Change to import PettyCache from 'petty-cache' and ensure your project is ESM-enabled (add type: 'module' to package.json).","cause":"Using CommonJS require() with an ESM-only package version.","error":"ERR MODULE_NOT_FOUND: require is not supported in ESM"},{"fix":"Ensure tsconfig.json has 'esModuleInterop': true and use 'import PettyCache from 'petty-cache''.","cause":"Using import PettyCache from 'petty-cache' in a TypeScript project without setting esModuleInterop or using default import syntax incorrectly.","error":"TypeError: PettyCache is not a constructor"},{"fix":"npm install redis (must be version >=2.6.0).","cause":"Missing peer dependency redis.","error":"Error: The 'redis' package is required to instantiate PettyCache"}],"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/stores-com/petty-cache","github":"https://github.com/stores-com/petty-cache","docs":null,"changelog":null,"pypi":null,"npm":"petty-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}}