You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
93 lines
2.1 KiB
93 lines
2.1 KiB
|
1 year ago
|
package nosqlData
|
||
|
|
|
||
|
|
import (
|
||
|
|
"appNewPlatform/appConstant"
|
||
|
|
"context"
|
||
|
|
"fmt"
|
||
|
|
"time"
|
||
|
|
|
||
|
|
"github.com/redis/go-redis/v9"
|
||
|
|
)
|
||
|
|
|
||
|
|
func (r *RedisConfitSetUp) OpenRedis() *redis.Client {
|
||
|
|
address := fmt.Sprintf("%v:%v", r.UrlPath, r.Port)
|
||
|
|
fmt.Printf("开启%v Redis库 %v\n", address, r.Name)
|
||
|
|
redisClient := redis.NewClient(&redis.Options{
|
||
|
|
Addr: address,
|
||
|
|
Password: r.PassWord,
|
||
|
|
DB: r.Name,
|
||
|
|
PoolSize: GetMaxLine(),
|
||
|
|
MinIdleConns: appConstant.Constant_Config.RedisPrefixStr.MinIdleConns,
|
||
|
|
DialTimeout: DialTimeout(),
|
||
|
|
})
|
||
|
|
pingLink, err := redisClient.Ping(context.Background()).Result()
|
||
|
|
if err != nil {
|
||
|
|
fmt.Printf("%v Redis链接失败!原因:%v\n", r.Name, err)
|
||
|
|
} else {
|
||
|
|
fmt.Printf("%v Redis链接成功!==%v===>%v\n", r.Name, r.UrlPath, pingLink)
|
||
|
|
}
|
||
|
|
return redisClient
|
||
|
|
}
|
||
|
|
|
||
|
|
/*
|
||
|
|
*
|
||
|
|
@ 作者: 秦东
|
||
|
|
@ 时间: 2024-10-03 11:33:39
|
||
|
|
@ 功能: 获取最大连接池数量
|
||
|
|
*/
|
||
|
|
func GetMaxLine() int {
|
||
|
|
|
||
|
|
if appConstant.Constant_Config.RedisPrefixStr.PoolSize == 0 {
|
||
|
|
return appConstant.MaxCpuNumber * 4
|
||
|
|
}
|
||
|
|
return appConstant.Constant_Config.RedisPrefixStr.PoolSize
|
||
|
|
}
|
||
|
|
|
||
|
|
/*
|
||
|
|
*
|
||
|
|
@ 作者: 秦东
|
||
|
|
@ 时间: 2024-10-03 13:06:22
|
||
|
|
@ 功能: 计算超时时间
|
||
|
|
*/
|
||
|
|
func DialTimeout() time.Duration {
|
||
|
|
return time.Duration(appConstant.Constant_Config.RedisPrefixStr.DialTimeout) * time.Second
|
||
|
|
}
|
||
|
|
|
||
|
|
/*
|
||
|
|
*
|
||
|
|
@ 作者: 秦东
|
||
|
|
@ 时间: 2023-12-13 10:12:38
|
||
|
|
@ 功能: 链接redis集群
|
||
|
|
@ 参数
|
||
|
|
|
||
|
|
#
|
||
|
|
|
||
|
|
@ 返回值
|
||
|
|
|
||
|
|
#
|
||
|
|
|
||
|
|
@ 方法原型
|
||
|
|
|
||
|
|
#
|
||
|
|
*/
|
||
|
|
func (r *RedisConfitSetUp) OpenRedisColony() *redis.ClusterClient {
|
||
|
|
fmt.Printf("开启%v Redis集群库 %v\n", r.UrlPathList, r.Name)
|
||
|
|
redisClient := redis.NewClusterClient(&redis.ClusterOptions{
|
||
|
|
Addrs: r.UrlPathList,
|
||
|
|
Password: r.PassWord,
|
||
|
|
RouteRandomly: true,
|
||
|
|
// DB: r.Name,
|
||
|
|
PoolSize: GetMaxLine(),
|
||
|
|
MinIdleConns: appConstant.Constant_Config.RedisPrefixStr.MinIdleConns,
|
||
|
|
DialTimeout: DialTimeout(),
|
||
|
|
})
|
||
|
|
|
||
|
|
pingLink, err := redisClient.Ping(context.Background()).Result()
|
||
|
|
if err != nil {
|
||
|
|
fmt.Printf("%v Redis集群链接失败!原因:%v\n", r.Name, err)
|
||
|
|
} else {
|
||
|
|
fmt.Printf("%v Redis集群链接成功!==%v===>%v\n", r.Name, r.UrlPathList, pingLink)
|
||
|
|
}
|
||
|
|
return redisClient
|
||
|
|
}
|