1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253 |
- package config
- import (
- "fmt"
- "github.com/beego/beego/v2/core/logs"
- "github.com/beego/beego/v2/server/web"
- )
- const (
- SPLIT = "."
- )
- type Config interface {
- GetConfig() interface{}
- InitConfig()
- }
- type BaseConfig struct {
- prefix string
- config map[string]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
- }
- type Instance func() Config
- var configs = make(map[string]Instance)
- func GetConfig(name string) (config Config) {
- instanceFunc, ok := configs[name]
- if !ok {
- logs.Error("cache: unknown adapter name %q (forgot to import?)", name)
- return nil
- }
- config = instanceFunc()
- config.InitConfig()
- return config
- }
- func Register(name string, adapter Instance) {
- if adapter == nil {
- panic("cache: Register adapter is nil")
- }
- if _, ok := configs[name]; ok {
- panic("cache: Register called twice for adapter " + name)
- }
- configs[name] = adapter
- }
|