github.com/gogf/gf@v1.16.9/database/gredis/gredis_instance.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 "github.com/gogf/gf/container/gmap"
    10  
    11  var (
    12  	// Instance map
    13  	instances = gmap.NewStrAnyMap(true)
    14  )
    15  
    16  // Instance returns an instance of redis client with specified group.
    17  // The <name> param is unnecessary, if <name> is not passed,
    18  // it returns a redis instance with default configuration group.
    19  func Instance(name ...string) *Redis {
    20  	group := DefaultGroupName
    21  	if len(name) > 0 && name[0] != "" {
    22  		group = name[0]
    23  	}
    24  	v := instances.GetOrSetFuncLock(group, func() interface{} {
    25  		if config, ok := GetConfig(group); ok {
    26  			r := New(config)
    27  			r.group = group
    28  			return r
    29  		}
    30  		return nil
    31  	})
    32  	if v != nil {
    33  		return v.(*Redis)
    34  	}
    35  	return nil
    36  }