goProject/trunk/goutil/redisUtil/redisPool.go

120 lines
3.3 KiB
Go
Raw Permalink Normal View History

2025-01-06 16:01:02 +08:00
/*
redisUtil对Redis的连接池进行了一定程度的封装
将常用的方法进行了内部封装对于不常见的方法有两种处理方式
1向作者提出请求由作者添加到代码中
2调用GetConnection方法然后自己实现逻辑
在代码中统一将conn.Do的结果和redis.Int,redis.String等类型转换合并处理
redis的命令请参考https://redis.readthedocs.io/en/2.6/index.html
*/
package redisUtil
import (
"fmt"
"time"
"github.com/gomodule/redigo/redis"
)
// 自定义Redis连接池对象
type RedisPool struct {
name string
address string
pool *redis.Pool
}
// 获取自定义Redis连接池对象的名称
// 返回值:
// 自定义Redis连接池对象的名称
func (this *RedisPool) GetName() string {
return this.name
}
// 获取自定义Redis连接池对象的目标地址
// 返回值:
// 自定义Redis连接池对象的目标地址
func (this *RedisPool) GetAddress() string {
return this.address
}
// 从自定义连接池中获取连接在使用后需要调用Close方法
// 返回值:
// 连接对象
func (this *RedisPool) GetConnection() redis.Conn {
return this.pool.Get()
}
// 关闭自定义连接池
func (this *RedisPool) Close() {
this.pool.Close()
}
// 测试连接情况
// 返回值:
// 错误对象
func (this *RedisPool) TestConnection() error {
conn := this.GetConnection()
defer conn.Close()
_, err := conn.Do("PING")
return err
}
// 创建新的Redis连接池对象(obsolete建议使用NewRedisPool2)
// name:连接池对象名称
// connectionString:Redis服务器连接地址
// passwordRedis服务器连接密码
// databaseRedis服务器选择的数据库
// maxActiveRedis连接池允许的最大活跃连接数量
// maxIdleRedis连接池允许的最大空闲数量
// idleTimeout连接被回收前的空闲时间
// dialConnectTimeout连接Redis服务器超时时间
// 返回值:
// Redis连接池对象
func NewRedisPool(name, connectionString, password string, database, maxActive, maxIdle int, idleTimeout, dialConnectTimeout time.Duration) *RedisPool {
redisConfig := NewRedisConfig2(connectionString, password, database, maxActive, maxIdle, idleTimeout, dialConnectTimeout)
return NewRedisPool2(name, redisConfig)
}
// 创建新的Redis连接池对象
// name:连接池对象名称
// redisConfig:Redis配置对象
// 返回值:
// Redis连接池对象
func NewRedisPool2(name string, redisConfig *RedisConfig) *RedisPool {
poolObj := &redis.Pool{
MaxActive: redisConfig.MaxActive,
MaxIdle: redisConfig.MaxIdle,
IdleTimeout: redisConfig.IdleTimeout,
Dial: func() (redis.Conn, error) {
options := make([]redis.DialOption, 0, 4)
options = append(options, redis.DialConnectTimeout(redisConfig.DialConnectTimeout))
options = append(options, redis.DialDatabase(redisConfig.Database))
if redisConfig.Password != "" {
options = append(options, redis.DialPassword(redisConfig.Password))
}
conn, err := redis.Dial("tcp", redisConfig.ConnectionString, options...)
if err != nil {
return nil, fmt.Errorf("Dial failed, err:%s", err)
}
return conn, err
},
TestOnBorrow: func(conn redis.Conn, t time.Time) error {
if time.Since(t) < time.Minute {
return nil
}
_, err := conn.Do("PING")
return err
},
}
return &RedisPool{
name: name,
address: redisConfig.ConnectionString,
pool: poolObj,
}
}