Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
phsl
/
new-api
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Snippets
Members
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit
5eaa5bf0
authored
Jan 11, 2024
by
CaIon
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
chore: cache username
parent
d72d8852
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
40 additions
and
8 deletions
+40
-8
model/cache.go
+18
-0
model/log.go
+3
-2
model/usedata.go
+16
-3
model/user.go
+3
-3
No files found.
model/cache.go
View file @
5eaa5bf0
...
...
@@ -68,6 +68,24 @@ func CacheGetUserGroup(id int) (group string, err error) {
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
)
{
if
!
common
.
RedisEnabled
{
return
GetUserQuota
(
id
)
...
...
model/log.go
View file @
5eaa5bf0
...
...
@@ -41,9 +41,10 @@ func RecordLog(userId int, logType int, content string) {
if
logType
==
LogTypeConsume
&&
!
common
.
LogConsumeEnabled
{
return
}
username
,
_
:=
CacheGetUsername
(
userId
)
log
:=
&
Log
{
UserId
:
userId
,
Username
:
GetUsernameById
(
userId
)
,
Username
:
username
,
CreatedAt
:
common
.
GetTimestamp
(),
Type
:
logType
,
Content
:
content
,
...
...
@@ -59,7 +60,7 @@ func RecordConsumeLog(ctx context.Context, userId int, channelId int, promptToke
if
!
common
.
LogConsumeEnabled
{
return
}
username
:=
GetUsernameById
(
userId
)
username
,
_
:=
CacheGetUsername
(
userId
)
log
:=
&
Log
{
UserId
:
userId
,
Username
:
username
,
...
...
model/usedata.go
View file @
5eaa5bf0
...
...
@@ -2,6 +2,7 @@ package model
import
(
"fmt"
"gorm.io/gorm"
"one-api/common"
"sync"
"time"
...
...
@@ -78,9 +79,10 @@ func SaveQuotaDataCache() {
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
)
if
quotaDataDB
.
Id
>
0
{
quotaDataDB
.
Count
+=
quotaData
.
Count
quotaDataDB
.
Quota
+=
quotaData
.
Quota
DB
.
Table
(
"quota_data"
)
.
Save
(
quotaDataDB
)
//quotaDataDB.Count += quotaData.Count
//quotaDataDB.Quota += quotaData.Quota
//DB.Table("quota_data").Save(quotaDataDB)
increaseQuotaData
(
quotaData
.
UserID
,
quotaData
.
Username
,
quotaData
.
ModelName
,
quotaData
.
Count
,
quotaData
.
Quota
,
quotaData
.
CreatedAt
)
}
else
{
DB
.
Table
(
"quota_data"
)
.
Create
(
quotaData
)
}
...
...
@@ -89,6 +91,17 @@ func SaveQuotaDataCache() {
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
)
{
var
quotaDatas
[]
*
QuotaData
// 从quota_data表中查询数据
...
...
model/user.go
View file @
5eaa5bf0
...
...
@@ -452,7 +452,7 @@ func updateUserRequestCount(id int, count int) {
}
}
func
GetUsernameById
(
id
int
)
(
username
string
)
{
DB
.
Model
(
&
User
{})
.
Where
(
"id = ?"
,
id
)
.
Select
(
"username"
)
.
Find
(
&
username
)
return
username
func
GetUsernameById
(
id
int
)
(
username
string
,
err
error
)
{
err
=
DB
.
Model
(
&
User
{})
.
Where
(
"id = ?"
,
id
)
.
Select
(
"username"
)
.
Find
(
&
username
)
.
Error
return
username
,
err
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment