befly 1.2.9 → 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/package.json +2 -2
- package/plugins/redis.js +56 -66
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
|
@@ -7,82 +7,72 @@ export default {
|
|
|
7
7
|
async onInit(befly) {
|
|
8
8
|
try {
|
|
9
9
|
if (Env.REDIS_ENABLE === 1) {
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
await redis.connect();
|
|
13
|
-
// 测试连接
|
|
14
|
-
const result = await redis.ping();
|
|
15
|
-
} catch (error) {
|
|
16
|
-
Logger.error({
|
|
17
|
-
msg: 'Redis 连接失败',
|
|
18
|
-
message: error.message,
|
|
19
|
-
stack: error.stack
|
|
20
|
-
});
|
|
21
|
-
process.exit();
|
|
10
|
+
if ((await redis.ping()) !== 'PONG') {
|
|
11
|
+
throw new Error('Redis 连接失败');
|
|
22
12
|
}
|
|
23
13
|
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
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
|
+
});
|
|
30
29
|
}
|
|
31
|
-
|
|
32
|
-
} catch (error) {
|
|
33
|
-
Logger.error({
|
|
34
|
-
msg: 'Redis setObject 错误',
|
|
35
|
-
message: error.message,
|
|
36
|
-
stack: error.stack
|
|
37
|
-
});
|
|
38
|
-
}
|
|
39
|
-
};
|
|
30
|
+
},
|
|
40
31
|
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
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
|
+
},
|
|
54
45
|
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
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
|
+
},
|
|
66
57
|
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
58
|
+
// 添加时序ID生成函数
|
|
59
|
+
genTimeID: async () => {
|
|
60
|
+
const timestamp = Math.floor(Date.now() / 1000);
|
|
61
|
+
const key = `time_id_counter:${timestamp}`;
|
|
71
62
|
|
|
72
|
-
|
|
73
|
-
|
|
63
|
+
const counter = await redis.incr(key);
|
|
64
|
+
await redis.expire(key, 2);
|
|
74
65
|
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
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}`;
|
|
81
72
|
|
|
82
|
-
|
|
73
|
+
return Number(`${timestamp}${suffix}`);
|
|
74
|
+
}
|
|
83
75
|
};
|
|
84
|
-
|
|
85
|
-
return redis;
|
|
86
76
|
} else {
|
|
87
77
|
Logger.warn(`Redis 未启用,跳过初始化`);
|
|
88
78
|
return {};
|