Skip to content

Commit ad1772a

Browse files
committed
fix: added @ladjs/redis
1 parent 36e5765 commit ad1772a

File tree

3 files changed

+21
-50
lines changed

3 files changed

+21
-50
lines changed

index.js

Lines changed: 8 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ const http2 = require('spdy');
44
const Cabin = require('cabin');
55
const I18N = require('@ladjs/i18n');
66
const Koa = require('koa');
7+
const Redis = require('@ladjs/redis');
78
const StoreIPAddress = require('@ladjs/store-ip-address');
89
const Timeout = require('koa-better-timeout');
910
const _ = require('lodash');
@@ -48,6 +49,13 @@ class API {
4849
// initialize the app
4950
const app = new Koa();
5051

52+
// initialize redis
53+
const client = new Redis(
54+
this.config.redis,
55+
logger,
56+
this.config.redisMonitor
57+
);
58+
5159
// store the server initialization
5260
// so that we can gracefully exit
5361
// later on with `server.close()`
@@ -57,51 +65,6 @@ class API {
5765
app.on('error', (err, ctx) => ctx.logger.error(err));
5866
app.on('log', logger.log);
5967

60-
// check if we've binded _any_ events otherwise
61-
// bind all normal events and assume we use the default
62-
// <https://github.com/luin/ioredis#events>
63-
const client = this.config.redisClient;
64-
// go through each event listener type for ioredis and check
65-
// if we've binded any listeners already
66-
// <https://nodejs.org/api/events.html#events_emitter_listeners_eventname>
67-
const listeners = [
68-
'connect',
69-
'ready',
70-
'error',
71-
'close',
72-
'reconnecting',
73-
'end',
74-
'+node',
75-
'-node',
76-
'node error'
77-
];
78-
let bindListeners = true;
79-
for (let i = 0; i < listeners.length; i++) {
80-
if (client.listeners(listeners[i]).length > 0) {
81-
bindListeners = false;
82-
break;
83-
}
84-
}
85-
86-
if (bindListeners) {
87-
client.on('connect', () =>
88-
app.emit('log', 'debug', 'redis connection established')
89-
);
90-
client.on('ready', () =>
91-
app.emit('log', 'debug', 'redis connection ready')
92-
);
93-
client.on('error', err => app.emit('error', err));
94-
client.on('close', () =>
95-
app.emit('log', 'debug', 'redis connection closed')
96-
);
97-
client.on('reconnecting', () =>
98-
app.emit('log', 'debug', 'redis reconnecting')
99-
);
100-
client.on('end', () =>
101-
app.emit('log', 'debug', 'redis connection ended')
102-
);
103-
}
104-
10568
// only trust proxy if enabled
10669
app.proxy = boolean(process.env.TRUST_PROXY);
10770

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@
2222
"dependencies": {
2323
"@koa/router": "^8.0.1",
2424
"@ladjs/i18n": "^1.2.1",
25-
"@ladjs/shared-config": "^0.0.4",
25+
"@ladjs/redis": "^1.0.2",
26+
"@ladjs/shared-config": "^0.0.7",
2627
"@ladjs/store-ip-address": "^0.0.6",
2728
"boolean": "^1.0.0",
2829
"cabin": "^5.0.1",

yarn.lock

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -453,13 +453,20 @@
453453
qs "^6.8.0"
454454
titleize "^2.1.0"
455455

456-
"@ladjs/shared-config@^0.0.4":
457-
version "0.0.4"
458-
resolved "https://registry.yarnpkg.com/@ladjs/shared-config/-/shared-config-0.0.4.tgz#5b202e248d5f076471c247f3376821eeef18ba3a"
459-
integrity sha512-uSJkPu6jE/fafvFP9J2mCkYacqNrTI8bR9az35wqQSY7/2GMWRRw52zVCYRTzwc5mH3VvV5l0uW+jf9VnSB3eA==
456+
"@ladjs/redis@^1.0.2":
457+
version "1.0.2"
458+
resolved "https://registry.yarnpkg.com/@ladjs/redis/-/redis-1.0.2.tgz#04dcfcd96606375927e0a07f3b088e33da27d1bc"
459+
integrity sha512-B7SYfjj0GRfQ8R7I+dvGY0Xk9NHoxEgoxC83vQUFHFWsWxIOh4R2bBKxopba+/7I+NX7GQxZVMYmAlrn7ajJNg==
460460
dependencies:
461461
ioredis "^4.14.0"
462462

463+
"@ladjs/shared-config@^0.0.7":
464+
version "0.0.7"
465+
resolved "https://registry.yarnpkg.com/@ladjs/shared-config/-/shared-config-0.0.7.tgz#85cf61a38aebdbd57a8487edddc899e27baae1fc"
466+
integrity sha512-FI2jEwLLx+ZhZ527R7/+UhxaICoC0a3faK9/ydlaBsj6UXq+0J9DvQwPzfUBsgXKc4LXtU/UYmIDW07pzRVXqg==
467+
dependencies:
468+
boolean "^1.0.0"
469+
463470
"@ladjs/store-ip-address@^0.0.6":
464471
version "0.0.6"
465472
resolved "https://registry.yarnpkg.com/@ladjs/store-ip-address/-/store-ip-address-0.0.6.tgz#1cf863b00e076a797117dd33b848141afd52d300"

0 commit comments

Comments
 (0)