github.com/wangyougui/gf/v2@v2.6.5/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/wangyougui/gf. 6 7 package gredis 8 9 import ( 10 "context" 11 12 "github.com/wangyougui/gf/v2/container/gmap" 13 "github.com/wangyougui/gf/v2/internal/intlog" 14 ) 15 16 var ( 17 // localInstances for instance management of redis client. 18 localInstances = gmap.NewStrAnyMap(true) 19 ) 20 21 // Instance returns an instance of redis client with specified group. 22 // The `name` param is unnecessary, if `name` is not passed, 23 // it returns a redis instance with default configuration group. 24 func Instance(name ...string) *Redis { 25 group := DefaultGroupName 26 if len(name) > 0 && name[0] != "" { 27 group = name[0] 28 } 29 v := localInstances.GetOrSetFuncLock(group, func() interface{} { 30 if config, ok := GetConfig(group); ok { 31 r, err := New(config) 32 if err != nil { 33 intlog.Errorf(context.TODO(), `%+v`, err) 34 return nil 35 } 36 return r 37 } 38 return nil 39 }) 40 if v != nil { 41 return v.(*Redis) 42 } 43 return nil 44 }