package res_c import ( "git.hilo.cn/hilo-common/domain" "github.com/bluele/gcache" "hilo-group/domain/model/res_m" "time" ) // 统一改成lru var countryIconLru = gcache.New(1000).LRU().Build() var countryIconKey = "country:icon" var countryAreaLru = gcache.New(1000).LRU().Build() var countryAreaKey = "country:area" func GetCountryIconMap(model *domain.Model) (map[string]string, error) { m, err := GetAllCountryIcon(model) if err != nil || len(m) <= 0 { m, err = res_m.GetAllCountries(model) if err != nil { return nil, err } if len(m) <= 0 { return map[string]string{}, nil } SaveAllCountryIcon(model, m) } return m, nil } func GetAllCountryIcon(model *domain.Model) (map[string]string, error) { //key := redis_key.GetCountryIconKey() if data, err := countryIconLru.Get(countryIconKey); err == nil { return data.(map[string]string), nil } //return model.Redis.HGetAll(context.Background(), key).Result() return map[string]string{}, nil } func SaveAllCountryIcon(model *domain.Model, icons map[string]string) { //key := redis_key.GetCountryIconKey() //ret, err := model.Redis.HSet(context.Background(), key, icons).Result() //if err == nil { // model.Redis.Expire(context.Background(), key, time.Minute*10) //} //return ret, err _ = countryIconLru.SetWithExpire(countryIconKey, icons, time.Minute*10) } func GetCountryAreaMap(model *domain.Model) (map[string]string, error) { m, err := GetAllCountryArea(model) if err != nil || len(m) <= 0 { m, err = res_m.GetAllCountriesArea(model) if err != nil { return nil, err } if len(m) <= 0 { return map[string]string{}, nil } SaveAllCountryArea(model, m) } return m, nil } func GetAllCountryArea(model *domain.Model) (map[string]string, error) { //key := redis_key.GetCountryAreaKey() //return model.Redis.HGetAll(context.Background(), key).Result() if data, err := countryAreaLru.Get(countryAreaKey); err == nil { return data.(map[string]string), nil } return map[string]string{}, nil } func SaveAllCountryArea(model *domain.Model, data map[string]string) { //key := redis_key.GetCountryAreaKey() //ret, err := model.Redis.HSet(context.Background(), key, data).Result() //if err == nil { // model.Redis.Expire(context.Background(), key, time.Minute*10) //} //return ret, err _ = countryAreaLru.SetWithExpire(countryAreaKey, data, time.Minute*10) }