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
819056a8
authored
Jul 23, 2023
by
JustSong
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
chore: do not hardcode cache time (close #302)
parent
04b8b412
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
13 additions
and
10 deletions
+13
-10
common/constants.go
+2
-0
main.go
+1
-0
model/cache.go
+10
-10
No files found.
common/constants.go
View file @
819056a8
...
@@ -77,6 +77,8 @@ var IsMasterNode = os.Getenv("NODE_TYPE") != "slave"
...
@@ -77,6 +77,8 @@ var IsMasterNode = os.Getenv("NODE_TYPE") != "slave"
var
requestInterval
,
_
=
strconv
.
Atoi
(
os
.
Getenv
(
"POLLING_INTERVAL"
))
var
requestInterval
,
_
=
strconv
.
Atoi
(
os
.
Getenv
(
"POLLING_INTERVAL"
))
var
RequestInterval
=
time
.
Duration
(
requestInterval
)
*
time
.
Second
var
RequestInterval
=
time
.
Duration
(
requestInterval
)
*
time
.
Second
var
SyncFrequency
=
10
*
60
// unit is second, will be overwritten by SYNC_FREQUENCY
const
(
const
(
RoleGuestUser
=
0
RoleGuestUser
=
0
RoleCommonUser
=
1
RoleCommonUser
=
1
...
...
main.go
View file @
819056a8
...
@@ -54,6 +54,7 @@ func main() {
...
@@ -54,6 +54,7 @@ func main() {
if
err
!=
nil
{
if
err
!=
nil
{
common
.
FatalLog
(
"failed to parse SYNC_FREQUENCY: "
+
err
.
Error
())
common
.
FatalLog
(
"failed to parse SYNC_FREQUENCY: "
+
err
.
Error
())
}
}
common
.
SyncFrequency
=
frequency
go
model
.
SyncOptions
(
frequency
)
go
model
.
SyncOptions
(
frequency
)
if
common
.
RedisEnabled
{
if
common
.
RedisEnabled
{
go
model
.
SyncChannelCache
(
frequency
)
go
model
.
SyncChannelCache
(
frequency
)
...
...
model/cache.go
View file @
819056a8
...
@@ -12,11 +12,11 @@ import (
...
@@ -12,11 +12,11 @@ import (
"time"
"time"
)
)
const
(
var
(
TokenCacheSeconds
=
60
*
60
TokenCacheSeconds
=
common
.
SyncFrequency
UserId2GroupCacheSeconds
=
60
*
60
UserId2GroupCacheSeconds
=
common
.
SyncFrequency
UserId2QuotaCacheSeconds
=
10
*
60
UserId2QuotaCacheSeconds
=
common
.
SyncFrequency
UserId2StatusCacheSeconds
=
60
*
60
UserId2StatusCacheSeconds
=
common
.
SyncFrequency
)
)
func
CacheGetTokenByKey
(
key
string
)
(
*
Token
,
error
)
{
func
CacheGetTokenByKey
(
key
string
)
(
*
Token
,
error
)
{
...
@@ -35,7 +35,7 @@ func CacheGetTokenByKey(key string) (*Token, error) {
...
@@ -35,7 +35,7 @@ func CacheGetTokenByKey(key string) (*Token, error) {
if
err
!=
nil
{
if
err
!=
nil
{
return
nil
,
err
return
nil
,
err
}
}
err
=
common
.
RedisSet
(
fmt
.
Sprintf
(
"token:%s"
,
key
),
string
(
jsonBytes
),
TokenCacheSeconds
*
time
.
Second
)
err
=
common
.
RedisSet
(
fmt
.
Sprintf
(
"token:%s"
,
key
),
string
(
jsonBytes
),
time
.
Duration
(
TokenCacheSeconds
)
*
time
.
Second
)
if
err
!=
nil
{
if
err
!=
nil
{
common
.
SysError
(
"Redis set token error: "
+
err
.
Error
())
common
.
SysError
(
"Redis set token error: "
+
err
.
Error
())
}
}
...
@@ -55,7 +55,7 @@ func CacheGetUserGroup(id int) (group string, err error) {
...
@@ -55,7 +55,7 @@ func CacheGetUserGroup(id int) (group string, err error) {
if
err
!=
nil
{
if
err
!=
nil
{
return
""
,
err
return
""
,
err
}
}
err
=
common
.
RedisSet
(
fmt
.
Sprintf
(
"user_group:%d"
,
id
),
group
,
UserId2GroupCacheSeconds
*
time
.
Second
)
err
=
common
.
RedisSet
(
fmt
.
Sprintf
(
"user_group:%d"
,
id
),
group
,
time
.
Duration
(
UserId2GroupCacheSeconds
)
*
time
.
Second
)
if
err
!=
nil
{
if
err
!=
nil
{
common
.
SysError
(
"Redis set user group error: "
+
err
.
Error
())
common
.
SysError
(
"Redis set user group error: "
+
err
.
Error
())
}
}
...
@@ -73,7 +73,7 @@ func CacheGetUserQuota(id int) (quota int, err error) {
...
@@ -73,7 +73,7 @@ func CacheGetUserQuota(id int) (quota int, err error) {
if
err
!=
nil
{
if
err
!=
nil
{
return
0
,
err
return
0
,
err
}
}
err
=
common
.
RedisSet
(
fmt
.
Sprintf
(
"user_quota:%d"
,
id
),
fmt
.
Sprintf
(
"%d"
,
quota
),
UserId2QuotaCacheSeconds
*
time
.
Second
)
err
=
common
.
RedisSet
(
fmt
.
Sprintf
(
"user_quota:%d"
,
id
),
fmt
.
Sprintf
(
"%d"
,
quota
),
time
.
Duration
(
UserId2QuotaCacheSeconds
)
*
time
.
Second
)
if
err
!=
nil
{
if
err
!=
nil
{
common
.
SysError
(
"Redis set user quota error: "
+
err
.
Error
())
common
.
SysError
(
"Redis set user quota error: "
+
err
.
Error
())
}
}
...
@@ -91,7 +91,7 @@ func CacheUpdateUserQuota(id int) error {
...
@@ -91,7 +91,7 @@ func CacheUpdateUserQuota(id int) error {
if
err
!=
nil
{
if
err
!=
nil
{
return
err
return
err
}
}
err
=
common
.
RedisSet
(
fmt
.
Sprintf
(
"user_quota:%d"
,
id
),
fmt
.
Sprintf
(
"%d"
,
quota
),
UserId2QuotaCacheSeconds
*
time
.
Second
)
err
=
common
.
RedisSet
(
fmt
.
Sprintf
(
"user_quota:%d"
,
id
),
fmt
.
Sprintf
(
"%d"
,
quota
),
time
.
Duration
(
UserId2QuotaCacheSeconds
)
*
time
.
Second
)
return
err
return
err
}
}
...
@@ -106,7 +106,7 @@ func CacheIsUserEnabled(userId int) bool {
...
@@ -106,7 +106,7 @@ func CacheIsUserEnabled(userId int) bool {
status
=
common
.
UserStatusEnabled
status
=
common
.
UserStatusEnabled
}
}
enabled
=
fmt
.
Sprintf
(
"%d"
,
status
)
enabled
=
fmt
.
Sprintf
(
"%d"
,
status
)
err
=
common
.
RedisSet
(
fmt
.
Sprintf
(
"user_enabled:%d"
,
userId
),
enabled
,
UserId2StatusCacheSeconds
*
time
.
Second
)
err
=
common
.
RedisSet
(
fmt
.
Sprintf
(
"user_enabled:%d"
,
userId
),
enabled
,
time
.
Duration
(
UserId2StatusCacheSeconds
)
*
time
.
Second
)
if
err
!=
nil
{
if
err
!=
nil
{
common
.
SysError
(
"Redis set user enabled error: "
+
err
.
Error
())
common
.
SysError
(
"Redis set user enabled error: "
+
err
.
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