github.com/gogf/gf@v1.16.9/database/gredis/gredis_config.go (about)

     1  // Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
     2  //
     3  // This Source Code Form is subject to the terms of the MIT License.
     4  // If a copy of the MIT was not distributed with this file,
     5  // You can obtain one at https://github.com/gogf/gf.
     6  
     7  package gredis
     8  
     9  import (
    10  	"context"
    11  	"github.com/gogf/gf/errors/gcode"
    12  	"github.com/gogf/gf/errors/gerror"
    13  	"github.com/gogf/gf/internal/intlog"
    14  
    15  	"github.com/gogf/gf/container/gmap"
    16  	"github.com/gogf/gf/text/gregex"
    17  	"github.com/gogf/gf/text/gstr"
    18  	"github.com/gogf/gf/util/gconv"
    19  )
    20  
    21  const (
    22  	DefaultGroupName = "default" // Default configuration group name.
    23  	DefaultRedisPort = 6379      // Default redis port configuration if not passed.
    24  )
    25  
    26  var (
    27  	// Configuration groups.
    28  	configs = gmap.NewStrAnyMap(true)
    29  )
    30  
    31  // SetConfig sets the global configuration for specified group.
    32  // If <name> is not passed, it sets configuration for the default group name.
    33  func SetConfig(config *Config, name ...string) {
    34  	group := DefaultGroupName
    35  	if len(name) > 0 {
    36  		group = name[0]
    37  	}
    38  	configs.Set(group, config)
    39  	instances.Remove(group)
    40  
    41  	intlog.Printf(context.TODO(), `SetConfig for group "%s": %+v`, group, config)
    42  }
    43  
    44  // SetConfigByStr sets the global configuration for specified group with string.
    45  // If <name> is not passed, it sets configuration for the default group name.
    46  func SetConfigByStr(str string, name ...string) error {
    47  	group := DefaultGroupName
    48  	if len(name) > 0 {
    49  		group = name[0]
    50  	}
    51  	config, err := ConfigFromStr(str)
    52  	if err != nil {
    53  		return err
    54  	}
    55  	configs.Set(group, config)
    56  	instances.Remove(group)
    57  	return nil
    58  }
    59  
    60  // GetConfig returns the global configuration with specified group name.
    61  // If <name> is not passed, it returns configuration of the default group name.
    62  func GetConfig(name ...string) (config *Config, ok bool) {
    63  	group := DefaultGroupName
    64  	if len(name) > 0 {
    65  		group = name[0]
    66  	}
    67  	if v := configs.Get(group); v != nil {
    68  		return v.(*Config), true
    69  	}
    70  	return &Config{}, false
    71  }
    72  
    73  // RemoveConfig removes the global configuration with specified group.
    74  // If <name> is not passed, it removes configuration of the default group name.
    75  func RemoveConfig(name ...string) {
    76  	group := DefaultGroupName
    77  	if len(name) > 0 {
    78  		group = name[0]
    79  	}
    80  	configs.Remove(group)
    81  	instances.Remove(group)
    82  
    83  	intlog.Printf(context.TODO(), `RemoveConfig: %s`, group)
    84  }
    85  
    86  // ConfigFromStr parses and returns config from given str.
    87  // Eg: host:port[,db,pass?maxIdle=x&maxActive=x&idleTimeout=x&maxConnLifetime=x]
    88  func ConfigFromStr(str string) (config *Config, err error) {
    89  	array, _ := gregex.MatchString(`^([^:]+):*(\d*),{0,1}(\d*),{0,1}(.*)\?(.+)$`, str)
    90  	if len(array) == 6 {
    91  		parse, _ := gstr.Parse(array[5])
    92  		config = &Config{
    93  			Host: array[1],
    94  			Port: gconv.Int(array[2]),
    95  			Db:   gconv.Int(array[3]),
    96  			Pass: array[4],
    97  		}
    98  		if config.Port == 0 {
    99  			config.Port = DefaultRedisPort
   100  		}
   101  		if err = gconv.Struct(parse, config); err != nil {
   102  			return nil, err
   103  		}
   104  		return
   105  	}
   106  	array, _ = gregex.MatchString(`([^:]+):*(\d*),{0,1}(\d*),{0,1}(.*)`, str)
   107  	if len(array) == 5 {
   108  		config = &Config{
   109  			Host: array[1],
   110  			Port: gconv.Int(array[2]),
   111  			Db:   gconv.Int(array[3]),
   112  			Pass: array[4],
   113  		}
   114  		if config.Port == 0 {
   115  			config.Port = DefaultRedisPort
   116  		}
   117  	} else {
   118  		err = gerror.NewCodef(gcode.CodeInvalidConfiguration, `invalid redis configuration: "%s"`, str)
   119  	}
   120  	return
   121  }
   122  
   123  // ClearConfig removes all configurations and instances of redis.
   124  func ClearConfig() {
   125  	configs.Clear()
   126  	instances.Clear()
   127  }