parent
e7f43e07c2
commit
ad44d7efec
@ -0,0 +1,35 @@
|
||||
package goredis
|
||||
|
||||
import (
|
||||
"context"
|
||||
"github.com/go-redis/redis/v8"
|
||||
"time"
|
||||
)
|
||||
|
||||
// App 实例
|
||||
type App struct {
|
||||
Rdb *redis.Client
|
||||
Addr string // 地址
|
||||
Password string // 密码
|
||||
DB int // 数据库
|
||||
PoolSize int // 连接池大小
|
||||
}
|
||||
|
||||
// InitClient 初始化连接 普通连接
|
||||
func (app *App) InitClient() (err error) {
|
||||
if app.PoolSize == 0 {
|
||||
app.PoolSize = 100
|
||||
}
|
||||
app.Rdb = redis.NewClient(&redis.Options{
|
||||
Addr: app.Addr, // 地址
|
||||
Password: app.Password, // 密码
|
||||
DB: app.DB, // 数据库
|
||||
PoolSize: app.PoolSize, // 连接池大小
|
||||
})
|
||||
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
||||
defer cancel()
|
||||
|
||||
_, err = app.Rdb.Ping(ctx).Result()
|
||||
return err
|
||||
}
|
@ -1,65 +0,0 @@
|
||||
package goredis
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"github.com/go-redis/redis/v8"
|
||||
"time"
|
||||
)
|
||||
|
||||
var (
|
||||
Rdb *redis.Client
|
||||
RdbC *redis.ClusterClient
|
||||
)
|
||||
|
||||
// InitRedis 初始化连接 普通连接
|
||||
func InitRedis(host string, port int, password string, db int) (err error) {
|
||||
dsn := fmt.Sprintf("%s:%v", host, port)
|
||||
fmt.Printf("【redis.普通】数据库配置 %s \n", dsn)
|
||||
Rdb = redis.NewClient(&redis.Options{
|
||||
Addr: dsn,
|
||||
Password: password, // no password set
|
||||
DB: db, // use default DB
|
||||
PoolSize: 100, // 连接池大小
|
||||
})
|
||||
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
||||
defer cancel()
|
||||
|
||||
_, err = Rdb.Ping(ctx).Result()
|
||||
return err
|
||||
}
|
||||
|
||||
// InitSentinelRedis 初始化连接 哨兵模式
|
||||
func InitSentinelRedis(adds []string, masterName string, password string, db int) (err error) {
|
||||
fmt.Printf("【redis.哨兵】数据库配置 %s \n", adds)
|
||||
Rdb = redis.NewFailoverClient(&redis.FailoverOptions{
|
||||
MasterName: masterName,
|
||||
SentinelAddrs: adds,
|
||||
Password: password, // no password set
|
||||
DB: db, // use default DB
|
||||
PoolSize: 100, // 连接池大小
|
||||
})
|
||||
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
||||
defer cancel()
|
||||
|
||||
_, err = Rdb.Ping(ctx).Result()
|
||||
return err
|
||||
}
|
||||
|
||||
// InitClusterRedis 初始化连接 集群
|
||||
func InitClusterRedis(adds []string, password string) (err error) {
|
||||
fmt.Printf("【redis.集群】数据库配置 %v \n", adds)
|
||||
RdbC = redis.NewClusterClient(&redis.ClusterOptions{
|
||||
Addrs: adds,
|
||||
Password: password, // no password set
|
||||
PoolSize: 100, // 连接池大小
|
||||
})
|
||||
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
||||
defer cancel()
|
||||
|
||||
_, err = RdbC.Ping(ctx).Result()
|
||||
return err
|
||||
}
|
Loading…
Reference in new issue