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
8c2323d7
authored
Feb 25, 2025
by
1808837298@qq.com
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
feat: redis poolsize
parent
583678d9
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
34 additions
and
1 deletions
+34
-1
common/redis.go
+34
-1
No files found.
common/redis.go
View file @
8c2323d7
...
...
@@ -32,6 +32,7 @@ func InitRedisClient() (err error) {
if
err
!=
nil
{
FatalLog
(
"failed to parse Redis connection string: "
+
err
.
Error
())
}
opt
.
PoolSize
=
GetEnvOrDefault
(
"REDIS_POOL_SIZE"
,
10
)
RDB
=
redis
.
NewClient
(
opt
)
ctx
,
cancel
:=
context
.
WithTimeout
(
context
.
Background
(),
5
*
time
.
Second
)
...
...
@@ -41,6 +42,10 @@ func InitRedisClient() (err error) {
if
err
!=
nil
{
FatalLog
(
"Redis ping test failed: "
+
err
.
Error
())
}
if
DebugEnabled
{
SysLog
(
fmt
.
Sprintf
(
"Redis connected to %s"
,
opt
.
Addr
))
SysLog
(
fmt
.
Sprintf
(
"Redis database: %d"
,
opt
.
DB
))
}
return
err
}
...
...
@@ -53,13 +58,20 @@ func ParseRedisOption() *redis.Options {
}
func
RedisSet
(
key
string
,
value
string
,
expiration
time
.
Duration
)
error
{
if
DebugEnabled
{
SysLog
(
fmt
.
Sprintf
(
"Redis SET: key=%s, value=%s, expiration=%v"
,
key
,
value
,
expiration
))
}
ctx
:=
context
.
Background
()
return
RDB
.
Set
(
ctx
,
key
,
value
,
expiration
)
.
Err
()
}
func
RedisGet
(
key
string
)
(
string
,
error
)
{
if
DebugEnabled
{
SysLog
(
fmt
.
Sprintf
(
"Redis GET: key=%s"
,
key
))
}
ctx
:=
context
.
Background
()
return
RDB
.
Get
(
ctx
,
key
)
.
Result
()
val
,
err
:=
RDB
.
Get
(
ctx
,
key
)
.
Result
()
return
val
,
err
}
//func RedisExpire(key string, expiration time.Duration) error {
...
...
@@ -73,16 +85,25 @@ func RedisGet(key string) (string, error) {
//}
func
RedisDel
(
key
string
)
error
{
if
DebugEnabled
{
SysLog
(
fmt
.
Sprintf
(
"Redis DEL: key=%s"
,
key
))
}
ctx
:=
context
.
Background
()
return
RDB
.
Del
(
ctx
,
key
)
.
Err
()
}
func
RedisHDelObj
(
key
string
)
error
{
if
DebugEnabled
{
SysLog
(
fmt
.
Sprintf
(
"Redis HDEL: key=%s"
,
key
))
}
ctx
:=
context
.
Background
()
return
RDB
.
HDel
(
ctx
,
key
)
.
Err
()
}
func
RedisHSetObj
(
key
string
,
obj
interface
{},
expiration
time
.
Duration
)
error
{
if
DebugEnabled
{
SysLog
(
fmt
.
Sprintf
(
"Redis HSET: key=%s, obj=%+v, expiration=%v"
,
key
,
obj
,
expiration
))
}
ctx
:=
context
.
Background
()
data
:=
make
(
map
[
string
]
interface
{})
...
...
@@ -130,6 +151,9 @@ func RedisHSetObj(key string, obj interface{}, expiration time.Duration) error {
}
func
RedisHGetObj
(
key
string
,
obj
interface
{})
error
{
if
DebugEnabled
{
SysLog
(
fmt
.
Sprintf
(
"Redis HGETALL: key=%s"
,
key
))
}
ctx
:=
context
.
Background
()
result
,
err
:=
RDB
.
HGetAll
(
ctx
,
key
)
.
Result
()
...
...
@@ -208,6 +232,9 @@ func RedisHGetObj(key string, obj interface{}) error {
// RedisIncr Add this function to handle atomic increments
func
RedisIncr
(
key
string
,
delta
int64
)
error
{
if
DebugEnabled
{
SysLog
(
fmt
.
Sprintf
(
"Redis INCR: key=%s, delta=%d"
,
key
,
delta
))
}
// 检查键的剩余生存时间
ttlCmd
:=
RDB
.
TTL
(
context
.
Background
(),
key
)
ttl
,
err
:=
ttlCmd
.
Result
()
...
...
@@ -238,6 +265,9 @@ func RedisIncr(key string, delta int64) error {
}
func
RedisHIncrBy
(
key
,
field
string
,
delta
int64
)
error
{
if
DebugEnabled
{
SysLog
(
fmt
.
Sprintf
(
"Redis HINCRBY: key=%s, field=%s, delta=%d"
,
key
,
field
,
delta
))
}
ttlCmd
:=
RDB
.
TTL
(
context
.
Background
(),
key
)
ttl
,
err
:=
ttlCmd
.
Result
()
if
err
!=
nil
&&
!
errors
.
Is
(
err
,
redis
.
Nil
)
{
...
...
@@ -262,6 +292,9 @@ func RedisHIncrBy(key, field string, delta int64) error {
}
func
RedisHSetField
(
key
,
field
string
,
value
interface
{})
error
{
if
DebugEnabled
{
SysLog
(
fmt
.
Sprintf
(
"Redis HSET field: key=%s, field=%s, value=%v"
,
key
,
field
,
value
))
}
ttlCmd
:=
RDB
.
TTL
(
context
.
Background
(),
key
)
ttl
,
err
:=
ttlCmd
.
Result
()
if
err
!=
nil
&&
!
errors
.
Is
(
err
,
redis
.
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