perf: 优化数据看板性能

This commit is contained in:
CaIon 2024-01-09 16:20:04 +08:00
parent 75b6327f4f
commit 1618a8c9fc
2 changed files with 6 additions and 5 deletions

View File

@ -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())
}
}

View File

@ -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() {