2021-01-17 21:46:25 +08:00
// Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
2019-04-02 14:37:46 +08:00
//
// 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://github.com/gogf/gf.
package gredis
2019-09-10 20:38:23 +08:00
import (
2021-06-26 16:23:54 +08:00
"context"
2021-09-26 23:22:32 +08:00
"crypto/tls"
2021-09-23 21:23:22 +08:00
"time"
2019-09-10 20:38:23 +08:00
2021-10-11 21:41:56 +08:00
"github.com/gogf/gf/v2/container/gmap"
2021-11-13 23:23:55 +08:00
"github.com/gogf/gf/v2/errors/gcode"
"github.com/gogf/gf/v2/errors/gerror"
"github.com/gogf/gf/v2/internal/intlog"
2021-10-11 21:41:56 +08:00
"github.com/gogf/gf/v2/util/gconv"
2019-09-10 20:38:23 +08:00
)
2019-04-02 14:37:46 +08:00
2021-09-23 21:23:22 +08:00
// Config is redis configuration.
type Config struct {
2021-11-27 01:10:00 +08:00
Address string ` json:"address" ` // It supports single and cluster redis server. Multiple addresses joined with char ','. Eg: 192.168.1.1:6379, 192.168.1.2:6379.
2021-09-26 23:22:32 +08:00
Db int ` json:"db" ` // Redis db.
2021-09-23 21:23:22 +08:00
Pass string ` json:"pass" ` // Password for AUTH.
2021-09-26 23:22:32 +08:00
MinIdle int ` json:"minIdle" ` // Minimum number of connections allowed to be idle (default is 0)
2021-09-23 21:23:22 +08:00
MaxIdle int ` json:"maxIdle" ` // Maximum number of connections allowed to be idle (default is 10)
MaxActive int ` json:"maxActive" ` // Maximum number of connections limit (default is 0 means no limit).
MaxConnLifetime time . Duration ` json:"maxConnLifetime" ` // Maximum lifetime of the connection (default is 30 seconds, not allowed to be set to 0)
2021-09-26 23:22:32 +08:00
IdleTimeout time . Duration ` json:"idleTimeout" ` // Maximum idle time for connection (default is 10 seconds, not allowed to be set to 0)
WaitTimeout time . Duration ` json:"waitTimeout" ` // Timed out duration waiting to get a connection from the connection pool.
DialTimeout time . Duration ` json:"dialTimeout" ` // Dial connection timeout for TCP.
2022-02-15 01:10:03 +08:00
ReadTimeout time . Duration ` json:"readTimeout" ` // Read timeout for TCP. DO NOT set it if not necessary.
2021-09-26 23:22:32 +08:00
WriteTimeout time . Duration ` json:"writeTimeout" ` // Write timeout for TCP.
MasterName string ` json:"masterName" ` // Used in Redis Sentinel mode.
TLS bool ` json:"tls" ` // Specifies whether TLS should be used when connecting to the server.
2021-09-23 21:23:22 +08:00
TLSSkipVerify bool ` json:"tlsSkipVerify" ` // Disables server name verification when connecting over TLS.
2021-09-26 23:22:32 +08:00
TLSConfig * tls . Config ` json:"-" ` // TLS Config to use. When set TLS will be negotiated.
2021-09-23 21:23:22 +08:00
}
2019-04-02 14:37:46 +08:00
const (
2020-12-14 19:34:02 +08:00
DefaultGroupName = "default" // Default configuration group name.
2019-04-02 14:37:46 +08:00
)
2019-06-19 09:06:52 +08:00
2019-04-02 14:37:46 +08:00
var (
2019-06-19 09:06:52 +08:00
// Configuration groups.
2021-09-23 19:12:02 +08:00
localConfigMap = gmap . NewStrAnyMap ( true )
2019-04-02 14:37:46 +08:00
)
// SetConfig sets the global configuration for specified group.
2021-09-16 20:57:59 +08:00
// If `name` is not passed, it sets configuration for the default group name.
2021-01-25 21:59:44 +08:00
func SetConfig ( config * Config , name ... string ) {
2020-12-14 19:34:02 +08:00
group := DefaultGroupName
2019-06-19 09:06:52 +08:00
if len ( name ) > 0 {
group = name [ 0 ]
}
2021-09-23 19:12:02 +08:00
localConfigMap . Set ( group , config )
2020-02-24 21:09:19 +08:00
2021-06-26 16:23:54 +08:00
intlog . Printf ( context . TODO ( ) , ` SetConfig for group "%s": %+v ` , group , config )
2019-04-02 14:37:46 +08:00
}
2021-09-26 23:22:32 +08:00
// SetConfigByMap sets the global configuration for specified group with map.
2021-09-16 20:57:59 +08:00
// If `name` is not passed, it sets configuration for the default group name.
2021-09-26 23:22:32 +08:00
func SetConfigByMap ( m map [ string ] interface { } , name ... string ) error {
2020-12-14 19:34:02 +08:00
group := DefaultGroupName
2019-09-10 20:38:23 +08:00
if len ( name ) > 0 {
group = name [ 0 ]
}
2021-09-26 23:22:32 +08:00
config , err := ConfigFromMap ( m )
2019-09-10 20:38:23 +08:00
if err != nil {
return err
}
2021-09-23 19:12:02 +08:00
localConfigMap . Set ( group , config )
2019-09-10 20:38:23 +08:00
return nil
}
2021-09-26 23:22:32 +08:00
// ConfigFromMap parses and returns config from given map.
func ConfigFromMap ( m map [ string ] interface { } ) ( config * Config , err error ) {
config = & Config { }
if err = gconv . Scan ( m , config ) ; err != nil {
2021-12-22 22:22:42 +08:00
err = gerror . NewCodef ( gcode . CodeInvalidConfiguration , ` invalid redis configuration: %#v ` , m )
2021-09-26 23:22:32 +08:00
}
2022-01-22 13:48:09 +08:00
if config . DialTimeout < time . Second {
2021-09-26 23:22:32 +08:00
config . DialTimeout = config . DialTimeout * time . Second
}
2022-01-22 13:48:09 +08:00
if config . WaitTimeout < time . Second {
2021-09-26 23:22:32 +08:00
config . WaitTimeout = config . WaitTimeout * time . Second
}
2022-01-22 13:48:09 +08:00
if config . WriteTimeout < time . Second {
2021-09-26 23:22:32 +08:00
config . WriteTimeout = config . WriteTimeout * time . Second
}
2022-01-22 13:48:09 +08:00
if config . ReadTimeout < time . Second {
2021-09-26 23:22:32 +08:00
config . ReadTimeout = config . ReadTimeout * time . Second
}
2022-01-22 13:48:09 +08:00
if config . IdleTimeout < time . Second {
2021-09-26 23:22:32 +08:00
config . IdleTimeout = config . IdleTimeout * time . Second
}
2022-01-22 13:48:09 +08:00
if config . MaxConnLifetime < time . Second {
2021-09-26 23:22:32 +08:00
config . MaxConnLifetime = config . MaxConnLifetime * time . Second
}
return
}
2019-04-03 23:39:31 +08:00
// GetConfig returns the global configuration with specified group name.
2021-09-16 20:57:59 +08:00
// If `name` is not passed, it returns configuration of the default group name.
2021-01-25 21:59:44 +08:00
func GetConfig ( name ... string ) ( config * Config , ok bool ) {
2020-12-14 19:34:02 +08:00
group := DefaultGroupName
2019-06-19 09:06:52 +08:00
if len ( name ) > 0 {
group = name [ 0 ]
}
2021-09-23 19:12:02 +08:00
if v := localConfigMap . Get ( group ) ; v != nil {
2021-01-25 21:59:44 +08:00
return v . ( * Config ) , true
2019-06-19 09:06:52 +08:00
}
2021-01-25 21:59:44 +08:00
return & Config { } , false
2019-04-02 14:37:46 +08:00
}
// RemoveConfig removes the global configuration with specified group.
2021-09-16 20:57:59 +08:00
// If `name` is not passed, it removes configuration of the default group name.
2019-06-19 09:06:52 +08:00
func RemoveConfig ( name ... string ) {
2020-12-14 19:34:02 +08:00
group := DefaultGroupName
2019-06-19 09:06:52 +08:00
if len ( name ) > 0 {
group = name [ 0 ]
}
2021-09-23 19:12:02 +08:00
localConfigMap . Remove ( group )
2020-02-24 21:09:19 +08:00
2021-06-26 16:23:54 +08:00
intlog . Printf ( context . TODO ( ) , ` RemoveConfig: %s ` , group )
2019-04-02 14:37:46 +08:00
}
2021-09-23 19:12:02 +08:00
// ClearConfig removes all configurations of redis.
2019-04-02 14:37:46 +08:00
func ClearConfig ( ) {
2021-09-23 19:12:02 +08:00
localConfigMap . Clear ( )
2019-04-02 14:37:46 +08:00
}