country.go 2.33 KB
Newer Older
hujiebin's avatar
hujiebin committed
1 2 3 4
package res_c

import (
	"git.hilo.cn/hilo-common/domain"
hujiebin's avatar
hujiebin committed
5
	"github.com/bluele/gcache"
hujiebin's avatar
hujiebin committed
6 7 8 9
	"hilo-group/domain/model/res_m"
	"time"
)

hujiebin's avatar
hujiebin committed
10 11 12 13 14 15 16
// 统一改成lru
var countryIconLru = gcache.New(1000).LRU().Build()
var countryIconKey = "country:icon"

var countryAreaLru = gcache.New(1000).LRU().Build()
var countryAreaKey = "country:area"

hujiebin's avatar
hujiebin committed
17 18 19 20 21 22 23 24 25 26
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
		}
iamhujiebin's avatar
iamhujiebin committed
27
		SaveAllCountryIcon(model, m)
hujiebin's avatar
hujiebin committed
28 29 30 31 32
	}
	return m, nil
}

func GetAllCountryIcon(model *domain.Model) (map[string]string, error) {
hujiebin's avatar
hujiebin committed
33 34 35 36 37 38
	//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
hujiebin's avatar
hujiebin committed
39 40
}

hujiebin's avatar
hujiebin committed
41 42 43 44 45 46 47 48
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)
hujiebin's avatar
hujiebin committed
49
}
chenweijian's avatar
chenweijian committed
50 51 52 53 54 55 56 57 58 59 60

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
		}
iamhujiebin's avatar
iamhujiebin committed
61
		SaveAllCountryArea(model, m)
chenweijian's avatar
chenweijian committed
62 63 64 65 66
	}
	return m, nil
}

func GetAllCountryArea(model *domain.Model) (map[string]string, error) {
hujiebin's avatar
hujiebin committed
67 68 69 70 71 72
	//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
chenweijian's avatar
chenweijian committed
73 74
}

hujiebin's avatar
hujiebin committed
75 76 77 78 79 80 81 82
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)
chenweijian's avatar
chenweijian committed
83
}