From ade509f26d9d118f2dc950d8a7a288f6984ecde6 Mon Sep 17 00:00:00 2001 From: linyuchen Date: Thu, 7 Mar 2024 09:00:22 +0800 Subject: [PATCH] =?UTF-8?q?perf:=20=E4=BC=98=E5=8C=96=E6=95=B0=E6=8D=AE?= =?UTF-8?q?=E5=BA=93=E7=BC=93=E5=AD=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/common/db.ts | 26 +++++++++++++++----------- 1 file changed, 15 insertions(+), 11 deletions(-) diff --git a/src/common/db.ts b/src/common/db.ts index b061a87..3dcb9f6 100644 --- a/src/common/db.ts +++ b/src/common/db.ts @@ -11,6 +11,7 @@ class DBUtil { private readonly DB_KEY_PREFIX_MSG_SEQ_ID = "msg_seq_id_"; private db: Level; private cache: Record = {} // : RawMessage + private currentShortId: number; /* * 数据库结构 @@ -110,9 +111,9 @@ class DBUtil { const seqIdKey = this.DB_KEY_PREFIX_MSG_SEQ_ID + msg.msgSeq; msg.msgShortId = shortMsgId; try { - await this.db.put(shortIdKey, msg.msgId); - await this.db.put(longIdKey, JSON.stringify(msg)); - await this.db.put(seqIdKey, msg.msgId); + this.db.put(shortIdKey, msg.msgId).then(); + this.db.put(longIdKey, JSON.stringify(msg)).then(); + this.db.put(seqIdKey, msg.msgId).then(); } catch (e) { log("addMsg db error", e.stack.toString()); @@ -137,16 +138,19 @@ class DBUtil { } private async genMsgShortId(): Promise { - let shortId = -2147483640 const key = "msg_current_short_id"; - try { - let id: string = await this.db.get(key); - shortId = parseInt(id); - } catch (e) { + if (this.currentShortId === undefined){ + try { + let id: string = await this.db.get(key); + this.currentShortId = parseInt(id); + } catch (e) { + this.currentShortId = -2147483640 + } } - shortId++; - await this.db.put(key, shortId.toString()); - return shortId; + + this.currentShortId++; + await this.db.put(key, this.currentShortId.toString()); + return this.currentShortId; } }