package cacheApi import ( "encoding/json" "epur-pay/model" "epur-pay/pkg/logger" "epur-pay/pkg/rds" "epur-pay/pkg/utils" "strconv" "strings" "sync" ) type Config struct { Data sync.Map } type configData struct { Code string `json:"code"` //key Comment string `json:"comment"` //comment } func (p *Config) Refresh() *Config { data := []configData{} utils.Error(rds.DB.Raw(`select * from config where pid != 0`).Scan(&data).Error) for _, v := range data { p.Data.Store(v.Code, v.Comment) } return p } func (p *Config) RefreshRow(data *model.Config) { p.Data.Store(data.Code, data.Comment) } func (p *Config) RefreshDel(data *model.Config) { p.Data.Delete(data.Code) } func (p *Config) Get(key string) string { if row, ok := p.Data.Load(key); ok { return row.(string) } else { logger.AccessLogger.Warnln("缓存不存在了,注意!", key) return "" } } func (p *Config) GetByte(key string) []byte { if row, ok := p.Data.Load(key); ok { return []byte(row.(string)) } else { logger.AccessLogger.Warnln("缓存不存在了,注意!", key) return nil } } func (p *Config) GetJsonRaw(key string) (row json.RawMessage) { data := p.GetByte(key) if data != nil { _ = json.Unmarshal(data, &row) } return row } func (p *Config) GetMap(key string) map[string]interface{} { data := p.GetByte(key) row := make(map[string]interface{}) if data != nil { configJson := []model.ConfigJson{} _ = json.Unmarshal(data, &configJson) for _, v := range configJson { row[v.Key] = v.Value } } return row } func (p *Config) GetArrayString(key string) []string { row := []string{} data := p.Get(key) if len(data) > 0 { row = strings.Split(p.Get(key), ",") } return row } func (p *Config) GetInt64(key string) int64 { if row, ok := p.Data.Load(key); ok { r, _ := strconv.Atoi(row.(string)) return int64(r) } else { return 0 } }