{"id":15499684,"url":"https://github.com/softwaresing/cache-bridge","last_synced_at":"2026-05-03T19:32:09.554Z","repository":{"id":52450237,"uuid":"381097706","full_name":"SoftwareSing/cache-bridge","owner":"SoftwareSing","description":null,"archived":false,"fork":false,"pushed_at":"2023-05-07T12:45:35.000Z","size":371,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-03-19T21:25:24.196Z","etag":null,"topics":["cache","cachemanager","lru-cache","redis","typescript"],"latest_commit_sha":null,"homepage":"","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/SoftwareSing.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"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}},"created_at":"2021-06-28T16:34:56.000Z","updated_at":"2023-05-07T12:35:53.000Z","dependencies_parsed_at":"2025-03-12T21:02:36.420Z","dependency_job_id":"0c0a1bcc-64c0-41ee-8a27-ccd457aa4f49","html_url":"https://github.com/SoftwareSing/cache-bridge","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/SoftwareSing%2Fcache-bridge","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/SoftwareSing%2Fcache-bridge/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/SoftwareSing%2Fcache-bridge/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/SoftwareSing%2Fcache-bridge/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/SoftwareSing","download_url":"https://codeload.github.com/SoftwareSing/cache-bridge/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":246071085,"owners_count":20719044,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","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":["cache","cachemanager","lru-cache","redis","typescript"],"created_at":"2024-10-02T08:54:39.609Z","updated_at":"2026-05-03T19:32:09.526Z","avatar_url":"https://github.com/SoftwareSing.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# cache-bridge\n\nSimplify managing data between cache and database.\n\n## Features\n\n- Get data from cache, and automatically copy it from database when cache miss.\n- Will acquire the lock before accessing database, avoid [Cache stampede](https://en.wikipedia.org/wiki/Cache_stampede).\n- Supports [lru-cache](https://www.npmjs.com/package/lru-cache).\n- Supports Redis, via [node-redis](https://www.npmjs.com/package/redis) or [ioredis](https://www.npmjs.com/package/ioredis).\n\n## Example\n\n```typescript\nimport { createClient } from 'redis'\nimport { MongoClient, ObjectId } from 'mongodb'\nimport { createBridge, RedisCacheClient } from './src'\n\nconst redis = createClient()\nconst mongo = new MongoClient('mongodb://localhost:27017')\n\nasync function main (): Promise\u003cvoid\u003e {\n  await redis.connect()\n  await mongo.connect()\n\n  const { bridge } = createBridge({\n    cacheClient: new RedisCacheClient({ client: redis }),\n    prefix: 'cache',\n    ttl: 5000,\n    // set how to get data from the DB\n    get: async (id) =\u003e {\n      return await mongo.db().collection('a').findOne({ _id: new ObjectId(id) })\n    },\n    // set how to get multiple data from the DB\n    getMany: async (idList) =\u003e {\n      const list = await mongo.db().collection('a')\n        .find({\n          _id: { $in: idList.map((id) =\u003e new ObjectId(id)) }\n        })\n        .toArray()\n      return new Map(\n        list.map((data) =\u003e [data._id.toHexString(), data])\n      )\n    }\n  })\n\n  const consoleData = async (id: string): Promise\u003cvoid\u003e =\u003e {\n    console.log(await bridge.get(id))\n  }\n\n  // when called sequentially\n  const id1 = '000000000000000000000001'\n  await consoleData(id1) // get data from the DB\n  await consoleData(id1) // get data from the cache\n  await consoleData(id1) // get data from the cache\n\n  // when called simultaneously\n  const id2 = '000000000000000000000002'\n  await Promise.all([\n    consoleData(id2), // get data from the DB\n    consoleData(id2), // wait for the previous line to store the data in the cache and get data from the cache\n    consoleData(id2) // wait for and get data from the cache\n  ])\n}\n\nmain()\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsoftwaresing%2Fcache-bridge","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsoftwaresing%2Fcache-bridge","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsoftwaresing%2Fcache-bridge/lists"}