Skip to content

Commit f66846e

Browse files
authored
feat:first implement (eggjs#3)
1 parent dcf260f commit f66846e

23 files changed

+438
-0
lines changed

.autod.conf.js

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
'use strict';
2+
3+
module.exports = {
4+
write: true,
5+
prefix: '^',
6+
test: [
7+
'test',
8+
'benchmark',
9+
],
10+
devdep: [
11+
'egg',
12+
'egg-bin',
13+
'autod',
14+
'eslint',
15+
'eslint-config-egg',
16+
'supertest',
17+
],
18+
exclude: [
19+
'./test/fixtures',
20+
],
21+
}

.eslintignore

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
test/fixtures
2+
coverage

.eslintrc

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"extends": "eslint-config-egg"
3+
}

.github/PULL_REQUEST_TEMPLATE.md

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
<!--
2+
Thank you for your pull request. Please review below requirements.
3+
Bug fixes and new features should include tests and possibly benchmarks.
4+
Contributors guide: https://github.com/eggjs/egg/blob/master/CONTRIBUTING.md
5+
6+
感谢您贡献代码。请确认下列 checklist 的完成情况。
7+
Bug 修复和新功能必须包含测试,必要时请附上性能测试。
8+
Contributors guide: https://github.com/eggjs/egg/blob/master/CONTRIBUTING.md
9+
-->
10+
11+
##### Checklist
12+
<!-- Remove items that do not apply. For completed items, change [ ] to [x]. -->
13+
14+
- [ ] `npm test` passes
15+
- [ ] tests and/or benchmarks are included
16+
- [ ] documentation is changed or added
17+
- [ ] commit message follows commit guidelines
18+
19+
##### Affected core subsystem(s)
20+
<!-- Provide affected core subsystem(s). -->
21+
22+
23+
##### Description of change
24+
<!-- Provide a description of the change below this comment. -->

.gitignore

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
logs/
2+
npm-debug.log
3+
node_modules/
4+
coverage/
5+
.idea/
6+
run/
7+
.DS_Store
8+
*.swp
9+

.travis.yml

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
sudo: false
2+
language: node_js
3+
node_js:
4+
- '6'
5+
- '7'
6+
install:
7+
- npm i npminstall && npminstall
8+
script:
9+
- npm run ci
10+
after_script:
11+
- npminstall codecov && codecov
12+
services:
13+
- redis-server

History.md

Whitespace-only changes.

LICENSE

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) Alibaba Group Holding Limited and other contributors.
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.

README.zh-CN.md

+56
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
# egg-redis
2+
3+
[![NPM version][npm-image]][npm-url]
4+
[![build status][travis-image]][travis-url]
5+
[![Test coverage][codecov-image]][codecov-url]
6+
[![David deps][david-image]][david-url]
7+
[![Known Vulnerabilities][snyk-image]][snyk-url]
8+
[![npm download][download-image]][download-url]
9+
10+
[npm-image]: https://img.shields.io/npm/v/egg-redis.svg?style=flat-square
11+
[npm-url]: https://npmjs.org/package/egg-redis
12+
[travis-image]: https://img.shields.io/travis/eggjs/egg-redis.svg?style=flat-square
13+
[travis-url]: https://travis-ci.org/eggjs/egg-redis
14+
[codecov-image]: https://img.shields.io/codecov/c/github/eggjs/egg-redis.svg?style=flat-square
15+
[codecov-url]: https://codecov.io/github/eggjs/egg-redis?branch=master
16+
[david-image]: https://img.shields.io/david/eggjs/egg-redis.svg?style=flat-square
17+
[david-url]: https://david-dm.org/eggjs/egg-redis
18+
[snyk-image]: https://snyk.io/test/npm/egg-redis/badge.svg?style=flat-square
19+
[snyk-url]: https://snyk.io/test/npm/egg-redis
20+
[download-image]: https://img.shields.io/npm/dm/egg-redis.svg?style=flat-square
21+
[download-url]: https://npmjs.org/package/egg-redis
22+
23+
Redis client(support redis portocal) based on ioredis for egg framework
24+
25+
# This project is still working in progress
26+
27+
## Install
28+
29+
```bash
30+
$ npm i egg-redis --save
31+
```
32+
33+
redis Plugin for egg, support egg application access to redis.
34+
35+
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).
36+
37+
## Configuration
38+
39+
Change `${app_root}/config/plugin.js` to enable redis plugin:
40+
41+
```js
42+
exports.redis = {
43+
enable: true,
44+
package: 'egg-redis',
45+
};
46+
```
47+
48+
Configure redis information in `${app_root}/config/config.default.js`:
49+
50+
## Questions & Suggestions
51+
52+
Please open an issue [here](https://github.com/eggjs/egg/issues).
53+
54+
## License
55+
56+
[MIT](LICENSE)

agent.js

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
'use strict';
2+
3+
const redis = require('./lib/redis');
4+
5+
module.exports = agent => {
6+
if (agent.config.redis.agent) redis(agent);
7+
};

app.js

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
'use strict';
2+
3+
const redis = require('./lib/redis');
4+
5+
module.exports = app => {
6+
if (app.config.redis.app) redis(app);
7+
};

config/config.default.js

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
'use strict';
2+
3+
exports.redis = {
4+
default: {
5+
},
6+
app: true,
7+
agent: false,
8+
9+
// Single Redis
10+
// client: {
11+
// host: 'host',
12+
// port: 'port',
13+
// family: 'user',
14+
// password: 'password',
15+
// db: 'db',
16+
// },
17+
18+
// Cluster Redis
19+
// client: [
20+
// cluster: true,
21+
// nodes: {
22+
// host: 'host',
23+
// port: 'port',
24+
// family: 'user',
25+
// password: 'password',
26+
// db: 'db',
27+
// },{
28+
// host: 'host',
29+
// port: 'port',
30+
// family: 'user',
31+
// password: 'password',
32+
// db: 'db',
33+
// }],
34+
35+
// Multi Redis
36+
// clients: {
37+
// instance1: {
38+
// host: 'host',
39+
// port: 'port',
40+
// family: 'user',
41+
// password: 'password',
42+
// db: 'db',
43+
// },
44+
// instance2: {
45+
// host: 'host',
46+
// port: 'port',
47+
// family: 'user',
48+
// password: 'password',
49+
// db: 'db',
50+
// },
51+
// },
52+
};

lib/redis.js

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
'use strict';
2+
3+
const assert = require('assert');
4+
const Redis = require('ioredis');
5+
6+
module.exports = app => {
7+
app.addSingleton('redis', createClient);
8+
};
9+
10+
let count = 0;
11+
12+
function createClient(config, app) {
13+
let client;
14+
15+
if (config.cluster === true) {
16+
assert(config.nodes && config.nodes.length !== 0, '[egg-redis] cluster nodes configuration is required when use cluster redis');
17+
18+
config.nodes.forEach(client => {
19+
assert(client.host && client.port && client.password !== undefined && client.db,
20+
`[egg-redis] 'host: ${client.host}', 'port: ${client.port}', 'password: ${client.password}', 'db: ${client.db}' are required on config`);
21+
});
22+
app.coreLogger.info('[egg-redis] cluster connecting start');
23+
24+
client = new Redis.Cluster(config.nodes, config);
25+
client.on('connect', function() {
26+
app.coreLogger.info('[egg-redis] cluster connect success');
27+
});
28+
client.on('error', function(error) {
29+
app.coreLogger.error(error);
30+
});
31+
} else {
32+
assert(config.host && config.port && config.password !== undefined && config.db,
33+
`[egg-redis] 'host: ${config.host}', 'port: ${config.port}', 'password: ${config.password}', 'db: ${config.db}' are required on config`);
34+
35+
app.coreLogger.info('[egg-redis] connecting redis://:%s@%s:%s/%s',
36+
config.password, config.host, config.port, config.db);
37+
38+
client = new Redis(config);
39+
client.on('connect', function() {
40+
app.coreLogger.info('[egg-redis] connect success on redis://:%s@%s:%s/%s',
41+
config.password, config.host, config.port, config.db);
42+
});
43+
client.on('error', function(error) {
44+
app.coreLogger.error(error);
45+
});
46+
}
47+
48+
app.beforeStart(function* () {
49+
const result = yield client.time();
50+
const index = count++;
51+
app.coreLogger.info(`[egg-redis] instance[${index}] status OK, redis currentTime: ${result[0]}`);
52+
});
53+
54+
return client;
55+
}

package.json

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
{
2+
"name": "egg-redis",
3+
"version": "0.0.2",
4+
"description": "Redis plugin for egg",
5+
"eggPlugin": {
6+
"name": "redis"
7+
},
8+
"keywords": [
9+
"egg",
10+
"eggPlugin",
11+
"egg-plugin",
12+
"redis",
13+
"database"
14+
],
15+
"dependencies": {
16+
"ioredis": "^2.5.0"
17+
},
18+
"devDependencies": {
19+
"autod": "^2.7.1",
20+
"egg": "^0.9.0",
21+
"egg-bin": "^2.0.2",
22+
"egg-mock": "^2.3.1",
23+
"eslint": "^3.14.1",
24+
"eslint-config-egg": "^3.2.0",
25+
"supertest": "^3.0.0",
26+
"utility": "^1.9.0"
27+
},
28+
"engines": {
29+
"node": ">=6.0.0"
30+
},
31+
"scripts": {
32+
"test": "npm run lint -- --fix && npm run test-local",
33+
"test-local": "egg-bin test",
34+
"cov": "egg-bin cov",
35+
"lint": "eslint .",
36+
"ci": "npm run lint && npm run cov",
37+
"autod": "autod"
38+
},
39+
"ci": {
40+
"version": "6, 7"
41+
},
42+
"repository": {
43+
"type": "git",
44+
"url": "git+https://github.com/eggjs/egg-redis.git"
45+
},
46+
"bugs": {
47+
"url": "https://github.com/eggjs/egg/issues"
48+
},
49+
"homepage": "https://github.com/eggjs/egg-redis#readme",
50+
"author": "jtyjty99999",
51+
"license": "MIT"
52+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
'use strict';
2+
3+
module.exports = function* () {
4+
const a = yield this.redis.get(a);
5+
6+
this.body = {
7+
status: 'success',
8+
a,
9+
};
10+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
'use strict';
2+
3+
module.exports = function(app) {
4+
app.get('/', app.controller.home);
5+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
'use strict';
2+
3+
exports.redis = {
4+
client: {
5+
host: '127.0.0.1',
6+
port: 6379,
7+
password: '',
8+
db: '0',
9+
},
10+
agent:true
11+
};
+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"name": "redisapp"
3+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
'use strict';
2+
3+
module.exports = function* () {
4+
const a = yield this.redis.get(a);
5+
6+
this.body = {
7+
status: 'success',
8+
a,
9+
};
10+
};
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
'use strict';
2+
3+
module.exports = function(app) {
4+
app.get('/', app.controller.home);
5+
};

0 commit comments

Comments
 (0)