gf/g/database/gredis/gredis.go

137 lines
3.8 KiB
Go
Raw Normal View History

2018-04-18 18:05:46 +08:00
// Copyright 2017 gf Author(https://gitee.com/johng/gf). All Rights Reserved.
//
// This Source Code Form is subject to the terms of the MIT License.
// If a copy of the MIT was not distributed with this file,
// You can obtain one at https://gitee.com/johng/gf.
2019-01-15 23:27:47 +08:00
// Package gredis provides client for redis server.
2019-01-16 13:35:16 +08:00
//
2018-04-18 18:05:46 +08:00
// Redis客户端.
2019-01-16 13:35:16 +08:00
// Redis中文手册文档请参考http://redisdoc.com/ , Redis官方命令请参考https://redis.io/commands
2018-04-18 18:05:46 +08:00
package gredis
import (
"time"
"gitee.com/johng/gf/third/github.com/gomodule/redigo/redis"
"gitee.com/johng/gf/g/container/gmap"
2018-06-05 21:18:41 +08:00
"fmt"
2018-04-18 18:05:46 +08:00
)
const (
gDEFAULT_POOL_MAX_IDLE = 1
gDEFAULT_POOL_MAX_ACTIVE = 10
gDEFAULT_POOL_IDLE_TIMEOUT = 180 * time.Second
gDEFAULT_POOL_MAX_LIFE_TIME = 60 * time.Second
)
// Redis客户端
type Redis struct {
pool *redis.Pool
2018-04-18 18:05:46 +08:00
}
2018-06-05 21:18:41 +08:00
// Redis服务端但节点连接配置信息
type Config struct {
Host string // IP/域名
Port int // 端口
Db int // db
Pass string // 密码
}
2018-04-18 18:05:46 +08:00
// Redis链接池统计信息
type PoolStats struct {
redis.PoolStats
}
// 连接池map
var pools = gmap.NewStringInterfaceMap()
2018-06-05 21:25:30 +08:00
// 创建redis操作对象.
2018-06-05 21:18:41 +08:00
func New(config Config) *Redis {
r := &Redis{}
poolKey := fmt.Sprintf("%s:%d,%d", config.Host, config.Port, config.Db)
if v := pools.Get(poolKey); v == nil {
pool := &redis.Pool {
MaxIdle : gDEFAULT_POOL_MAX_IDLE,
MaxActive : gDEFAULT_POOL_MAX_ACTIVE,
IdleTimeout : gDEFAULT_POOL_IDLE_TIMEOUT,
MaxConnLifetime : gDEFAULT_POOL_MAX_LIFE_TIME,
Dial : func() (redis.Conn, error) {
2018-06-05 21:18:41 +08:00
c, err := redis.Dial("tcp", fmt.Sprintf("%s:%d", config.Host, config.Port))
if err != nil {
return nil, err
}
2018-06-05 21:18:41 +08:00
if len(config.Pass) > 0 {
if _, err := c.Do("AUTH", config.Pass); err != nil {
return nil, err
}
2018-06-05 21:18:41 +08:00
}
2018-08-21 09:40:55 +08:00
if _, err := c.Do("SELECT", config.Db); err != nil {
return nil, err
}
return c, nil
},
2018-08-21 10:34:36 +08:00
// 用来测试连接是否可用
TestOnBorrow: func(c redis.Conn, t time.Time) error {
_, err := c.Do("PING")
return err
},
}
pools.Set(poolKey, pool)
r.pool = pool
} else {
r.pool = v.(*redis.Pool)
2018-04-18 18:05:46 +08:00
}
return r
}
// 关闭redis管理对象将会关闭底层的
2018-04-18 18:05:46 +08:00
func (r *Redis) Close() error {
return r.pool.Close()
2018-04-18 18:05:46 +08:00
}
// 获得一个原生的redis连接对象用于自定义连接操作
// 但是需要注意的是如果不再使用该连接对象时需要手动Close连接否则会造成连接数超限。
func (r *Redis) GetConn() redis.Conn {
return r.pool.Get()
}
2018-04-18 18:05:46 +08:00
// 设置属性 - MaxIdle
func (r *Redis) SetMaxIdle(value int) {
r.pool.MaxIdle = value
}
// 设置属性 - MaxActive
func (r *Redis) SetMaxActive(value int) {
r.pool.MaxActive = value
}
// 设置属性 - IdleTimeout
func (r *Redis) SetIdleTimeout(value time.Duration) {
r.pool.IdleTimeout = value
}
// 设置属性 - MaxConnLifetime
func (r *Redis) SetMaxConnLifetime(value time.Duration) {
r.pool.MaxConnLifetime = value
}
// 获取当前连接池统计信息
func (r *Redis) Stats() *PoolStats {
return &PoolStats{r.pool.Stats()}
}
2018-06-05 21:18:41 +08:00
// 执行同步命令 - Do
2018-04-18 18:05:46 +08:00
func (r *Redis) Do(command string, args ...interface{}) (interface{}, error) {
conn := r.pool.Get()
defer conn.Close()
return conn.Do(command, args...)
2018-04-18 18:05:46 +08:00
}
2018-06-05 21:18:41 +08:00
// 执行异步命令 - Send
2018-04-18 18:05:46 +08:00
func (r *Redis) Send(command string, args ...interface{}) error {
conn := r.pool.Get()
defer conn.Close()
return conn.Send(command, args...)
2018-04-18 18:05:46 +08:00
}