diff --git a/domain/cache/res_c/country.go b/domain/cache/res_c/country.go index 3e5744da6c4c92fe621cdf65d0a331ddc5152390..98db54d7ad2b85c4c74282d4595c155106f246e1 100644 --- a/domain/cache/res_c/country.go +++ b/domain/cache/res_c/country.go @@ -1,13 +1,19 @@ package res_c import ( - "context" "git.hilo.cn/hilo-common/domain" - "hilo-group/_const/redis_key" + "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 { @@ -24,17 +30,22 @@ func GetCountryIconMap(model *domain.Model) (map[string]string, error) { } func GetAllCountryIcon(model *domain.Model) (map[string]string, error) { - key := redis_key.GetCountryIconKey() - return model.Redis.HGetAll(context.Background(), key).Result() + //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) (int64, error) { - 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 +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) { @@ -53,15 +64,20 @@ func GetCountryAreaMap(model *domain.Model) (map[string]string, error) { } func GetAllCountryArea(model *domain.Model) (map[string]string, error) { - key := redis_key.GetCountryAreaKey() - return model.Redis.HGetAll(context.Background(), key).Result() + //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) (int64, error) { - 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 +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) }