From 75948b6ab1ee6fa147f7d224c6cfc112762cbe55 Mon Sep 17 00:00:00 2001 From: hujiebin Date: Wed, 30 Aug 2023 11:21:13 +0800 Subject: [PATCH] =?UTF-8?q?feat:=E5=9B=BD=E6=97=97=EF=BC=8C=E5=9B=BD?= =?UTF-8?q?=E5=AE=B6=E4=BD=8D=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- domain/cache/res_c/country.go | 56 ++++++++++++++++++++++------------- 1 file changed, 36 insertions(+), 20 deletions(-) diff --git a/domain/cache/res_c/country.go b/domain/cache/res_c/country.go index 3e5744d..98db54d 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) } -- 2.22.0