{"id":43962057,"url":"https://github.com/vnykmshr/obcachejs","last_synced_at":"2026-02-07T05:34:04.087Z","repository":{"id":10703984,"uuid":"12949503","full_name":"vnykmshr/obcachejs","owner":"vnykmshr","description":"Object caching for Node.js with LRU and Redis backends. Wraps async functions with automatic key generation and request deduplication.","archived":false,"fork":false,"pushed_at":"2025-11-25T00:15:02.000Z","size":111,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-11-28T10:38:12.271Z","etag":null,"topics":["async","cache","function-wrapper","lru-cache","memoization","nodejs","redis","request-deduplication"],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":false,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":"qzaidi/obcache","license":"bsd-3-clause","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/vnykmshr.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":".github/CONTRIBUTING.md","funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null,"notice":null,"maintainers":null,"copyright":null,"agents":null,"dco":null,"cla":null}},"created_at":"2013-09-19T13:42:14.000Z","updated_at":"2025-11-25T00:14:55.000Z","dependencies_parsed_at":null,"dependency_job_id":null,"html_url":"https://github.com/vnykmshr/obcachejs","commit_stats":null,"previous_names":["vnykmshr/obcachejs"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/vnykmshr/obcachejs","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/vnykmshr%2Fobcachejs","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/vnykmshr%2Fobcachejs/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/vnykmshr%2Fobcachejs/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/vnykmshr%2Fobcachejs/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/vnykmshr","download_url":"https://codeload.github.com/vnykmshr/obcachejs/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/vnykmshr%2Fobcachejs/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":29187239,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-02-07T05:07:31.176Z","status":"ssl_error","status_checked_at":"2026-02-07T05:06:15.227Z","response_time":63,"last_error":"SSL_connect returned=1 errno=0 peeraddr=140.82.121.6:443 state=error: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["async","cache","function-wrapper","lru-cache","memoization","nodejs","redis","request-deduplication"],"created_at":"2026-02-07T05:34:03.396Z","updated_at":"2026-02-07T05:34:04.082Z","avatar_url":"https://github.com/vnykmshr.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# obcachejs\n\n[![npm version](https://img.shields.io/npm/v/obcachejs.svg)](https://www.npmjs.com/package/obcachejs)\n[![CI](https://github.com/vnykmshr/obcachejs/actions/workflows/test.yml/badge.svg)](https://github.com/vnykmshr/obcachejs/actions/workflows/test.yml)\n[![License](https://img.shields.io/badge/license-BSD--3--Clause-blue.svg)](LICENSE)\n[![Node](https://img.shields.io/node/v/obcachejs.svg)](https://nodejs.org)\n\nObject caching for Node.js. Wraps async functions and caches results with automatic key generation.\n\n## Features\n\n- LRU in-memory cache with TTL support\n- Optional Redis backend for distributed caching\n- Request deduplication (prevents thundering herd)\n- Promise and callback APIs\n- TypeScript definitions included\n\n## Installation\n\n```bash\nnpm install obcachejs\n```\n\nRequires Node.js 18+\n\n## Quick Start\n\n```javascript\nconst obcache = require('obcachejs');\n\nconst cache = new obcache.Create({ max: 1000, maxAge: 60000 });\n\nconst getUser = cache.wrap(function(id, callback) {\n  // Simulate async database lookup\n  setTimeout(() =\u003e callback(null, { id, name: 'User ' + id }), 10);\n});\n\n// Promise style\nconst user = await getUser(123);\nconsole.log(user); // { id: 123, name: 'User 123' }\n\n// Callback style\ngetUser(456, (err, user) =\u003e console.log(user));\n```\n\n## API\n\n### obcache.Create(options)\n\nCreates a cache instance.\n\n```javascript\nconst cache = new obcache.Create({\n  max: 1000,           // max keys (default: 1000)\n  maxAge: 60000,       // TTL in ms\n  queueEnabled: true   // deduplicate concurrent requests\n});\n```\n\nOptions:\n\n| Option | Description |\n|--------|-------------|\n| `max` | Maximum number of cached keys |\n| `maxSize` | Maximum cache size in bytes (alternative to max) |\n| `maxAge` | Time-to-live in milliseconds |\n| `queueEnabled` | Enable request deduplication |\n| `dispose` | Function called when entries are evicted |\n| `reset.interval` | Auto-reset interval in ms |\n| `reset.firstReset` | First reset time (Date or ms) |\n\n### cache.wrap(fn, [thisobj], [skipArgs])\n\nWraps a callback-based function with caching. The wrapped function:\n- Returns a Promise when called without a callback\n- Calls the callback when provided as last argument\n- Generates cache keys from function name and arguments\n\n```javascript\nconst cached = cache.wrap(myAsyncFn);\n\n// Exclude arguments from key generation\nconst cached = cache.wrap(function(id, timestamp, cb) {\n  // timestamp won't affect cache key\n  cb(null, result);\n}, null, [1]);\n```\n\n### cache.warmup(fn, ...args, value)\n\nPre-populate cache for given arguments.\n\n```javascript\ncache.warmup(getUser, 123, { id: 123, name: 'Alice' });\n```\n\n### cache.invalidate(fn, ...args)\n\nRemove cached entry for given arguments.\n\n```javascript\ncache.invalidate(getUser, 123);\n```\n\n### cache.isReady()\n\nReturns true when cache backend is ready. Always true for LRU, waits for connection with Redis.\n\n### cache.stats\n\nCache statistics object.\n\n```javascript\n{\n  hit: 0,      // cache hits\n  miss: 0,     // cache misses\n  reset: 0,    // number of resets\n  pending: 0   // queued requests\n}\n```\n\n## Redis Backend\n\n```javascript\nconst cache = new obcache.Create({\n  max: 10000,\n  maxAge: 300000,\n  id: 1,  // required for Redis\n  redis: {\n    host: 'localhost',\n    port: 6379,\n    connectTimeout: 5000\n  }\n});\n\n// Wait for connection\nif (!cache.isReady()) {\n  // handle not ready\n}\n```\n\nRedis options:\n\n| Option | Description |\n|--------|-------------|\n| `host` | Redis host |\n| `port` | Redis port |\n| `url` | Connection URL (alternative to host/port) |\n| `database` | Redis database number |\n| `connectTimeout` | Connection timeout in ms (default: 5000) |\n| `twemproxy` | Enable twemproxy compatibility |\n\n## Debug Interface\n\nRegister caches for inspection:\n\n```javascript\nobcache.debug.register(cache, 'users');\n\n// Express middleware\napp.get('/debug/cache', obcache.debug.view);\n\n// Console output\nobcache.debug.log();\n```\n\n**Security Warning:** The debug endpoint exposes cached data via `?detail=cachename` and allows cache flushing via POST with `?flush=cachename`. Never expose this endpoint publicly. Always protect it with authentication middleware:\n\n```javascript\napp.get('/debug/cache', requireAdmin, obcache.debug.view);\n```\n\n## How It Works\n\nobcachejs generates cache keys by hashing the function name and serialized arguments using sigmund. When a wrapped function is called:\n\n1. Generate key from function name + arguments\n2. Check cache for existing value\n3. On hit: return cached value\n4. On miss: call original function, cache result, return\n\nWith `queueEnabled`, concurrent calls with the same key are queued and resolved together, preventing duplicate work.\n\n## TypeScript\n\nType definitions are included.\n\n```typescript\nconst obcache = require('obcachejs');\n// or with ES modules: import obcache from 'obcachejs';\n\nconst cache = new obcache.Create({ max: 100 });\n```\n\nTypes available: `Cache`, `CacheOptions`, `CacheStats`, `CachedFunction`.\n\n## Security\n\n- **Redis authentication**: Use the `url` option with credentials: `redis://user:pass@host:port`\n- **Secrets**: Never cache sensitive data (tokens, passwords, PII) without encryption\n- **Network**: Use TLS for Redis in production (`rediss://` protocol)\n- **Eviction**: Cached data persists until TTL expires or LRU evicts; plan accordingly\n\nReport security issues via GitHub issues with `[SECURITY]` prefix.\n\n## License\n\nBSD-3-Clause\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fvnykmshr%2Fobcachejs","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fvnykmshr%2Fobcachejs","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fvnykmshr%2Fobcachejs/lists"}