koishi-plugin-share-links-analysis 0.8.0 → 0.8.1
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/lib/index.js +40 -12
- package/package.json +1 -1
package/lib/index.js
CHANGED
|
@@ -146,6 +146,7 @@ function apply(ctx, config) {
|
|
|
146
146
|
if (config.debug) {
|
|
147
147
|
logger.level = 3; // Debug Level
|
|
148
148
|
}
|
|
149
|
+
const pendingChecks = new Map();
|
|
149
150
|
// 清理缓存函数
|
|
150
151
|
const cleanExpiredCache = async () => {
|
|
151
152
|
if (!config.enableCache || config.cacheExpiration <= 0)
|
|
@@ -296,9 +297,10 @@ function apply(ctx, config) {
|
|
|
296
297
|
if (config.waitTip_Switch) {
|
|
297
298
|
await session.send(config.waitTip_Switch);
|
|
298
299
|
}
|
|
299
|
-
// ===
|
|
300
|
+
// === 缓存与并发控制逻辑 ===
|
|
300
301
|
let result = null;
|
|
301
302
|
const cacheKey = `${link.platform}:${link.id}`;
|
|
303
|
+
// 1. 查持久化缓存 (DB)
|
|
302
304
|
if (config.enableCache) {
|
|
303
305
|
const cached = await ctx.database.get('sla_parse_cache', cacheKey);
|
|
304
306
|
// 检查是否存在且未过期
|
|
@@ -315,21 +317,47 @@ function apply(ctx, config) {
|
|
|
315
317
|
}
|
|
316
318
|
}
|
|
317
319
|
}
|
|
318
|
-
//
|
|
320
|
+
// 2. 查内存任务队列
|
|
319
321
|
if (!result) {
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
await
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
322
|
+
if (pendingChecks.has(cacheKey)) {
|
|
323
|
+
logger.debug(`检测到正在进行的解析任务,正在等待合并结果: ${cacheKey}`);
|
|
324
|
+
// 如果有相同的任务正在进行,直接等待它的结果
|
|
325
|
+
result = await pendingChecks.get(cacheKey) || null;
|
|
326
|
+
}
|
|
327
|
+
else {
|
|
328
|
+
// 如果没有,创建一个新的 Promise 任务
|
|
329
|
+
const task = (async () => {
|
|
330
|
+
try {
|
|
331
|
+
const res = await (0, core_1.processLink)(ctx, config, link, session);
|
|
332
|
+
// 解析成功且开启缓存,则写入 DB
|
|
333
|
+
if (res && config.enableCache) {
|
|
334
|
+
await ctx.database.upsert('sla_parse_cache', [{
|
|
335
|
+
key: cacheKey,
|
|
336
|
+
data: res,
|
|
337
|
+
created_at: Date.now()
|
|
338
|
+
}]);
|
|
339
|
+
}
|
|
340
|
+
return res;
|
|
341
|
+
}
|
|
342
|
+
catch (e) {
|
|
343
|
+
logger.warn(`解析任务出错: ${e}`);
|
|
344
|
+
return null;
|
|
345
|
+
}
|
|
346
|
+
})();
|
|
347
|
+
// 将任务存入 Map
|
|
348
|
+
pendingChecks.set(cacheKey, task);
|
|
349
|
+
try {
|
|
350
|
+
result = await task;
|
|
351
|
+
}
|
|
352
|
+
finally {
|
|
353
|
+
// 无论成功失败,任务结束后从 Map 中移除
|
|
354
|
+
pendingChecks.delete(cacheKey);
|
|
355
|
+
}
|
|
328
356
|
}
|
|
329
357
|
}
|
|
330
|
-
// ===
|
|
358
|
+
// === 逻辑结束 ===
|
|
331
359
|
if (result) {
|
|
332
|
-
lastProcessedUrls[channelId][link.url] = now;
|
|
360
|
+
lastProcessedUrls[channelId][link.url] = Date.now();
|
|
333
361
|
await sendResult(ctx, session, config, result, logger);
|
|
334
362
|
}
|
|
335
363
|
linkCount++;
|