Просмотр исходного кода

perf: 优化数据看板性能

CaIon 2 лет назад
Родитель
Сommit
1618a8c9fc
2 измененных файлов с 6 добавлено и 5 удалено
  1. 1 1
      model/log.go
  2. 5 4
      model/usedata.go

+ 1 - 1
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())
 	}
 }
 

+ 5 - 4
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() {