mirror of
https://github.com/linux-do/new-api.git
synced 2025-09-18 16:36:37 +08:00
chore: cache username
This commit is contained in:
parent
64e9e9cc20
commit
f07b9f8ab2
@ -68,6 +68,24 @@ func CacheGetUserGroup(id int) (group string, err error) {
|
|||||||
return group, err
|
return group, err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func CacheGetUsername(id int) (username string, err error) {
|
||||||
|
if !common.RedisEnabled {
|
||||||
|
return GetUsernameById(id)
|
||||||
|
}
|
||||||
|
username, err = common.RedisGet(fmt.Sprintf("user_name:%d", id))
|
||||||
|
if err != nil {
|
||||||
|
username, err = GetUserGroup(id)
|
||||||
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
err = common.RedisSet(fmt.Sprintf("user_name:%d", id), username, time.Duration(UserId2GroupCacheSeconds)*time.Second)
|
||||||
|
if err != nil {
|
||||||
|
common.SysError("Redis set user group error: " + err.Error())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return username, err
|
||||||
|
}
|
||||||
|
|
||||||
func CacheGetUserQuota(id int) (quota int, err error) {
|
func CacheGetUserQuota(id int) (quota int, err error) {
|
||||||
if !common.RedisEnabled {
|
if !common.RedisEnabled {
|
||||||
return GetUserQuota(id)
|
return GetUserQuota(id)
|
||||||
|
@ -41,9 +41,10 @@ func RecordLog(userId int, logType int, content string) {
|
|||||||
if logType == LogTypeConsume && !common.LogConsumeEnabled {
|
if logType == LogTypeConsume && !common.LogConsumeEnabled {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
username, _ := CacheGetUsername(userId)
|
||||||
log := &Log{
|
log := &Log{
|
||||||
UserId: userId,
|
UserId: userId,
|
||||||
Username: GetUsernameById(userId),
|
Username: username,
|
||||||
CreatedAt: common.GetTimestamp(),
|
CreatedAt: common.GetTimestamp(),
|
||||||
Type: logType,
|
Type: logType,
|
||||||
Content: content,
|
Content: content,
|
||||||
@ -59,7 +60,7 @@ func RecordConsumeLog(ctx context.Context, userId int, channelId int, promptToke
|
|||||||
if !common.LogConsumeEnabled {
|
if !common.LogConsumeEnabled {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
username := GetUsernameById(userId)
|
username, _ := CacheGetUsername(userId)
|
||||||
log := &Log{
|
log := &Log{
|
||||||
UserId: userId,
|
UserId: userId,
|
||||||
Username: username,
|
Username: username,
|
||||||
|
@ -2,6 +2,7 @@ package model
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"gorm.io/gorm"
|
||||||
"one-api/common"
|
"one-api/common"
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
@ -78,9 +79,10 @@ func SaveQuotaDataCache() {
|
|||||||
DB.Table("quota_data").Where("user_id = ? and username = ? and model_name = ? and created_at = ?",
|
DB.Table("quota_data").Where("user_id = ? and username = ? and model_name = ? and created_at = ?",
|
||||||
quotaData.UserID, quotaData.Username, quotaData.ModelName, quotaData.CreatedAt).First(quotaDataDB)
|
quotaData.UserID, quotaData.Username, quotaData.ModelName, quotaData.CreatedAt).First(quotaDataDB)
|
||||||
if quotaDataDB.Id > 0 {
|
if quotaDataDB.Id > 0 {
|
||||||
quotaDataDB.Count += quotaData.Count
|
//quotaDataDB.Count += quotaData.Count
|
||||||
quotaDataDB.Quota += quotaData.Quota
|
//quotaDataDB.Quota += quotaData.Quota
|
||||||
DB.Table("quota_data").Save(quotaDataDB)
|
//DB.Table("quota_data").Save(quotaDataDB)
|
||||||
|
increaseQuotaData(quotaData.UserID, quotaData.Username, quotaData.ModelName, quotaData.Count, quotaData.Quota, quotaData.CreatedAt)
|
||||||
} else {
|
} else {
|
||||||
DB.Table("quota_data").Create(quotaData)
|
DB.Table("quota_data").Create(quotaData)
|
||||||
}
|
}
|
||||||
@ -89,6 +91,17 @@ func SaveQuotaDataCache() {
|
|||||||
common.SysLog(fmt.Sprintf("保存数据看板数据成功,共保存%d条数据", size))
|
common.SysLog(fmt.Sprintf("保存数据看板数据成功,共保存%d条数据", size))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func increaseQuotaData(userId int, username string, modelName string, count int, quota int, createdAt int64) {
|
||||||
|
err := DB.Table("quota_data").Where("user_id = ? and username = ? and model_name = ? and created_at = ?",
|
||||||
|
userId, username, modelName, createdAt).Updates(map[string]interface{}{
|
||||||
|
"count": gorm.Expr("count + ?", count),
|
||||||
|
"quota": gorm.Expr("quota + ?", quota),
|
||||||
|
}).Error
|
||||||
|
if err != nil {
|
||||||
|
common.SysLog(fmt.Sprintf("increaseQuotaData error: %s", err))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func GetQuotaDataByUsername(username string, startTime int64, endTime int64) (quotaData []*QuotaData, err error) {
|
func GetQuotaDataByUsername(username string, startTime int64, endTime int64) (quotaData []*QuotaData, err error) {
|
||||||
var quotaDatas []*QuotaData
|
var quotaDatas []*QuotaData
|
||||||
// 从quota_data表中查询数据
|
// 从quota_data表中查询数据
|
||||||
|
@ -452,7 +452,7 @@ func updateUserRequestCount(id int, count int) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func GetUsernameById(id int) (username string) {
|
func GetUsernameById(id int) (username string, err error) {
|
||||||
DB.Model(&User{}).Where("id = ?", id).Select("username").Find(&username)
|
err = DB.Model(&User{}).Where("id = ?", id).Select("username").Find(&username).Error
|
||||||
return username
|
return username, err
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user