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
a2873b6d
authored
Jan 25, 2024
by
CaIon
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
feat: token缓存逻辑更新(实验性)
parent
d238aaea
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
83 additions
and
18 deletions
+83
-18
common/redis.go
+5
-0
main.go
+3
-0
model/cache.go
+65
-18
model/token.go
+10
-0
No files found.
common/redis.go
View file @
a2873b6d
...
@@ -57,6 +57,11 @@ func RedisGet(key string) (string, error) {
...
@@ -57,6 +57,11 @@ func RedisGet(key string) (string, error) {
return
RDB
.
Get
(
ctx
,
key
)
.
Result
()
return
RDB
.
Get
(
ctx
,
key
)
.
Result
()
}
}
func
RedisGetEx
(
key
string
,
expiration
time
.
Duration
)
(
string
,
error
)
{
ctx
:=
context
.
Background
()
return
RDB
.
GetSet
(
ctx
,
key
,
expiration
)
.
Result
()
}
func
RedisDel
(
key
string
)
error
{
func
RedisDel
(
key
string
)
error
{
ctx
:=
context
.
Background
()
ctx
:=
context
.
Background
()
return
RDB
.
Del
(
ctx
,
key
)
.
Err
()
return
RDB
.
Del
(
ctx
,
key
)
.
Err
()
...
...
main.go
View file @
a2873b6d
...
@@ -63,6 +63,9 @@ func main() {
...
@@ -63,6 +63,9 @@ func main() {
common
.
SysError
(
fmt
.
Sprintf
(
"sync frequency: %d seconds"
,
common
.
SyncFrequency
))
common
.
SysError
(
fmt
.
Sprintf
(
"sync frequency: %d seconds"
,
common
.
SyncFrequency
))
model
.
InitChannelCache
()
model
.
InitChannelCache
()
}
}
if
common
.
RedisEnabled
{
go
model
.
SyncTokenCache
(
common
.
SyncFrequency
)
}
if
common
.
MemoryCacheEnabled
{
if
common
.
MemoryCacheEnabled
{
go
model
.
SyncOptions
(
common
.
SyncFrequency
)
go
model
.
SyncOptions
(
common
.
SyncFrequency
)
go
model
.
SyncChannelCache
(
common
.
SyncFrequency
)
go
model
.
SyncChannelCache
(
common
.
SyncFrequency
)
...
...
model/cache.go
View file @
a2873b6d
...
@@ -20,34 +20,81 @@ var (
...
@@ -20,34 +20,81 @@ var (
UserId2StatusCacheSeconds
=
common
.
SyncFrequency
UserId2StatusCacheSeconds
=
common
.
SyncFrequency
)
)
func
CacheGetTokenByKey
(
key
string
)
(
*
Token
,
error
)
{
// 仅用于定时同步缓存
keyCol
:=
"`key`"
var
token2UserId
=
make
(
map
[
string
]
int
)
if
common
.
UsingPostgreSQL
{
var
token2UserIdLock
sync
.
RWMutex
keyCol
=
`"key"`
func
cacheSetToken
(
token
*
Token
)
error
{
if
!
common
.
RedisEnabled
{
return
token
.
SelectUpdate
()
}
}
var
token
Token
jsonBytes
,
err
:=
json
.
Marshal
(
token
)
if
err
!=
nil
{
return
err
}
err
=
common
.
RedisSet
(
fmt
.
Sprintf
(
"token:%s"
,
token
.
Key
),
string
(
jsonBytes
),
time
.
Duration
(
TokenCacheSeconds
)
*
time
.
Second
)
if
err
!=
nil
{
common
.
SysError
(
fmt
.
Sprintf
(
"failed to set token %s to redis: %s"
,
token
.
Key
,
err
.
Error
()))
return
err
}
token2UserIdLock
.
Lock
()
defer
token2UserIdLock
.
Unlock
()
token2UserId
[
token
.
Key
]
=
token
.
UserId
return
nil
}
// CacheGetTokenByKey 从缓存中获取 token 并续期时间,如果缓存中不存在,则从数据库中获取
func
CacheGetTokenByKey
(
key
string
)
(
*
Token
,
error
)
{
if
!
common
.
RedisEnabled
{
if
!
common
.
RedisEnabled
{
err
:=
DB
.
Where
(
keyCol
+
" = ?"
,
key
)
.
First
(
&
token
)
.
Error
return
GetTokenByKey
(
key
)
return
&
token
,
err
}
}
tokenObjectString
,
err
:=
common
.
RedisGet
(
fmt
.
Sprintf
(
"token:%s"
,
key
))
var
token
*
Token
tokenObjectString
,
err
:=
common
.
RedisGetEx
(
fmt
.
Sprintf
(
"token:%s"
,
key
),
time
.
Duration
(
TokenCacheSeconds
)
*
time
.
Second
)
if
err
!=
nil
{
if
err
!=
nil
{
err
:=
DB
.
Where
(
keyCol
+
" = ?"
,
key
)
.
First
(
&
token
)
.
Error
// 如果缓存中不存在,则从数据库中获取
token
,
err
=
GetTokenByKey
(
key
)
if
err
!=
nil
{
if
err
!=
nil
{
return
nil
,
err
return
nil
,
err
}
}
jsonBytes
,
err
:=
json
.
Marshal
(
token
)
err
=
cacheSetToken
(
token
)
if
err
!=
nil
{
return
token
,
nil
return
nil
,
err
}
err
=
json
.
Unmarshal
([]
byte
(
tokenObjectString
),
&
token
)
return
token
,
err
}
func
SyncTokenCache
(
frequency
int
)
{
for
{
time
.
Sleep
(
time
.
Duration
(
frequency
)
*
time
.
Second
)
common
.
SysLog
(
"syncing tokens from database"
)
token2UserIdLock
.
Lock
()
// 从token2UserId中获取所有的key
var
copyToken2UserId
=
make
(
map
[
string
]
int
)
for
s
,
i
:=
range
token2UserId
{
copyToken2UserId
[
s
]
=
i
}
}
err
=
common
.
RedisSet
(
fmt
.
Sprintf
(
"token:%s"
,
key
),
string
(
jsonBytes
),
time
.
Duration
(
TokenCacheSeconds
)
*
time
.
Second
)
token2UserId
=
make
(
map
[
string
]
int
)
if
err
!=
nil
{
token2UserIdLock
.
Unlock
()
common
.
SysError
(
"Redis set token error: "
+
err
.
Error
())
for
key
:=
range
copyToken2UserId
{
token
,
err
:=
GetTokenByKey
(
key
)
if
err
!=
nil
{
// 如果数据库中不存在,则删除缓存
common
.
SysError
(
fmt
.
Sprintf
(
"failed to get token %s from database: %s"
,
key
,
err
.
Error
()))
//delete redis
err
:=
common
.
RedisDel
(
fmt
.
Sprintf
(
"token:%s"
,
key
))
if
err
!=
nil
{
common
.
SysError
(
fmt
.
Sprintf
(
"failed to delete token %s from redis: %s"
,
key
,
err
.
Error
()))
}
}
else
{
// 如果数据库中存在,则更新缓存
err
:=
cacheSetToken
(
token
)
if
err
!=
nil
{
common
.
SysError
(
fmt
.
Sprintf
(
"failed to update token %s to redis: %s"
,
key
,
err
.
Error
()))
}
}
}
}
return
&
token
,
nil
}
}
err
=
json
.
Unmarshal
([]
byte
(
tokenObjectString
),
&
token
)
return
&
token
,
err
}
}
func
CacheGetUserGroup
(
id
int
)
(
group
string
,
err
error
)
{
func
CacheGetUserGroup
(
id
int
)
(
group
string
,
err
error
)
{
...
...
model/token.go
View file @
a2873b6d
...
@@ -100,6 +100,16 @@ func GetTokenById(id int) (*Token, error) {
...
@@ -100,6 +100,16 @@ func GetTokenById(id int) (*Token, error) {
return
&
token
,
err
return
&
token
,
err
}
}
func
GetTokenByKey
(
key
string
)
(
*
Token
,
error
)
{
keyCol
:=
"`key`"
if
common
.
UsingPostgreSQL
{
keyCol
=
`"key"`
}
var
token
Token
err
:=
DB
.
Where
(
keyCol
+
" = ?"
,
key
)
.
First
(
&
token
)
.
Error
return
&
token
,
err
}
func
(
token
*
Token
)
Insert
()
error
{
func
(
token
*
Token
)
Insert
()
error
{
var
err
error
var
err
error
err
=
DB
.
Create
(
token
)
.
Error
err
=
DB
.
Create
(
token
)
.
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