Skip to content

Commit 4e9e2fd

Browse files
committed
refactor: AB and UpdateSentenceTask
1 parent 3e9ec9d commit 4e9e2fd

File tree

9 files changed

+336
-292
lines changed

9 files changed

+336
-292
lines changed

config.example.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ redis: # 配置 Redis
2424
port: 6379 # Redis 端口
2525
password: '' # Redis 密码
2626
database: 0 # Redis 数据库
27-
sentences_ab_switchter: # 本节是服务 AB 异步更新的配置,如果您不知道这个是什么意思,请保持默认
27+
sentences_ab_switcher: # 本节是服务 AB 异步更新的配置,如果您不知道这个是什么意思,请保持默认
2828
a: 1 # a 状态对应的 redis 数据库
2929
b: 2 # b 状态对应的 redis 数据库
3030
log_level: info # 本节已废弃,输出到日记文件的一律为 Error;输出到终端由 flag `-D` 控制。

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -68,12 +68,12 @@
6868
"@hitokoto/koa-respond": "3.0.3",
6969
"NeteaseCloudMusicApi": "3.40.0",
7070
"async": "3.2.0",
71-
"axios": "0.20.0",
7271
"bytes": "3.1.0",
7372
"chalk": "4.1.0",
7473
"commander": "6.1.0",
7574
"cron": "1.8.2",
7675
"fast-json-stringify": "2.2.6",
76+
"got": "^11.7.0",
7777
"humanize-number": "0.0.2",
7878
"ioredis": "4.17.3",
7979
"joi": "17.2.1",

src/cache.js

+9-8
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ class Cache {
1313
// Connect Redis
1414
if (!newConnection) {
1515
this.redis = new Redis(ConnectionConfig)
16-
this.redis.on('connect', () => nconf.set('connectionFailedAttemp', 0))
16+
this.redis.on('connect', () => nconf.set('connectionFailedAttempt', 0))
1717
this.redis.on('error', handleError.bind(this))
1818
return true
1919
}
@@ -27,13 +27,14 @@ class Cache {
2727
return this.redis ? true : this.connect()
2828
}
2929

30-
static command(commands, ...params) {
30+
static async command(commands, ...params) {
31+
this.connectOrSkip()
3132
params[0] = 'cache:' + params[0]
3233
return this.redis[commands](params)
3334
}
3435

35-
static set(key, v, time) {
36-
this.connectOrSkip(this.isABSwitcher)
36+
static async set(key, v, time) {
37+
await this.connectOrSkip()
3738
const value = typeof v === 'object' ? JSON.stringify(v) : v
3839
if (time) {
3940
return this.redis.set('cache:' + key, value, 'EX', time)
@@ -43,7 +44,7 @@ class Cache {
4344
}
4445

4546
static async get(key, toJson = true) {
46-
this.connectOrSkip(this.isABSwitcher)
47+
await this.connectOrSkip()
4748
const data = await this.redis.get('cache:' + key)
4849
if (toJson) {
4950
try {
@@ -65,13 +66,13 @@ class Cache {
6566
* @param {any[]} callerParams the callerFunc params
6667
* @param {boolean} toJSON
6768
*/
68-
static async remeber(key, time, ...params) {
69-
this.connectOrSkip(this.isABSwitcher)
69+
static async remember(key, time, ...params) {
70+
await this.connectOrSkip()
7071
if (params.length <= 0 || params.length > 3) {
7172
throw new Error('the length of params is wrong')
7273
}
7374
if (typeof params[0] !== 'function') {
74-
throw new Error('the remeber caller must be a function')
75+
throw new Error('the remember caller must be a function')
7576
}
7677
const caller = params[0]
7778
const callerParams = params[1] ?? []

src/extensions/sentencesABSwitcher.js

+11-5
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@
55
const Cache = require('../cache')
66
const nconf = require('nconf')
77
const Redis = require('ioredis')
8-
const databaseA = nconf.get('sentences_ab_switchter:a') || 1
9-
const databaseB = nconf.get('sentences_ab_switchter:b') || 2
8+
const databaseA = nconf.get('sentences_ab_switcher:a') || 1
9+
const databaseB = nconf.get('sentences_ab_switcher:b') || 2
10+
const chalk = require('chalk')
11+
const winston = require('winston')
1012

1113
const { ConnectionConfig, handleError } = require('../utils/cache')
1214
class SentencesABSwitcher extends Cache {
@@ -19,29 +21,33 @@ class SentencesABSwitcher extends Cache {
1921
}
2022
// Connect Redis
2123
const tmp = new Redis(config)
22-
tmp.on('connect', () => nconf.set('connectionFailedAttemp', 0))
24+
tmp.on('connect', () => nconf.set('connectionFailedAttempt', 0))
2325
tmp.on('error', handleError.bind(this))
2426
if (isDefault) {
25-
this.redis = tmp // set defalt slot
27+
this.redis = tmp // set default slot
2628
}
2729
this['redis' + target.toUpperCase()] = tmp
2830
}
2931

30-
static connectOrSkip(database = 'a') {
32+
static async connectOrSkip() {
3133
if (this.redis) {
3234
return true
3335
} else {
36+
const database = await Cache.get('hitokoto:ab') // 初始化时读取默认分区
3437
this.connect('a', database === 'a')
3538
this.connect('b', database !== 'a')
39+
this.db = database
3640
}
3741
}
3842

3943
static setDatabase(target) {
44+
winston.verbose('[AB] switching database: ' + chalk.blue(target))
4045
if (target === 'a') {
4146
this.redis = this.redisA
4247
} else {
4348
this.redis = this.redisB
4449
}
50+
this.db = target
4551
}
4652

4753
static getConnection(target) {

0 commit comments

Comments
 (0)