Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
test/fixtures
coverage
__snapshots__
5 changes: 4 additions & 1 deletion .eslintrc
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
{
"extends": "eslint-config-egg"
"extends": [
"eslint-config-egg/typescript",
"eslint-config-egg/lib/rules/enforce-node-prefix"
]
}
2 changes: 1 addition & 1 deletion .github/workflows/nodejs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@ jobs:
name: Node.js
uses: node-modules/github-actions/.github/workflows/node-test-mysql.yml@master
with:
version: '16, 18, 20, 22'
version: '18.19.0, 18, 20, 22'
secrets:
CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}
12 changes: 8 additions & 4 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,12 @@ logs/
npm-debug.log
node_modules/
coverage/
.idea/
run/
test/fixtures/**/run
.DS_Store
*.swp
.nyc_output/
.tshy*
.eslintcache
dist
package-lock.json
.package-lock.json
test/fixtures/**/*.d.ts
run/
86 changes: 46 additions & 40 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,34 +1,35 @@
# egg-redis
# @eggjs/redis

[![NPM version][npm-image]][npm-url]
[![Node.js CI](https://github.com/eggjs/redis/actions/workflows/nodejs.yml/badge.svg)](https://github.com/eggjs/redis/actions/workflows/nodejs.yml)
[![Test coverage][codecov-image]][codecov-url]
[![Known Vulnerabilities][snyk-image]][snyk-url]
[![npm download][download-image]][download-url]
[![Node.js Version](https://img.shields.io/node/v/egg-redis.svg?style=flat)](https://nodejs.org/en/download/)
[![Node.js Version](https://img.shields.io/node/v/@eggjs/redis.svg?style=flat)](https://nodejs.org/en/download/)
[![PRs Welcome](https://img.shields.io/badge/PRs-welcome-brightgreen.svg?style=flat-square)](https://makeapullrequest.com)
![CodeRabbit Pull Request Reviews](https://img.shields.io/coderabbit/prs/github/eggjs/redis)

[npm-image]: https://img.shields.io/npm/v/egg-redis.svg?style=flat-square
[npm-url]: https://npmjs.org/package/egg-redis
[npm-image]: https://img.shields.io/npm/v/@eggjs/redis.svg?style=flat-square
[npm-url]: https://npmjs.org/package/@eggjs/redis
[codecov-image]: https://codecov.io/gh/eggjs/redis/branch/master/graph/badge.svg
[codecov-url]: https://codecov.io/gh/eggjs/redis
[snyk-image]: https://snyk.io/test/npm/egg-redis/badge.svg?style=flat-square
[snyk-url]: https://snyk.io/test/npm/egg-redis
[download-image]: https://img.shields.io/npm/dm/egg-redis.svg?style=flat-square
[download-url]: https://npmjs.org/package/egg-redis
[snyk-image]: https://snyk.io/test/npm/@eggjs/redis/badge.svg?style=flat-square
[snyk-url]: https://snyk.io/test/npm/@eggjs/redis
[download-image]: https://img.shields.io/npm/dm/@eggjs/redis.svg?style=flat-square
[download-url]: https://npmjs.org/package/@eggjs/redis

Redis client(support redis protocol) based on ioredis for egg framework
Valkey / Redis client (support [redis protocol](https://redis.io/docs/latest/develop/reference/protocol-spec/)) based on iovalkey for egg framework

## Install

```bash
npm i egg-redis --save
npm i @eggjs/redis
```

redis Plugin for egg, support egg application access to redis.
Valkey / Redis Plugin for egg, support egg application access to Valkey / Redis Service.

This plugin based on [ioredis](https://github.com/luin/ioredis), if you want to know specific usage, you should refer to the document of [ioredis](https://github.com/luin/ioredis).
This plugin based on [ioredis](https://github.com/redis/ioredis).
If you want to know specific usage, you should refer to the document of [ioredis](https://github.com/redis/ioredis).

## Configuration

Expand All @@ -37,7 +38,7 @@ Change `${app_root}/config/plugin.js` to enable redis plugin:
```js
exports.redis = {
enable: true,
package: 'egg-redis',
package: '@eggjs/redis',
};
```

Expand Down Expand Up @@ -82,10 +83,14 @@ config.redis = {
```javascript
config.redis = {
client: {
sentinels: [{ // Sentinel instances
port: 26379, // Sentinel port
host: '127.0.0.1', // Sentinel host
}],
// Sentinel instances
sentinels: [
{
port: 26379, // Sentinel port
host: '127.0.0.1', // Sentinel host
},
// other sentinel instance config
],
name: 'mymaster', // Master name
password: 'auth',
db: 0
Expand All @@ -110,11 +115,12 @@ Because it may be cause security risk, refer:

If you want to access redis with no password, use `password: null`.

See [ioredis API Documentation](https://github.com/luin/ioredis/blob/master/API.md#new_Redis) for all available options.
See [ioredis API Documentation](https://github.com/redis/ioredis#basic-usage) for all available options.

### Customize `ioredis` version

`egg-redis` using ioredis@4 now, if you want to use other version of ioredis, you can pass the instance by `config.redis.Redis`:
`@eggjs/redis` using `ioredis@5` now, if you want to use other version of iovalkey or ioredis,
you can pass the instance by `config.redis.Redis`:

```js
// config/config.default.js
Expand All @@ -138,14 +144,14 @@ config.redis = {
host: '127.0.0.1', // Redis host
password: 'auth',
db: 0,
weakDependent: true, // this redis instance won't block app start
weakDependent: true, // the redis instance won't block app start
},
}
```

## Usage

In controller, you can use `app.redis` to get the redis instance, check [ioredis](https://github.com/luin/ioredis#basic-usage) to see how to use.
In 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.

```js
// app/controller/home.js
Expand Down Expand Up @@ -190,30 +196,30 @@ Before you start to use Redis Cluster, please checkout the [document](https://re
In controller, you also can use `app.redis` to get the redis instance based on Redis Cluster.

```js

// app/config/config.default.js

exports.redis = {
client: {
cluster: true,
nodes: [{
host: '127.0.0.1',
port: '6379',
family: 'user',
password: 'password',
db: 'db',
}, {
host: '127.0.0.1',
port: '6380',
family: 'user',
password: 'password',
db: 'db',
}]
},
client: {
cluster: true,
nodes: [
{
host: '127.0.0.1',
port: '6379',
family: 'user',
password: 'password',
db: 'db',
},
{
host: '127.0.0.1',
port: '6380',
family: 'user',
password: 'password',
db: 'db',
},
],
},
};

// app/controller/home.js

module.exports = app => {
return class HomeController extends app.Controller {
async index() {
Expand Down
12 changes: 12 additions & 0 deletions __snapshots__/redis.test.ts.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
exports['test/redis.test.js default config should make default config stable 1'] = {
"default": {},
"app": true,
"agent": false,
"supportTimeCommand": true,
"client": {
"host": "127.0.0.1",
"port": 6379,
"password": "",
"db": "0"
}
}
7 changes: 0 additions & 7 deletions agent.js

This file was deleted.

7 changes: 0 additions & 7 deletions app.js

This file was deleted.

58 changes: 0 additions & 58 deletions config/config.default.js

This file was deleted.

12 changes: 12 additions & 0 deletions example/hello/app/router.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { Application } from 'egg';

export default (app: Application) => {
const { router } = app;

router.get('/', async ctx => {
const redis = app.redis;
await redis.set('foo', 'bar');
const cacheValue = await redis.get('foo');
ctx.body = cacheValue;
});
};
3 changes: 3 additions & 0 deletions example/hello/config/config.default.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default {
keys: 'bala',
};
6 changes: 6 additions & 0 deletions example/hello/config/plugin.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export default {
redis: {
enable: true,
path: '../../../../..',
},
};
4 changes: 4 additions & 0 deletions example/hello/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"name": "hello-redis",
"type": "module"
}
12 changes: 12 additions & 0 deletions example/hello/start.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import path from 'node:path';
import { fileURLToPath } from 'node:url';
import { startEgg } from 'egg';

const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);

const app = await startEgg({
baseDir: __dirname,
});

console.log(`Server started at http://localhost:${app.config.cluster.listen.port}`);
10 changes: 10 additions & 0 deletions example/hello/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"extends": "@eggjs/tsconfig",
"compilerOptions": {
"strict": true,
"noImplicitAny": true,
"target": "ES2022",
"module": "NodeNext",
"moduleResolution": "NodeNext"
}
}
4 changes: 4 additions & 0 deletions example/hello/typings/index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
// make sure to import egg typings and let typescript know about it
// @see https://github.com/whxaxes/blog/issues/11
// and https://www.typescriptlang.org/docs/handbook/declaration-merging.html
import '../../../src/index.ts';
25 changes: 0 additions & 25 deletions index.d.ts

This file was deleted.

Loading
Loading