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
0ae99986
authored
Feb 20, 2024
by
1808837298@qq.com
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
fix: 修复redis缓存用户额度不过期的问题
parent
ffe9deb1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
31 additions
and
2 deletions
+31
-2
common/redis.go
+31
-2
No files found.
common/redis.go
View file @
0ae99986
...
...
@@ -73,6 +73,35 @@ func RedisDel(key string) error {
}
func
RedisDecrease
(
key
string
,
value
int64
)
error
{
ctx
:=
context
.
Background
()
return
RDB
.
DecrBy
(
ctx
,
key
,
value
)
.
Err
()
// 检查键的剩余生存时间
ttlCmd
:=
RDB
.
TTL
(
context
.
Background
(),
key
)
ttl
,
err
:=
ttlCmd
.
Result
()
if
err
!=
nil
{
// 失败则尝试直接减少
return
RDB
.
DecrBy
(
context
.
Background
(),
key
,
value
)
.
Err
()
}
// 如果剩余生存时间大于0,则进行减少操作
if
ttl
>
0
{
ctx
:=
context
.
Background
()
// 开始一个Redis事务
txn
:=
RDB
.
TxPipeline
()
// 减少余额
decrCmd
:=
txn
.
DecrBy
(
ctx
,
key
,
value
)
if
err
:=
decrCmd
.
Err
();
err
!=
nil
{
return
err
// 如果减少失败,则直接返回错误
}
// 重新设置过期时间,使用原来的过期时间
txn
.
Expire
(
ctx
,
key
,
ttl
)
// 执行事务
_
,
err
=
txn
.
Exec
(
ctx
)
return
err
}
else
{
_
=
RedisDel
(
key
)
}
return
nil
}
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