2019-02-02 16:18:25 +08:00
|
|
|
// Copyright 2017 gf Author(https://github.com/gogf/gf). All Rights Reserved.
|
2017-12-29 16:03:30 +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,
|
2019-02-02 16:18:25 +08:00
|
|
|
// You can obtain one at https://github.com/gogf/gf.
|
2017-12-29 16:03:30 +08:00
|
|
|
|
2019-11-28 23:19:37 +08:00
|
|
|
// Package gins provides instances and core components management.
|
2017-12-30 23:49:55 +08:00
|
|
|
package gins
|
2017-12-14 17:32:51 +08:00
|
|
|
|
2017-12-20 12:05:36 +08:00
|
|
|
import (
|
2019-07-29 21:01:19 +08:00
|
|
|
"github.com/gogf/gf/container/gmap"
|
2017-12-20 12:05:36 +08:00
|
|
|
)
|
2017-12-14 17:32:51 +08:00
|
|
|
|
2019-11-28 23:19:37 +08:00
|
|
|
var (
|
|
|
|
// instances is the instance map for common used components.
|
|
|
|
instances = gmap.NewStrAnyMap(true)
|
|
|
|
)
|
2017-12-14 17:32:51 +08:00
|
|
|
|
2019-11-06 20:22:20 +08:00
|
|
|
// Get returns the instance by given name.
|
|
|
|
func Get(name string) interface{} {
|
|
|
|
return instances.Get(name)
|
2017-12-14 17:32:51 +08:00
|
|
|
}
|
|
|
|
|
2019-11-06 20:22:20 +08:00
|
|
|
// Set sets a instance object to the instance manager with given name.
|
|
|
|
func Set(name string, instance interface{}) {
|
|
|
|
instances.Set(name, instance)
|
2018-10-13 20:29:27 +08:00
|
|
|
}
|
|
|
|
|
2019-11-06 20:22:20 +08:00
|
|
|
// GetOrSet returns the instance by name,
|
|
|
|
// or set instance to the instance manager if it does not exist and returns this instance.
|
|
|
|
func GetOrSet(name string, instance interface{}) interface{} {
|
|
|
|
return instances.GetOrSet(name, instance)
|
2018-10-13 20:29:27 +08:00
|
|
|
}
|
|
|
|
|
2019-11-06 20:22:20 +08:00
|
|
|
// GetOrSetFunc returns the instance by name,
|
|
|
|
// or sets instance with returned value of callback function <f> if it does not exist
|
|
|
|
// and then returns this instance.
|
|
|
|
func GetOrSetFunc(name string, f func() interface{}) interface{} {
|
|
|
|
return instances.GetOrSetFunc(name, f)
|
2018-10-13 20:29:27 +08:00
|
|
|
}
|
|
|
|
|
2019-11-06 20:22:20 +08:00
|
|
|
// GetOrSetFuncLock returns the instance by name,
|
|
|
|
// or sets instance with returned value of callback function <f> if it does not exist
|
|
|
|
// and then returns this instance.
|
|
|
|
//
|
|
|
|
// GetOrSetFuncLock differs with GetOrSetFunc function is that it executes function <f>
|
|
|
|
// with mutex.Lock of the hash map.
|
|
|
|
func GetOrSetFuncLock(name string, f func() interface{}) interface{} {
|
|
|
|
return instances.GetOrSetFuncLock(name, f)
|
2018-10-13 20:29:27 +08:00
|
|
|
}
|
|
|
|
|
2019-11-06 20:22:20 +08:00
|
|
|
// SetIfNotExist sets <instance> to the map if the <name> does not exist, then returns true.
|
|
|
|
// It returns false if <name> exists, and <instance> would be ignored.
|
|
|
|
func SetIfNotExist(name string, instance interface{}) bool {
|
|
|
|
return instances.SetIfNotExist(name, instance)
|
2017-12-20 12:05:36 +08:00
|
|
|
}
|