2021-01-17 21:46:25 +08:00
|
|
|
// Copyright GoFrame Author(https://goframe.org). 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 (
|
2021-10-11 21:41:56 +08:00
|
|
|
"github.com/gogf/gf/v2/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 (
|
2021-09-19 10:01:09 +08:00
|
|
|
// localInstances is the instance map for common used components.
|
|
|
|
|
localInstances = gmap.NewStrAnyMap(true)
|
2019-11-28 23:19:37 +08:00
|
|
|
)
|
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{} {
|
2021-09-19 10:01:09 +08:00
|
|
|
return localInstances.Get(name)
|
2017-12-14 17:32:51 +08:00
|
|
|
}
|
|
|
|
|
|
2021-12-30 00:20:38 +08:00
|
|
|
// Set sets an instance object to the instance manager with given name.
|
2019-11-06 20:22:20 +08:00
|
|
|
func Set(name string, instance interface{}) {
|
2021-09-19 10:01:09 +08:00
|
|
|
localInstances.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{} {
|
2021-09-19 10:01:09 +08:00
|
|
|
return localInstances.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,
|
2021-09-16 20:57:59 +08:00
|
|
|
// or sets instance with returned value of callback function `f` if it does not exist
|
2019-11-06 20:22:20 +08:00
|
|
|
// and then returns this instance.
|
|
|
|
|
func GetOrSetFunc(name string, f func() interface{}) interface{} {
|
2021-09-19 10:01:09 +08:00
|
|
|
return localInstances.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,
|
2021-09-16 20:57:59 +08:00
|
|
|
// or sets instance with returned value of callback function `f` if it does not exist
|
2019-11-06 20:22:20 +08:00
|
|
|
// and then returns this instance.
|
|
|
|
|
//
|
2021-09-16 20:57:59 +08:00
|
|
|
// GetOrSetFuncLock differs with GetOrSetFunc function is that it executes function `f`
|
2019-11-06 20:22:20 +08:00
|
|
|
// with mutex.Lock of the hash map.
|
|
|
|
|
func GetOrSetFuncLock(name string, f func() interface{}) interface{} {
|
2021-09-19 10:01:09 +08:00
|
|
|
return localInstances.GetOrSetFuncLock(name, f)
|
2018-10-13 20:29:27 +08:00
|
|
|
}
|
|
|
|
|
|
2021-10-21 18:22:47 +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.
|
2019-11-06 20:22:20 +08:00
|
|
|
func SetIfNotExist(name string, instance interface{}) bool {
|
2021-09-19 10:01:09 +08:00
|
|
|
return localInstances.SetIfNotExist(name, instance)
|
2017-12-20 12:05:36 +08:00
|
|
|
}
|