Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
R
rym-util
Project
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
zhengqiuyun
rym-util
Commits
f3e80479
Commit
f3e80479
authored
Oct 19, 2022
by
zhengqiuyun86
Browse files
Options
Browse Files
Download
Plain Diff
Merge remote-tracking branch 'origin/master'
parents
2ea05b30
49e72ff6
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
46 additions
and
28 deletions
+46
-28
RedisClient.go
redis/RedisClient.go
+46
-28
No files found.
redis/RedisClient.go
View file @
f3e80479
...
...
@@ -17,12 +17,12 @@ var redisClient *redis.Client
func
Init
()
{
redisClient
=
redis
.
NewClient
(
&
redis
.
Options
{
Addr
:
fmt
.
Sprintf
(
"%s:%d"
,
conf
.
Redis
.
RedisHost
,
conf
.
Redis
.
RedisPort
),
Password
:
conf
.
Redis
.
RedisPwd
,
DB
:
conf
.
Redis
.
RedisDb
,
// redis一共16个库,指定其中一个库即可
PoolSize
:
conf
.
Redis
.
PoolSize
,
//最大连接数 默认cpu数*10
MinIdleConns
:
conf
.
Redis
.
MinIdleConns
,
//最小空闲连接数
ReadTimeout
:
time
.
Duration
(
conf
.
Redis
.
ReadTimeout
)
*
time
.
Second
,
//取超时间 单位秒 默认值为3秒
Addr
:
fmt
.
Sprintf
(
"%s:%d"
,
conf
.
Redis
.
RedisHost
,
conf
.
Redis
.
RedisPort
),
Password
:
conf
.
Redis
.
RedisPwd
,
DB
:
conf
.
Redis
.
RedisDb
,
// redis一共16个库,指定其中一个库即可
PoolSize
:
conf
.
Redis
.
PoolSize
,
//最大连接数 默认cpu数*10
MinIdleConns
:
conf
.
Redis
.
MinIdleConns
,
//最小空闲连接数
ReadTimeout
:
time
.
Duration
(
conf
.
Redis
.
ReadTimeout
)
*
time
.
Second
,
//取超时间 单位秒 默认值为3秒
WriteTimeout
:
time
.
Duration
(
conf
.
Redis
.
WriteTimeout
)
*
time
.
Second
,
// 写入超时时间 单位秒 默认与读超时时间一致
})
_
,
err
:=
redisClient
.
Ping
()
.
Result
()
...
...
@@ -81,7 +81,7 @@ func Get(key string) []byte {
func
Set
(
k
,
v
string
)
error
{
e
:=
getDb
()
.
Set
(
k
,
v
,
0
)
.
Err
()
if
e
==
nil
{
if
e
==
nil
{
log
.
Debug
(
fmt
.
Sprintf
(
"设置缓存成功:%s %s"
,
k
,
v
))
}
else
{
log
.
Debug
(
fmt
.
Sprintf
(
"设置缓存失败:%s %s %s"
,
k
,
v
,
e
))
...
...
@@ -90,8 +90,8 @@ func Set(k, v string) error {
}
func
SetNx
(
k
,
v
string
,
timeoutSecond
int
)
error
{
e
:=
getDb
()
.
Set
(
k
,
v
,
time
.
Duration
(
timeoutSecond
)
*
time
.
Second
)
.
Err
()
if
e
==
nil
{
e
:=
getDb
()
.
Set
(
k
,
v
,
time
.
Duration
(
timeoutSecond
)
*
time
.
Second
)
.
Err
()
if
e
==
nil
{
log
.
Debug
(
fmt
.
Sprintf
(
"设置缓存成功:%s %s"
,
k
,
v
))
}
else
{
log
.
Debug
(
fmt
.
Sprintf
(
"设置缓存失败:%s %s %s"
,
k
,
v
,
e
))
...
...
@@ -99,7 +99,7 @@ func SetNx(k, v string, timeoutSecond int) error {
return
e
}
func
Del
(
ks
...
string
)
(
int64
,
error
)
{
func
Del
(
ks
...
string
)
(
int64
,
error
)
{
return
getDb
()
.
Del
(
ks
...
)
.
Result
()
}
...
...
@@ -133,8 +133,8 @@ end`
// Lock 抢锁。抢到锁返回true,否则false。只抢一次
// 所有参数必传,超时时间单位秒
func
Lock
(
k
,
uniqueValue
string
,
timeoutSecond
int
)
bool
{
r
,
_
:=
getDb
()
.
Eval
(
lockScript
,
[]
string
{
k
},
timeoutSecond
,
uniqueValue
)
.
String
()
if
r
==
"1"
{
r
,
_
:=
getDb
()
.
Eval
(
lockScript
,
[]
string
{
k
},
timeoutSecond
,
uniqueValue
)
.
String
()
if
r
==
"1"
{
return
true
}
return
false
...
...
@@ -143,19 +143,38 @@ func Lock(k, uniqueValue string, timeoutSecond int) bool {
// Unlock 解锁
// 所有参数必传
func
Unlock
(
k
,
uniqueValue
string
)
bool
{
r
,
_
:=
getDb
()
.
Eval
(
unlockScript
,
[]
string
{
k
},
uniqueValue
)
.
String
()
if
r
==
"1"
{
r
,
_
:=
getDb
()
.
Eval
(
unlockScript
,
[]
string
{
k
},
uniqueValue
)
.
String
()
if
r
==
"1"
{
return
true
}
return
false
}
// LockWait 抢锁,在规定的时间内直到抢锁成功或者超时失败。抢到锁返回true,否则false
// k 必须
// uniqueValue 必须
// timeoutSecond 锁自动释放时间,必须
// lockTimeoutSecond 抢锁超时时间,默认1分钟
func
LockWait
(
k
,
uniqueValue
string
,
timeoutSecond
int
,
lockTimeoutSecond
int
)
bool
{
if
lockTimeoutSecond
==
0
{
lockTimeoutSecond
=
60
}
loopCount
:=
lockTimeoutSecond
*
10
for
i
:=
0
;
i
<
loopCount
;
i
++
{
if
Lock
(
k
,
uniqueValue
,
timeoutSecond
)
{
return
true
}
time
.
Sleep
(
time
.
Duration
(
100
)
*
time
.
Millisecond
)
//100ms
}
return
false
}
// Hset 向一张hash表中放入数据,如果不存在将创建
func
Hset
(
k
,
i
,
v
string
,
timeoutSecond
int
)
error
{
e
:=
getDb
()
.
HSet
(
k
,
i
,
v
)
.
Err
()
if
e
==
nil
{
if
e
==
nil
{
log
.
Debug
(
fmt
.
Sprintf
(
"设置缓存成功:%s %s %s"
,
k
,
i
,
v
))
if
timeoutSecond
>
0
{
if
timeoutSecond
>
0
{
Expire
(
k
,
int64
(
timeoutSecond
))
}
}
else
{
...
...
@@ -165,28 +184,28 @@ func Hset(k, i, v string, timeoutSecond int) error {
}
// HGet 从hash中获取值
func
HGet
(
k
,
i
string
)
(
string
,
error
)
{
r
,
e
:=
getDb
()
.
HGet
(
k
,
i
)
.
Result
()
return
r
,
e
func
HGet
(
k
,
i
string
)
(
string
,
error
)
{
r
,
e
:=
getDb
()
.
HGet
(
k
,
i
)
.
Result
()
return
r
,
e
}
// HGetAll 从hash中获key所有的取值
func
HGetAll
(
k
string
)
(
map
[
string
]
string
,
error
)
{
func
HGetAll
(
k
string
)
(
map
[
string
]
string
,
error
)
{
return
getDb
()
.
HGetAll
(
k
)
.
Result
()
}
// Hdel 从hash中删除
func
Hdel
(
k
string
,
is
...
string
)
(
int64
,
error
)
{
r
,
e
:=
getDb
()
.
HDel
(
k
,
is
...
)
.
Result
()
return
r
,
e
func
Hdel
(
k
string
,
is
...
string
)
(
int64
,
error
)
{
r
,
e
:=
getDb
()
.
HDel
(
k
,
is
...
)
.
Result
()
return
r
,
e
}
// HMSet 向一张hash表中放入数据,如果不存在将创建
func
HMSet
(
k
string
,
fields
map
[
string
]
interface
{},
timeoutSecond
int
)
error
{
e
:=
getDb
()
.
HMSet
(
k
,
fields
)
.
Err
()
if
e
==
nil
{
if
e
==
nil
{
log
.
Debug
(
fmt
.
Sprintf
(
"设置缓存成功:%s %s"
,
k
,
fields
))
if
timeoutSecond
>
0
{
if
timeoutSecond
>
0
{
Expire
(
k
,
int64
(
timeoutSecond
))
}
}
else
{
...
...
@@ -195,6 +214,6 @@ func HMSet(k string, fields map[string]interface{}, timeoutSecond int) error {
return
e
}
func
HMget
(
k
string
,
is
...
string
)
([]
interface
{},
error
)
{
func
HMget
(
k
string
,
is
...
string
)
([]
interface
{},
error
)
{
return
getDb
()
.
HMGet
(
k
,
is
...
)
.
Result
()
}
\ No newline at end of file
}
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