2024-10-14 13:49:16 +08:00

72 lines
1.7 KiB
Go
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package container
import (
"catface/app/global/my_errors"
"catface/app/global/variable"
"log"
"strings"
"sync"
)
// INFO 线程安全的 KV 对存储容器;
// 定义一个全局键值对存储容器
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
})
}