Skip to content

Commit 153ba80

Browse files
committed
refactor: use ioredis instead of redis
1 parent ddced15 commit 153ba80

File tree

6 files changed

+84
-47
lines changed

6 files changed

+84
-47
lines changed

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@
5757
"cron": "^1.3.0",
5858
"fast-json-stringify": "^2.2.3",
5959
"humanize-number": "^0.0.2",
60+
"ioredis": "^4.17.3",
6061
"kcors": "^2.2.1",
6162
"koa": "2.13.0",
6263
"koa-bodyparser": "^4.2.0",
@@ -74,7 +75,6 @@
7475
"nodemailer": "^6.4.10",
7576
"passthrough-counter": "^1.0.0",
7677
"pify": "^5.0.0",
77-
"redis": "^3.0.2",
7878
"semver": "^7.3.2",
7979
"sequelize": "^6.3.3",
8080
"simple-netease-cloud-music": "^0.5.0",

src/cache.js

+19-13
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
'use strict'
22
// Import Packages
33
const nconf = require('nconf')
4-
const bluebrid = require('bluebird')
54
const winston = require('winston')
65
const colors = require('colors')
7-
// Promisify Redis
8-
const redis = bluebrid.promisifyAll(require('redis'))
6+
7+
const Redis = require('ioredis')
98

109
let connectionFailedAttemp = 0
1110
class cache {
@@ -14,15 +13,22 @@ class cache {
1413
const config = {
1514
host: nconf.get('redis:host') || '127.0.0.1',
1615
port: nconf.get('redis:port') || 6379,
17-
password: nconf.get('redis:password') && nconf.get('redis:password') !== '' ? nconf.get('redis:password') : false,
18-
db: nconf.get('redis:database') || 0
16+
db: nconf.get('redis:database') || 0,
17+
family: nconf.get('redis:family') || 4,
18+
reconnectOnError: (err) => {
19+
const targetError = 'READONLY'
20+
if (err.message.includes(targetError)) {
21+
// Only reconnect when the error contains "READONLY"
22+
return true
23+
}
24+
}
1925
}
20-
if (!config.password) {
21-
delete config.password
26+
if (nconf.get('redis:password') && (nconf.get('redis:password') !== '')) {
27+
config.password = nconf.get('redis:password')
2228
}
2329
// Connect Redis
2430
if (!newConnection) {
25-
this.redis = redis.createClient(config)
31+
this.redis = new Redis(config)
2632
this.redis.on('connect', () => {
2733
connectionFailedAttemp = 0 // clear the attemp count
2834
})
@@ -38,7 +44,7 @@ class cache {
3844
})
3945
return true
4046
}
41-
const client = redis.createClient(config)
47+
const client = new Redis(config)
4248
return client
4349
}
4450

@@ -52,22 +58,22 @@ class cache {
5258

5359
static command (commands, ...params) {
5460
this.connectOrSkip()
55-
return this.redis[commands + 'Async'](...params)
61+
return this.redis[commands](...params)
5662
}
5763

5864
static set (key, v, time) {
5965
this.connectOrSkip()
6066
const value = typeof v === 'object' ? JSON.stringify(v) : v
6167
if (time) {
62-
return this.redis.setAsync('cache:' + key, value, 'EX', time)
68+
return this.redis.set('cache:' + key, value, 'EX', time)
6369
} else {
64-
return this.redis.setAsync('cache:' + key, value)
70+
return this.redis.set('cache:' + key, value)
6571
}
6672
}
6773

6874
static async get (key, toJson = true) {
6975
this.connectOrSkip()
70-
const data = await this.redis.getAsync('cache:' + key)
76+
const data = await this.redis.get('cache:' + key)
7177
if (toJson) {
7278
try {
7379
const json = JSON.parse(data)

src/controllers/hitokoto.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ async function hitokoto (ctx, next) {
9191
category = targetCategories[Math.floor(Math.random() * targetCategories.length)]
9292
// get hitokoto sentences id
9393
const client = AB.getClient()
94-
const uuids = await client.zrangebyscoreAsync('hitokoto:bundle:category:' + category, minLength, maxLength)
94+
const uuids = await client.zrangebyscore('hitokoto:bundle:category:' + category, minLength, maxLength)
9595
if (uuids.length === 0) {
9696
ctx.status = 404
9797
ctx.body = {
@@ -129,7 +129,7 @@ async function hitokoto (ctx, next) {
129129
const category = targetCategories[Math.floor(Math.random() * targetCategories.length)]
130130
// get hitokoto sentences id
131131
const client = AB.getClient()
132-
const uuids = await client.zrangebyscoreAsync('hitokoto:bundle:category:' + category, minLength, maxLength)
132+
const uuids = await client.zrangebyscore('hitokoto:bundle:category:' + category, minLength, maxLength)
133133
if (uuids.length === 0) {
134134
ctx.status = 404
135135
ctx.body = {

src/extensions/sentencesABSwitcher.js

+22-16
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,10 @@
33
// intended to implement a a/b redis switcher to acquire a non-aware sentences update.
44

55
const Cache = require('../cache')
6-
const bluebrid = require('bluebird')
76
const nconf = require('nconf')
87
const colors = require('colors/safe')
98
const winston = require('winston')
10-
const redis = bluebrid.promisifyAll(require('redis'))
9+
const Redis = require('ioredis')
1110

1211
let connectionFailedAttemp = 0
1312
const databaseA = nconf.get('sentences_ab_switchter:a') || 1
@@ -19,19 +18,26 @@ class SentencesABSwitcher extends Cache {
1918
const config = {
2019
host: nconf.get('redis:host') || '127.0.0.1',
2120
port: nconf.get('redis:port') || 6379,
22-
password: nconf.get('redis:password') && nconf.get('redis:password') !== '' ? nconf.get('redis:password') : false,
23-
db: target === 'a' ? databaseA : databaseB // default connect to A database
21+
db: target === 'a' ? databaseA : databaseB, // default connect to A database,
22+
family: nconf.get('redis:family') || 4,
23+
reconnectOnError: (err) => {
24+
const targetError = 'READONLY'
25+
if (err.message.includes(targetError)) {
26+
// Only reconnect when the error contains "READONLY"
27+
return true
28+
}
29+
}
2430
}
25-
if (!config.password) {
26-
delete config.password
31+
if (nconf.get('redis:password') && (nconf.get('redis:password') !== '')) {
32+
config.password = nconf.get('redis:password')
2733
}
2834
// Connect Redis
29-
const tmp = redis.createClient(config)
35+
const tmp = new Redis(config)
3036
tmp.on('connect', () => {
3137
connectionFailedAttemp = 0 // clear the attemp count
3238
})
3339
tmp.on('error', err => {
34-
console.log(colors.red(err.stack))
40+
winston.error(colors.red(err.stack))
3541
if (connectionFailedAttemp >= 3) {
3642
winston.error('[AB] attemp to connect to redis ' + connectionFailedAttemp + ' times, but all failed, process exiting.')
3743
process.exit(1)
@@ -67,22 +73,22 @@ class SentencesABSwitcher extends Cache {
6773
this.connectOrSkip()
6874
const param = params
6975
param[0] = 'cache:' + param[0]
70-
return this.redis[commands + 'Async'](param)
76+
return this.redis[commands](param)
7177
}
7278

7379
static set (key, v, time) {
7480
this.connectOrSkip()
7581
const value = typeof v === 'object' ? JSON.stringify(v) : v
7682
if (time) {
77-
return this.redis.setAsync('cache:' + key, value, 'EX', time)
83+
return this.redis.set('cache:' + key, value, 'EX', time)
7884
} else {
79-
return this.redis.setAsync('cache:' + key, value)
85+
return this.redis.set('cache:' + key, value)
8086
}
8187
}
8288

8389
static async get (key, toJson = true) {
8490
this.connectOrSkip()
85-
const data = await this.redis.getAsync('cache:' + key)
91+
const data = await this.redis.get('cache:' + key)
8692
if (toJson) {
8793
try {
8894
const json = JSON.parse(data)
@@ -110,20 +116,20 @@ class WrapperRedis {
110116
}
111117

112118
command (commands, ...params) {
113-
return this.redis[commands + 'Async'](...params)
119+
return this.redis[commands](...params)
114120
}
115121

116122
set (key, v, time) {
117123
const value = typeof v === 'object' ? JSON.stringify(v) : v
118124
if (time) {
119-
return this.redis.setAsync('cache:' + key, value, 'EX', time)
125+
return this.redis.set('cache:' + key, value, 'EX', time)
120126
} else {
121-
return this.redis.setAsync('cache:' + key, value)
127+
return this.redis.set('cache:' + key, value)
122128
}
123129
}
124130

125131
async get (key, toJson = true) {
126-
const data = await this.redis.getAsync('cache:' + key)
132+
const data = await this.redis.get('cache:' + key)
127133
if (toJson) {
128134
try {
129135
const json = JSON.parse(data)

src/task/updateSentencesTask.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ async function Task () {
7777
}
7878
await Promise.all([
7979
SideAB.set('hitokoto:sentence:' + sentence.uuid, sentence),
80-
rClient.zaddAsync(['hitokoto:bundle:category:' + category.key, sentence.length, sentence.uuid])
80+
rClient.zadd(['hitokoto:bundle:category:' + category.key, sentence.length, sentence.uuid])
8181
])
8282
}
8383
// 保存句子长度范围
@@ -131,7 +131,7 @@ async function Task () {
131131
}
132132
await Promise.all([
133133
SideAB.set('hitokoto:sentence:' + sentence.uuid, sentence),
134-
rClient.zaddAsync(['hitokoto:bundle:category:' + category.key, sentence.length, sentence.uuid])
134+
rClient.zadd(['hitokoto:bundle:category:' + category.key, sentence.length, sentence.uuid])
135135
])
136136
}
137137
// 保存句子长度范围
@@ -176,7 +176,7 @@ async function Task () {
176176
// 保存句子长度范围
177177
await queue.set(`hitokoto:bundle:category:${queue.key}:max`, maxLength)
178178
await queue.set(`hitokoto:bundle:category:${queue.key}:min`, minLength)
179-
await queue.execAsync()
179+
await queue.exec()
180180
queue.quit() // 结束连接
181181
}
182182
}

yarn.lock

+37-12
Original file line numberDiff line numberDiff line change
@@ -1444,6 +1444,11 @@ cliui@^6.0.0:
14441444
strip-ansi "^6.0.0"
14451445
wrap-ansi "^6.2.0"
14461446

1447+
cluster-key-slot@^1.1.0:
1448+
version "1.1.0"
1449+
resolved "https://registry.npm.taobao.org/cluster-key-slot/download/cluster-key-slot-1.1.0.tgz#30474b2a981fb12172695833052bc0d01336d10d"
1450+
integrity sha1-MEdLKpgfsSFyaVgzBSvA0BM20Q0=
1451+
14471452
co-body@^6.0.0:
14481453
version "6.0.0"
14491454
resolved "https://registry.npm.taobao.org/co-body/download/co-body-6.0.0.tgz#965b9337d7f5655480787471f4237664820827e3"
@@ -1855,7 +1860,7 @@ delegates@^1.0.0:
18551860
resolved "https://registry.yarnpkg.com/delegates/-/delegates-1.0.0.tgz#84c6e159b81904fdca59a0ef44cd870d31250f9a"
18561861
integrity sha1-hMbhWbgZBP3KWaDvRM2HDTElD5o=
18571862

1858-
denque@^1.4.1:
1863+
denque@^1.1.0, denque@^1.4.1:
18591864
version "1.4.1"
18601865
resolved "https://registry.npm.taobao.org/denque/download/denque-1.4.1.tgz#6744ff7641c148c3f8a69c307e51235c1f4a37cf"
18611866
integrity sha1-Z0T/dkHBSMP4ppwwflEjXB9KN88=
@@ -3021,6 +3026,21 @@ invert-kv@^1.0.0:
30213026
resolved "https://registry.npm.taobao.org/invert-kv/download/invert-kv-1.0.0.tgz#104a8e4aaca6d3d8cd157a8ef8bfab2d7a3ffdb6"
30223027
integrity sha1-EEqOSqym09jNFXqO+L+rLXo//bY=
30233028

3029+
ioredis@^4.17.3:
3030+
version "4.17.3"
3031+
resolved "https://registry.npm.taobao.org/ioredis/download/ioredis-4.17.3.tgz?cache=0&sync_timestamp=1595989425959&other_urls=https%3A%2F%2Fregistry.npm.taobao.org%2Fioredis%2Fdownload%2Fioredis-4.17.3.tgz#9938c60e4ca685f75326337177bdc2e73ae9c9dc"
3032+
integrity sha1-mTjGDkymhfdTJjNxd73C5zrpydw=
3033+
dependencies:
3034+
cluster-key-slot "^1.1.0"
3035+
debug "^4.1.1"
3036+
denque "^1.1.0"
3037+
lodash.defaults "^4.2.0"
3038+
lodash.flatten "^4.4.0"
3039+
redis-commands "1.5.0"
3040+
redis-errors "^1.2.0"
3041+
redis-parser "^3.0.0"
3042+
standard-as-callback "^2.0.1"
3043+
30243044
ip-regex@^2.1.0:
30253045
version "2.1.0"
30263046
resolved "https://registry.npm.taobao.org/ip-regex/download/ip-regex-2.1.0.tgz#fa78bf5d2e6913c911ce9f819ee5146bb6d844e9"
@@ -4086,6 +4106,16 @@ lodash.curry@^4.1.1:
40864106
resolved "http://registry.npm.taobao.org/lodash.curry/download/lodash.curry-4.1.1.tgz#248e36072ede906501d75966200a86dab8b23170"
40874107
integrity sha1-JI42By7ekGUB11lmIAqG2riyMXA=
40884108

4109+
lodash.defaults@^4.2.0:
4110+
version "4.2.0"
4111+
resolved "https://registry.npm.taobao.org/lodash.defaults/download/lodash.defaults-4.2.0.tgz#d09178716ffea4dde9e5fb7b37f6f0802274580c"
4112+
integrity sha1-0JF4cW/+pN3p5ft7N/bwgCJ0WAw=
4113+
4114+
lodash.flatten@^4.4.0:
4115+
version "4.4.0"
4116+
resolved "https://registry.npm.taobao.org/lodash.flatten/download/lodash.flatten-4.4.0.tgz#f31c22225a9632d2bbf8e4addbef240aa765a61f"
4117+
integrity sha1-8xwiIlqWMtK7+OSt2+8kCqdlph8=
4118+
40894119
lodash.flattendeep@^4.4.0:
40904120
version "4.4.0"
40914121
resolved "https://registry.npm.taobao.org/lodash.flattendeep/download/lodash.flattendeep-4.4.0.tgz#fb030917f86a3134e5bc9bec0d69e0013ddfedb2"
@@ -5107,7 +5137,7 @@ redeyed@~2.1.0:
51075137
dependencies:
51085138
esprima "~4.0.0"
51095139

5110-
redis-commands@^1.5.0:
5140+
redis-commands@1.5.0:
51115141
version "1.5.0"
51125142
resolved "https://registry.yarnpkg.com/redis-commands/-/redis-commands-1.5.0.tgz#80d2e20698fe688f227127ff9e5164a7dd17e785"
51135143
integrity sha512-6KxamqpZ468MeQC3bkWmCB1fp56XL64D4Kf0zJSwDZbVLLm7KFkoIcHrgRvQ+sk8dnhySs7+yBg94yIkAK7aJg==
@@ -5124,16 +5154,6 @@ redis-parser@^3.0.0:
51245154
dependencies:
51255155
redis-errors "^1.0.0"
51265156

5127-
redis@^3.0.2:
5128-
version "3.0.2"
5129-
resolved "https://registry.yarnpkg.com/redis/-/redis-3.0.2.tgz#bd47067b8a4a3e6a2e556e57f71cc82c7360150a"
5130-
integrity sha512-PNhLCrjU6vKVuMOyFu7oSP296mwBkcE6lrAjruBYG5LgdSqtRBoVQIylrMyVZD/lkF24RSNNatzvYag6HRBHjQ==
5131-
dependencies:
5132-
denque "^1.4.1"
5133-
redis-commands "^1.5.0"
5134-
redis-errors "^1.2.0"
5135-
redis-parser "^3.0.0"
5136-
51375157
referrer-policy@1.2.0:
51385158
version "1.2.0"
51395159
resolved "https://registry.yarnpkg.com/referrer-policy/-/referrer-policy-1.2.0.tgz#b99cfb8b57090dc454895ef897a4cc35ef67a98e"
@@ -5713,6 +5733,11 @@ stack-utils@^2.0.2:
57135733
dependencies:
57145734
escape-string-regexp "^2.0.0"
57155735

5736+
standard-as-callback@^2.0.1:
5737+
version "2.0.1"
5738+
resolved "https://registry.npm.taobao.org/standard-as-callback/download/standard-as-callback-2.0.1.tgz#ed8bb25648e15831759b6023bdb87e6b60b38126"
5739+
integrity sha1-7YuyVkjhWDF1m2Ajvbh+a2CzgSY=
5740+
57165741
static-extend@^0.1.1:
57175742
version "0.1.2"
57185743
resolved "https://registry.npm.taobao.org/static-extend/download/static-extend-0.1.2.tgz#60809c39cbff55337226fd5e0b520f341f1fb5c6"

0 commit comments

Comments
 (0)