Files
full/apps/base/initial/internal/logic/data/areas_cache.go

40 lines
929 B
Go
Raw Normal View History

package data
import (
"git.apinb.com/bsm-apps/initial/internal/impl"
"git.apinb.com/bsm-apps/initial/internal/models"
pb "git.apinb.com/bsm-apps/initial/pb"
"git.apinb.com/bsm-sdk/core/vars"
)
func GetAreasByCache(in *pb.AreasRequest) ([]*models.InitialAreas, error) {
var areas []*models.InitialAreas
var (
key string
val any
)
if in.GetCountryCode() != "" {
key = "country_code = ?"
val = in.GetCountryCode()
} else if in.GetCountryId() != 0 {
key = "country_id = ?"
val = in.GetCountryId()
}
cacheKey := impl.RedisService.BuildKey("areas", key, val, in.ShowTown)
err := impl.RedisService.Get(cacheKey, &areas)
if err == nil {
return areas, nil
}
err = impl.DBService.Where("enabled = ?", true).Where(key, val).Where("show_town = ?", in.ShowTown).
Order("sort_order ASC, name ASC").
Find(&areas).Error
err = impl.RedisService.Set(cacheKey, areas, vars.DefaultTTL)
return areas, err
}