|
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950 |
- package db
-
- import (
- "code.fnuoos.com/go_rely_warehouse/zyos_go_pay.git/db/model"
- "code.fnuoos.com/go_rely_warehouse/zyos_go_pay.git/md"
- cache "code.fnuoos.com/go_rely_warehouse/zyos_go_tools.git/utils/cache"
- "fmt"
- "xorm.io/xorm"
- )
-
- // 获取一条记录
- func SysCfgGetOne(Db *xorm.Engine, key string) (*model.SysCfg, error) {
- var cfgList model.SysCfg
- if has, err := Db.Where("`key`=?", key).Get(&cfgList); err != nil || has == false {
- return nil, err
- }
- return &cfgList, nil
- }
-
- //单条记录获取DB
- func SysCfgGetWithDb(eg *xorm.Engine, masterId string, HKey string) string {
- cacheKey := fmt.Sprintf(md.AppCfgCacheKey, masterId, HKey[0:1])
- get, err := cache.HGetString(cacheKey, HKey)
- if err != nil || get == "" {
- cfg, err := SysCfgGetOne(eg, HKey)
- if err != nil || cfg == nil {
- return ""
- }
-
- // key是否存在
- cacheKeyExist := false
- if cache.Exists(cacheKey) {
- cacheKeyExist = true
- }
-
- // 设置缓存
- _, err = cache.HSet(cacheKey, HKey, cfg.Val)
- if err != nil {
- return ""
- }
- if !cacheKeyExist { // 如果是首次设置 设置过期时间
- _, err := cache.Expire(cacheKey, md.CfgCacheTime)
- if err != nil {
- return ""
- }
- }
- return cfg.Val
- }
- return get
- }
|