You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

70 lines
1.7 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

package container
import (
"goskeleton/app/global/my_errors"
"goskeleton/app/global/variable"
"log"
"strings"
"sync"
)
// 定义一个全局键值对存储容器
var sMap sync.Map
// CreateContainersFactory 创建一个容器工厂
func CreateContainersFactory() *containers {
return &containers{}
}
// 定义一个容器结构体
type containers struct {
}
// Set 1.以键值对的形式将代码注册到容器
func (c *containers) Set(key string, value interface{}) (res bool) {
if _, exists := c.KeyIsExists(key); exists == false {
sMap.Store(key, value)
res = true
} else {
// 程序启动阶段zaplog 未初始化使用系统log打印启动时候发生的异常日志
if variable.ZapLog == nil {
log.Fatal(my_errors.ErrorsContainerKeyAlreadyExists + ",请解决键名重复问题,相关键:" + key)
} else {
// 程序启动初始化完成
variable.ZapLog.Warn(my_errors.ErrorsContainerKeyAlreadyExists + ", 相关键:" + key)
}
}
return
}
// Delete 2.删除
func (c *containers) Delete(key string) {
sMap.Delete(key)
}
// Get 3.传递键,从容器获取值
func (c *containers) Get(key string) interface{} {
if value, exists := c.KeyIsExists(key); exists {
return value
}
return nil
}
// KeyIsExists 4. 判断键是否被注册
func (c *containers) KeyIsExists(key string) (interface{}, bool) {
return sMap.Load(key)
}
// FuzzyDelete 按照键的前缀模糊删除容器中注册的内容
func (c *containers) FuzzyDelete(keyPre string) {
sMap.Range(func(key, value interface{}) bool {
if keyname, ok := key.(string); ok {
if strings.HasPrefix(keyname, keyPre) {
sMap.Delete(keyname)
}
}
return true
})
}