Files
full/module/base/initial/internal/logic/data/areas_cache.go
2026-09-22 21:15:34 +08:00

47 lines
1.2 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package data
import (
"bsm/full/module/base/initial/internal/impl"
"bsm/full/module/base/initial/internal/models"
pb "bsm/full/module/base/initial/pb"
"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()
}
// 缓存键只包含实际参与查询的条件show_town 在现有模型下无对应列,不参与查询)
cacheKey := impl.RedisService.BuildKey("areas", key, val)
err := impl.RedisService.Get(cacheKey, &areas)
if err == nil {
return areas, nil
}
// 按模型真实列查询InitialAreas 没有 enabled/show_town/sort_order 列)
err = impl.DBService.Where(key, val).
Order("name ASC").
Find(&areas).Error
if err != nil {
// 查询失败不写缓存,避免空结果污染缓存
return nil, errcode.ErrDB
}
err = impl.RedisService.Set(cacheKey, areas, vars.DefaultTTL)
return areas, err
}