package config import ( "fmt" "github.com/beego/beego/v2/core/logs" "github.com/beego/beego/v2/server/web" ) const ( SPLIT = "." ) type Config interface { InitConfig() } type BaseConfig struct { prefix string } func (b *BaseConfig) GetString(key string) string { value, err := web.AppConfig.String(fmt.Sprintf("%s%s%s", b.prefix, SPLIT, key)) if err != nil { return "" } return value } func (b *BaseConfig) GetInt(key string) int { value, err := web.AppConfig.Int(fmt.Sprintf("%s%s%s", b.prefix, SPLIT, key)) if err != nil { return 0 } return value } type Instance func() Config var configs = make(map[string]Instance) func GetConfig(name string) (config Config) { instanceFunc, ok := configs[name] if !ok { logs.Error("配置信息不存在", name) return nil } config = instanceFunc() config.InitConfig() return config } func Register(name string, adapter Instance) { if adapter == nil { panic("缓存类型不存在") } if _, ok := configs[name]; ok { panic("当前缓存类型已注册:" + name) } configs[name] = adapter }