From 1618a8c9fcfd41c87d6332f23649da5eb580c240 Mon Sep 17 00:00:00 2001 From: CaIon <1808837298@qq.com> Date: Tue, 9 Jan 2024 16:20:04 +0800 Subject: [PATCH] =?UTF-8?q?perf:=20=E4=BC=98=E5=8C=96=E6=95=B0=E6=8D=AE?= =?UTF-8?q?=E7=9C=8B=E6=9D=BF=E6=80=A7=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- model/log.go | 2 +- model/usedata.go | 9 +++++---- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/model/log.go b/model/log.go index 36418145..85de9d38 100644 --- a/model/log.go +++ b/model/log.go @@ -79,7 +79,7 @@ func RecordConsumeLog(ctx context.Context, userId int, channelId int, promptToke common.LogError(ctx, "failed to record log: "+err.Error()) } if common.DataExportEnabled { - LogQuotaData(userId, username, modelName, quota, common.GetTimestamp()) + go LogQuotaData(userId, username, modelName, quota, common.GetTimestamp()) } } diff --git a/model/usedata.go b/model/usedata.go index 136af55e..aca3a366 100644 --- a/model/usedata.go +++ b/model/usedata.go @@ -37,9 +37,7 @@ func UpdateQuotaData() { var CacheQuotaData = make(map[string]*QuotaData) var CacheQuotaDataLock = sync.Mutex{} -func LogQuotaDataCache(userId int, username string, modelName string, quota int, createdAt int64) { - // 只精确到小时 - createdAt = createdAt - (createdAt % 3600) +func logQuotaDataCache(userId int, username string, modelName string, quota int, createdAt int64) { key := fmt.Sprintf("%d-%s-%s-%d", userId, username, modelName, createdAt) quotaData, ok := CacheQuotaData[key] if ok { @@ -59,9 +57,12 @@ func LogQuotaDataCache(userId int, username string, modelName string, quota int, } func LogQuotaData(userId int, username string, modelName string, quota int, createdAt int64) { + // 只精确到小时 + createdAt = createdAt - (createdAt % 3600) + CacheQuotaDataLock.Lock() defer CacheQuotaDataLock.Unlock() - LogQuotaDataCache(userId, username, modelName, quota, createdAt) + logQuotaDataCache(userId, username, modelName, quota, createdAt) } func SaveQuotaDataCache() {