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
1ae0a384
authored
Feb 19, 2025
by
1808837298@qq.com
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
refactor: Optimize user caching and token retrieval methods
parent
83e161a1
Show whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
21 additions
and
8 deletions
+21
-8
controller/pricing.go
+1
-1
controller/user.go
+1
-1
model/token_cache.go
+1
-1
model/user.go
+2
-2
model/user_cache.go
+14
-1
service/quota.go
+1
-1
service/user_notify.go
+1
-1
No files found.
controller/pricing.go
View file @
1ae0a384
...
...
@@ -17,7 +17,7 @@ func GetPricing(c *gin.Context) {
}
var
group
string
if
exists
{
user
,
err
:=
model
.
GetUser
ById
(
userId
.
(
int
),
false
)
user
,
err
:=
model
.
GetUser
Cache
(
userId
.
(
int
)
)
if
err
==
nil
{
group
=
user
.
Group
}
...
...
controller/user.go
View file @
1ae0a384
...
...
@@ -472,7 +472,7 @@ func GetUserModels(c *gin.Context) {
if
err
!=
nil
{
id
=
c
.
GetInt
(
"id"
)
}
user
,
err
:=
model
.
GetUser
ById
(
id
,
true
)
user
,
err
:=
model
.
GetUser
Cache
(
id
)
if
err
!=
nil
{
c
.
JSON
(
http
.
StatusOK
,
gin
.
H
{
"success"
:
false
,
...
...
model/token_cache.go
View file @
1ae0a384
...
...
@@ -52,7 +52,7 @@ func cacheSetTokenField(key string, field string, value string) error {
func
cacheGetTokenByKey
(
key
string
)
(
*
Token
,
error
)
{
hmacKey
:=
common
.
GenerateHMAC
(
key
)
if
!
common
.
RedisEnabled
{
return
nil
,
nil
return
nil
,
fmt
.
Errorf
(
"redis is not enabled"
)
}
var
token
Token
err
:=
common
.
RedisHGetObj
(
fmt
.
Sprintf
(
"token:%s"
,
hmacKey
),
&
token
)
...
...
model/user.go
View file @
1ae0a384
...
...
@@ -42,8 +42,8 @@ type User struct {
Setting
string
`json:"setting" gorm:"type:text;column:setting"`
}
func
(
user
*
User
)
ToBaseUser
()
UserBase
{
cache
:=
UserBase
{
func
(
user
*
User
)
ToBaseUser
()
*
UserBase
{
cache
:=
&
UserBase
{
Id
:
user
.
Id
,
Group
:
user
.
Group
,
Quota
:
user
.
Quota
,
...
...
model/user_cache.go
View file @
1ae0a384
...
...
@@ -79,7 +79,7 @@ func GetUserCache(userId int) (userCache *UserBase, err error) {
}()
// Try getting from Redis first
err
=
common
.
RedisHGetObj
(
getUserCacheKey
(
userId
),
&
userCache
)
userCache
,
err
=
cacheGetUserBase
(
userId
)
if
err
==
nil
{
return
userCache
,
nil
}
...
...
@@ -105,6 +105,19 @@ func GetUserCache(userId int) (userCache *UserBase, err error) {
return
userCache
,
nil
}
func
cacheGetUserBase
(
userId
int
)
(
*
UserBase
,
error
)
{
if
!
common
.
RedisEnabled
{
return
nil
,
fmt
.
Errorf
(
"redis is not enabled"
)
}
var
userCache
UserBase
// Try getting from Redis first
err
:=
common
.
RedisHGetObj
(
getUserCacheKey
(
userId
),
&
userCache
)
if
err
!=
nil
{
return
nil
,
err
}
return
&
userCache
,
nil
}
// Add atomic quota operations using hash fields
func
cacheIncrUserQuota
(
userId
int
,
delta
int64
)
error
{
if
!
common
.
RedisEnabled
{
...
...
service/quota.go
View file @
1ae0a384
...
...
@@ -252,7 +252,7 @@ func PreConsumeTokenQuota(relayInfo *relaycommon.RelayInfo, quota int) error {
//if relayInfo.TokenUnlimited {
// return nil
//}
token
,
err
:=
model
.
GetTokenBy
Id
(
relayInfo
.
TokenId
)
token
,
err
:=
model
.
GetTokenBy
Key
(
relayInfo
.
TokenKey
,
false
)
if
err
!=
nil
{
return
err
}
...
...
service/user_notify.go
View file @
1ae0a384
...
...
@@ -11,7 +11,7 @@ import (
func
NotifyRootUser
(
t
string
,
subject
string
,
content
string
)
{
user
:=
model
.
GetRootUser
()
.
ToBaseUser
()
_
=
NotifyUser
(
&
user
,
dto
.
NewNotify
(
t
,
subject
,
content
,
nil
))
_
=
NotifyUser
(
user
,
dto
.
NewNotify
(
t
,
subject
,
content
,
nil
))
}
func
NotifyUser
(
user
*
model
.
UserBase
,
data
dto
.
Notify
)
error
{
...
...
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