befly 1.2.8 → 1.2.10
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/config/env.js +1 -0
- package/package.json +2 -2
- package/plugins/redis.js +57 -84
package/config/env.js
CHANGED
|
@@ -33,6 +33,7 @@ export const Env = {
|
|
|
33
33
|
MYSQL_POOL_TIMEOUT: Number(process.env.MYSQL_POOL_TIMEOUT),
|
|
34
34
|
MYSQL_POOL_MAX: Number(process.env.MYSQL_POOL_MAX),
|
|
35
35
|
// Redis配置
|
|
36
|
+
REDIS_URL: process.env.REDIS_URL,
|
|
36
37
|
REDIS_ENABLE: Number(process.env.REDIS_ENABLE),
|
|
37
38
|
REDIS_HOST: process.env.REDIS_HOST,
|
|
38
39
|
REDIS_PORT: Number(process.env.REDIS_PORT),
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "befly",
|
|
3
|
-
"version": "1.2.
|
|
3
|
+
"version": "1.2.10",
|
|
4
4
|
"description": "Buma - 为 Bun 专属打造的 API 接口框架核心引擎",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"private": false,
|
|
@@ -49,5 +49,5 @@
|
|
|
49
49
|
"README.md",
|
|
50
50
|
"vitest.config.js"
|
|
51
51
|
],
|
|
52
|
-
"gitHead": "
|
|
52
|
+
"gitHead": "96191ef97c7d7ee34e4967345062ab850546e4f2"
|
|
53
53
|
}
|
package/plugins/redis.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { redis } from 'bun';
|
|
1
2
|
import { Env } from '../config/env.js';
|
|
2
3
|
import { Logger } from '../utils/logger.js';
|
|
3
4
|
|
|
@@ -6,100 +7,72 @@ export default {
|
|
|
6
7
|
async onInit(befly) {
|
|
7
8
|
try {
|
|
8
9
|
if (Env.REDIS_ENABLE === 1) {
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
password: Env.REDIS_PASSWORD || '',
|
|
12
|
-
database: Env.REDIS_DB || 0,
|
|
13
|
-
socket: {
|
|
14
|
-
host: Env.REDIS_HOST || '127.0.0.1',
|
|
15
|
-
port: Env.REDIS_PORT || 6379,
|
|
16
|
-
reconnectStrategy: (retries) => {
|
|
17
|
-
// 指数退避重连策略,最大延迟 2 秒
|
|
18
|
-
const jitter = Math.floor(Math.random() * 200);
|
|
19
|
-
const delay = Math.min(Math.pow(2, retries) * 50, 2000);
|
|
20
|
-
return delay + jitter;
|
|
21
|
-
}
|
|
22
|
-
}
|
|
23
|
-
};
|
|
24
|
-
const createClient = await import('@redis/client').then((m) => m.createClient);
|
|
25
|
-
const redis = createClient(config);
|
|
26
|
-
|
|
27
|
-
// 测试连接
|
|
28
|
-
try {
|
|
29
|
-
await redis.connect();
|
|
30
|
-
// 测试连接
|
|
31
|
-
const result = await redis.ping();
|
|
32
|
-
} catch (error) {
|
|
33
|
-
Logger.error({
|
|
34
|
-
msg: 'Redis 连接失败',
|
|
35
|
-
message: error.message,
|
|
36
|
-
stack: error.stack
|
|
37
|
-
});
|
|
38
|
-
process.exit();
|
|
10
|
+
if ((await redis.ping()) !== 'PONG') {
|
|
11
|
+
throw new Error('Redis 连接失败');
|
|
39
12
|
}
|
|
40
13
|
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
14
|
+
return {
|
|
15
|
+
// 添加对象存储辅助方法
|
|
16
|
+
setObject: async (key, obj, ttl = null) => {
|
|
17
|
+
try {
|
|
18
|
+
const data = JSON.stringify(obj);
|
|
19
|
+
if (ttl) {
|
|
20
|
+
return await redis.setEx(`${process.env.REDIS_KEY_PREFIX}:${key}`, ttl, data);
|
|
21
|
+
}
|
|
22
|
+
return await redis.set(`${process.env.REDIS_KEY_PREFIX}:${key}`, data);
|
|
23
|
+
} catch (error) {
|
|
24
|
+
Logger.error({
|
|
25
|
+
msg: 'Redis setObject 错误',
|
|
26
|
+
message: error.message,
|
|
27
|
+
stack: error.stack
|
|
28
|
+
});
|
|
47
29
|
}
|
|
48
|
-
|
|
49
|
-
} catch (error) {
|
|
50
|
-
Logger.error({
|
|
51
|
-
msg: 'Redis setObject 错误',
|
|
52
|
-
message: error.message,
|
|
53
|
-
stack: error.stack
|
|
54
|
-
});
|
|
55
|
-
}
|
|
56
|
-
};
|
|
30
|
+
},
|
|
57
31
|
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
32
|
+
getObject: async (key) => {
|
|
33
|
+
try {
|
|
34
|
+
const data = await redis.get(`${process.env.REDIS_KEY_PREFIX}:${key}`);
|
|
35
|
+
return data ? JSON.parse(data) : null;
|
|
36
|
+
} catch (error) {
|
|
37
|
+
Logger.error({
|
|
38
|
+
msg: 'Redis getObject 错误',
|
|
39
|
+
message: error.message,
|
|
40
|
+
stack: error.stack
|
|
41
|
+
});
|
|
42
|
+
return null;
|
|
43
|
+
}
|
|
44
|
+
},
|
|
71
45
|
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
46
|
+
delObject: async (key) => {
|
|
47
|
+
try {
|
|
48
|
+
await redis.del(`${process.env.REDIS_KEY_PREFIX}:${key}`);
|
|
49
|
+
} catch (error) {
|
|
50
|
+
Logger.error({
|
|
51
|
+
msg: 'Redis delObject 错误',
|
|
52
|
+
message: error.message,
|
|
53
|
+
stack: error.stack
|
|
54
|
+
});
|
|
55
|
+
}
|
|
56
|
+
},
|
|
83
57
|
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
58
|
+
// 添加时序ID生成函数
|
|
59
|
+
genTimeID: async () => {
|
|
60
|
+
const timestamp = Math.floor(Date.now() / 1000);
|
|
61
|
+
const key = `time_id_counter:${timestamp}`;
|
|
88
62
|
|
|
89
|
-
|
|
90
|
-
|
|
63
|
+
const counter = await redis.incr(key);
|
|
64
|
+
await redis.expire(key, 2);
|
|
91
65
|
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
66
|
+
// 前3位计数器 + 后3位随机数
|
|
67
|
+
const counterPrefix = (counter % 1000).toString().padStart(3, '0'); // 000-999
|
|
68
|
+
const randomSuffix = Math.floor(Math.random() * 1000)
|
|
69
|
+
.toString()
|
|
70
|
+
.padStart(3, '0'); // 000-999
|
|
71
|
+
const suffix = `${counterPrefix}${randomSuffix}`;
|
|
98
72
|
|
|
99
|
-
|
|
73
|
+
return Number(`${timestamp}${suffix}`);
|
|
74
|
+
}
|
|
100
75
|
};
|
|
101
|
-
|
|
102
|
-
return redis;
|
|
103
76
|
} else {
|
|
104
77
|
Logger.warn(`Redis 未启用,跳过初始化`);
|
|
105
78
|
return {};
|