{"id":13487555,"url":"https://github.com/eggjs/redis","last_synced_at":"2025-12-29T23:20:29.324Z","repository":{"id":56032992,"uuid":"81744405","full_name":"eggjs/egg-redis","owner":"eggjs","description":"redis plugin for egg","archived":false,"fork":false,"pushed_at":"2024-04-07T09:46:35.000Z","size":73,"stargazers_count":270,"open_issues_count":8,"forks_count":68,"subscribers_count":14,"default_branch":"master","last_synced_at":"2024-10-29T21:05:54.314Z","etag":null,"topics":["egg","egg-plugin","redis-client"],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","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/eggjs.png","metadata":{"files":{"readme":"README.md","changelog":"History.md","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}},"created_at":"2017-02-12T17:43:51.000Z","updated_at":"2024-09-25T06:07:27.000Z","dependencies_parsed_at":"2022-08-15T11:50:35.035Z","dependency_job_id":"4d78dd93-149a-45e0-b2d6-2366b66d4c8f","html_url":"https://github.com/eggjs/egg-redis","commit_stats":{"total_commits":41,"total_committers":16,"mean_commits":2.5625,"dds":0.6829268292682926,"last_synced_commit":"8b16fd610d668df2cb720c08c106f180bdde47c7"},"previous_names":[],"tags_count":13,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eggjs%2Fegg-redis","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eggjs%2Fegg-redis/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eggjs%2Fegg-redis/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/eggjs%2Fegg-redis/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/eggjs","download_url":"https://codeload.github.com/eggjs/egg-redis/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":222133570,"owners_count":16936803,"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":["egg","egg-plugin","redis-client"],"created_at":"2024-07-31T18:01:00.580Z","updated_at":"2025-12-29T23:20:29.262Z","avatar_url":"https://github.com/eggjs.png","language":"JavaScript","readme":"# @eggjs/redis\n\n[![NPM version][npm-image]][npm-url]\n[![Node.js CI](https://github.com/eggjs/redis/actions/workflows/nodejs.yml/badge.svg)](https://github.com/eggjs/redis/actions/workflows/nodejs.yml)\n[![Test coverage][codecov-image]][codecov-url]\n[![Known Vulnerabilities][snyk-image]][snyk-url]\n[![npm download][download-image]][download-url]\n[![Node.js Version](https://img.shields.io/node/v/@eggjs/redis.svg?style=flat)](https://nodejs.org/en/download/)\n[![PRs Welcome](https://img.shields.io/badge/PRs-welcome-brightgreen.svg?style=flat-square)](https://makeapullrequest.com)\n![CodeRabbit Pull Request Reviews](https://img.shields.io/coderabbit/prs/github/eggjs/redis)\n\n[npm-image]: https://img.shields.io/npm/v/@eggjs/redis.svg?style=flat-square\n[npm-url]: https://npmjs.org/package/@eggjs/redis\n[codecov-image]: https://codecov.io/gh/eggjs/redis/branch/master/graph/badge.svg\n[codecov-url]: https://codecov.io/gh/eggjs/redis\n[snyk-image]: https://snyk.io/test/npm/@eggjs/redis/badge.svg?style=flat-square\n[snyk-url]: https://snyk.io/test/npm/@eggjs/redis\n[download-image]: https://img.shields.io/npm/dm/@eggjs/redis.svg?style=flat-square\n[download-url]: https://npmjs.org/package/@eggjs/redis\n\nValkey / Redis client (support [redis protocol](https://redis.io/docs/latest/develop/reference/protocol-spec/)) based on iovalkey for egg framework\n\n## Install\n\n```bash\nnpm i @eggjs/redis\n```\n\nValkey / Redis Plugin for egg, support egg application access to Valkey / Redis Service.\n\nThis plugin based on [ioredis](https://github.com/redis/ioredis).\nIf you want to know specific usage, you should refer to the document of [ioredis](https://github.com/redis/ioredis).\n\n## Configuration\n\nChange `${app_root}/config/plugin.js` to enable redis plugin:\n\n```js\nexports.redis = {\n  enable: true,\n  package: '@eggjs/redis',\n};\n```\n\nConfigure redis information in `${app_root}/config/config.default.js`:\n\n**Single Client**\n\n```javascript\nconfig.redis = {\n  client: {\n    port: 6379, // Redis port\n    host: '127.0.0.1', // Redis host\n    password: 'auth',\n    db: 0,\n  },\n};\n```\n\n**Multi Clients**\n\n```javascript\nconfig.redis = {\n  clients: {\n    foo: {\n      // instanceName. See below\n      port: 6379, // Redis port\n      host: '127.0.0.1', // Redis host\n      password: 'auth',\n      db: 0,\n    },\n    bar: {\n      port: 6379,\n      host: '127.0.0.1',\n      password: 'auth',\n      db: 1,\n    },\n  },\n};\n```\n\n**Sentinel**\n\n```javascript\nconfig.redis = {\n  client: {\n    // Sentinel instances\n    sentinels: [\n      {\n        port: 26379, // Sentinel port\n        host: '127.0.0.1', // Sentinel host\n      },\n      // other sentinel instance config\n    ],\n    name: 'mymaster', // Master name\n    password: 'auth',\n    db: 0,\n  },\n};\n```\n\n**No password**\n\nRedis support no authentication access, but we are highly recommend you to use redis `requirepass` in `redis.conf`.\n\n```bash\n$vim /etc/redis/redis.conf\n\nrequirepass xxxxxxxxxx  // xxxxxxxxxx is your password\n```\n\nBecause it may be cause security risk, refer:\n\n- \u003chttps://ruby-china.org/topics/28094\u003e\n- \u003chttps://zhuoroger.github.io/2016/07/29/redis-sec/\u003e\n\nIf you want to access redis with no password, use `password: null`.\n\nSee [ioredis API Documentation](https://github.com/redis/ioredis#basic-usage) for all available options.\n\n### Customize `ioredis` version\n\n`@eggjs/redis` using `ioredis@5` now, if you want to use other version of iovalkey or ioredis,\nyou can pass the instance by `config.redis.Redis`:\n\n```js\n// config/config.default.js\nconfig.redis = {\n  Redis: require('ioredis'), // customize ioredis version, only set when you needed\n  client: {\n    port: 6379, // Redis port\n    host: '127.0.0.1', // Redis host\n    password: 'auth',\n    db: 0,\n  },\n};\n```\n\n**weakDependent**\n\n```javascript\nconfig.redis = {\n  client: {\n    port: 6379, // Redis port\n    host: '127.0.0.1', // Redis host\n    password: 'auth',\n    db: 0,\n    weakDependent: true, // the redis instance won't block app start\n  },\n};\n```\n\n## Usage\n\nIn controller, you can use `app.redis` to get the redis instance, check [ioredis](https://github.com/redis/ioredis#basic-usage) to see how to use.\n\n```js\n// app/controller/home.js\n\nmodule.exports = app =\u003e {\n  return class HomeController extends app.Controller {\n    async index() {\n      const { ctx, app } = this;\n      // set\n      await app.redis.set('foo', 'bar');\n      // get\n      ctx.body = await app.redis.get('foo');\n    }\n  };\n};\n```\n\n### Multi Clients\n\nIf your Configure with multi clients, you can use `app.redis.get(instanceName)` to get the specific redis instance and use it like above.\n\n```js\n// app/controller/home.js\n\nmodule.exports = app =\u003e {\n  return class HomeController extends app.Controller {\n    async index() {\n      const { ctx, app } = this;\n      // set\n      await app.redis.get('instance1').set('foo', 'bar');\n      // get\n      ctx.body = await app.redis.get('instance1').get('foo');\n    }\n  };\n};\n```\n\n### Clients Depend on Redis Cluster\n\nBefore you start to use Redis Cluster, please checkout the [document](https://redis.io/topics/cluster-tutorial) first, especially confirm `cluster-enabled yes` in Redis Cluster configuration file.\n\nIn controller, you also can use `app.redis` to get the redis instance based on Redis Cluster.\n\n```js\n// app/config/config.default.js\nexports.redis = {\n  client: {\n    cluster: true,\n    nodes: [\n      {\n        host: '127.0.0.1',\n        port: '6379',\n        family: 'user',\n        password: 'password',\n        db: 'db',\n      },\n      {\n        host: '127.0.0.1',\n        port: '6380',\n        family: 'user',\n        password: 'password',\n        db: 'db',\n      },\n    ],\n  },\n};\n\n// app/controller/home.js\nmodule.exports = app =\u003e {\n  return class HomeController extends app.Controller {\n    async index() {\n      const { ctx, app } = this;\n      // set\n      await app.redis.set('foo', 'bar');\n      // get\n      ctx.body = await app.redis.get('foo');\n    }\n  };\n};\n```\n\n## For the local dev\n\nRun docker compose to start test redis service\n\n```bash\ndocker compose -f docker-compose.yml up -d\n```\n\nRun the unit tests\n\n```bash\nnpm test\n```\n\nStop test redis service\n\n```bash\ndocker compose -f docker-compose.yml down\n```\n\n## Questions \u0026 Suggestions\n\nPlease open an issue [here](https://github.com/eggjs/egg/issues).\n\n## License\n\n[MIT](LICENSE)\n\n## Contributors\n\n[![Contributors](https://contrib.rocks/image?repo=eggjs/redis)](https://github.com/eggjs/redis/graphs/contributors)\n\nMade with [contributors-img](https://contrib.rocks).\n","funding_links":[],"categories":["仓库","Plugins"],"sub_categories":["插件"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Feggjs%2Fredis","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Feggjs%2Fredis","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Feggjs%2Fredis/lists"}