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

47 lines
1.2 KiB
Go
Raw Normal View History

package data
import (
"bsm/full/module/base/initial/internal/impl"
"bsm/full/module/base/initial/internal/models"
pb "bsm/full/module/base/initial/pb"
2026-09-22 21:15:34 +08:00
"git.apinb.com/bsm-sdk/core/errcode"
"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()
}
2026-09-22 21:15:34 +08:00
// 缓存键只包含实际参与查询的条件show_town 在现有模型下无对应列,不参与查询)
cacheKey := impl.RedisService.BuildKey("areas", key, val)
err := impl.RedisService.Get(cacheKey, &areas)
if err == nil {
return areas, nil
}
2026-09-22 21:15:34 +08:00
// 按模型真实列查询InitialAreas 没有 enabled/show_town/sort_order 列)
err = impl.DBService.Where(key, val).
Order("name ASC").
Find(&areas).Error
2026-09-22 21:15:34 +08:00
if err != nil {
// 查询失败不写缓存,避免空结果污染缓存
return nil, errcode.ErrDB
}
err = impl.RedisService.Set(cacheKey, areas, vars.DefaultTTL)
return areas, err
}