package main import ( "redisdog/model" ) type SysCfgCache struct { lock chan int data map[string]string } func NewSysCfgCache() *SysCfgCache { return &SysCfgCache{ lock: make(chan int, 1), data: make(map[string]string), } } func (s *SysCfgCache) Load() error { mdl := model.NewSysCfg(Db) values, err := mdl.GetAll() if err != nil { return err } s.lock <- 1 for _, item := range values { s.data[item.CfgKey] = item.CfgValue } <-s.lock return nil } func (s *SysCfgCache) Get(k string) (string, bool) { s.lock <- 1 v, ok := s.data[k] <-s.lock return v, ok } func (s *SysCfgCache) Set(k, v string) { s.lock <- 1 s.data[k] = v <-s.lock } func (s *SysCfgCache) GetMulti(keys []string) map[string]string { s.lock <- 1 cpy := make(map[string]string) for _, key := range keys { v, ok := s.data[key] if ok { cpy[key] = v } else { cpy[key] = "" } } <-s.lock return cpy } func (s *SysCfgCache) GetAll() map[string]string { s.lock <- 1 cpy := make(map[string]string) for k, v := range s.data { cpy[k] = v } <-s.lock return cpy }