Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
H
hilo-common
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
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
hujiebin
hilo-common
Commits
6320e420
Commit
6320e420
authored
Aug 22, 2023
by
hujiebin
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
feat:添加redis-cluster
parent
72fcf402
Changes
5
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
55 additions
and
15 deletions
+55
-15
ctx.go
domain/ctx.go
+2
-1
model.go
domain/model.go
+8
-6
service.go
domain/service.go
+8
-6
config.go
resource/config/config.go
+4
-2
redisCluster.go
resource/redisCli/redisCluster.go
+33
-0
No files found.
domain/ctx.go
View file @
6320e420
...
...
@@ -9,5 +9,6 @@ import (
type
CtxAndDb
struct
{
Db
*
gorm
.
DB
*
mycontext
.
MyContext
Redis
*
redis
.
Client
Redis
*
redis
.
Client
RedisCluster
*
redis
.
Client
}
domain/model.go
View file @
6320e420
...
...
@@ -21,9 +21,10 @@ func CreateModel(ctxAndDb *CtxAndDb) *Model {
func
CreateModelContext
(
myContext
*
mycontext
.
MyContext
)
*
Model
{
return
&
Model
{
CtxAndDb
:
&
CtxAndDb
{
Db
:
mysql
.
Db
,
MyContext
:
myContext
,
Redis
:
redisCli
.
GetRedis
(),
Db
:
mysql
.
Db
,
MyContext
:
myContext
,
Redis
:
redisCli
.
GetRedis
(),
RedisCluster
:
redisCli
.
GetClusterRedis
(),
},
}
}
...
...
@@ -31,9 +32,10 @@ func CreateModelContext(myContext *mycontext.MyContext) *Model {
func
CreateModelNil
()
*
Model
{
return
&
Model
{
CtxAndDb
:
&
CtxAndDb
{
Db
:
mysql
.
Db
,
MyContext
:
mycontext
.
CreateMyContext
(
nil
),
Redis
:
redisCli
.
GetRedis
(),
Db
:
mysql
.
Db
,
MyContext
:
mycontext
.
CreateMyContext
(
nil
),
Redis
:
redisCli
.
GetRedis
(),
RedisCluster
:
redisCli
.
GetClusterRedis
(),
},
}
}
...
...
domain/service.go
View file @
6320e420
...
...
@@ -23,15 +23,17 @@ func (service *Service) getMyContext() *mycontext.MyContext {
func
CreateService
(
myContext
*
mycontext
.
MyContext
)
*
Service
{
if
myContext
==
nil
{
return
&
Service
{
CtxAndDb
:
&
CtxAndDb
{
Db
:
mysql
.
Db
,
MyContext
:
mycontext
.
CreateMyContext
(
nil
),
Redis
:
redisCli
.
GetRedis
(),
Db
:
mysql
.
Db
,
MyContext
:
mycontext
.
CreateMyContext
(
nil
),
Redis
:
redisCli
.
GetRedis
(),
RedisCluster
:
redisCli
.
GetClusterRedis
(),
}}
}
else
{
return
&
Service
{
CtxAndDb
:
&
CtxAndDb
{
Db
:
mysql
.
Db
,
MyContext
:
myContext
,
Redis
:
redisCli
.
GetRedis
(),
Db
:
mysql
.
Db
,
MyContext
:
myContext
,
Redis
:
redisCli
.
GetRedis
(),
RedisCluster
:
redisCli
.
GetClusterRedis
(),
}}
}
}
...
...
resource/config/config.go
View file @
6320e420
...
...
@@ -26,8 +26,10 @@ type MysqlCodeConfig struct {
//redis配置
type
RedisConfig
struct
{
REDIS_HOST
string
REDIS_PASSWORD
string
REDIS_HOST
string
REDIS_PASSWORD
string
REDIS_CLUSTER_HOST
string
REDIS_CLUSTER_PASSWORD
string
}
//jwt
...
...
resource/redisCli/redisCluster.go
0 → 100644
View file @
6320e420
package
redisCli
import
(
"context"
"git.hilo.cn/hilo-common/mylogrus"
"git.hilo.cn/hilo-common/resource/config"
"github.com/go-redis/redis/v8"
)
var
RedisClusterClient
*
redis
.
Client
func
Init
()
{
RedisClusterClient
=
redis
.
NewClient
(
&
redis
.
Options
{
Addr
:
config
.
GetConfigRedis
()
.
REDIS_CLUSTER_HOST
,
Password
:
config
.
GetConfigRedis
()
.
REDIS_CLUSTER_PASSWORD
,
// no password set
DB
:
0
,
// use default DB
PoolSize
:
20
,
MinIdleConns
:
20
,
})
mylogrus
.
MyLog
.
Infoln
(
config
.
GetConfigRedis
()
.
REDIS_HOST
)
mylogrus
.
MyLog
.
Infoln
(
config
.
GetConfigRedis
()
.
REDIS_PASSWORD
)
pong
,
err
:=
RedisClient
.
Ping
(
context
.
Background
())
.
Result
()
if
err
!=
nil
{
mylogrus
.
MyLog
.
Warn
(
err
)
mylogrus
.
MyLog
.
Fatal
(
"redis cluster db0 connect fail"
)
}
else
{
mylogrus
.
MyLog
.
Info
(
"redis cluster db0 connection success - "
,
pong
)
}
}
func
GetClusterRedis
()
*
redis
.
Client
{
return
RedisClusterClient
}
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