From 24bd03ded20fd68556cd6f832b4342cc2c2d6843 Mon Sep 17 00:00:00 2001 From: huangjiajun <582604932@qq.com> Date: Wed, 21 Aug 2024 14:32:22 +0800 Subject: [PATCH] cfg --- src/db_sys_cfg.go | 52 +++++++++----------------------------------- src/model/sys_cfg.go | 4 ++-- 2 files changed, 12 insertions(+), 44 deletions(-) diff --git a/src/db_sys_cfg.go b/src/db_sys_cfg.go index 2c54f70..f6c3099 100644 --- a/src/db_sys_cfg.go +++ b/src/db_sys_cfg.go @@ -1,19 +1,15 @@ package db import ( - "applet/app/md" - "applet/app/utils/cache" - "applet/app/utils/logx" "code.fnuoos.com/zhimeng/model.git/src/model" - "fmt" "xorm.io/xorm" ) // 系统配置get func SysCfgGetAll(Db *xorm.Engine) (*[]model.SysCfg, error) { var cfgList []model.SysCfg - if err := Db.Cols("key,val,memo").Find(&cfgList); err != nil { - return nil, logx.Error(err) + if err := Db.Cols("k,v,memo").Find(&cfgList); err != nil { + return nil, err } return &cfgList, nil } @@ -21,62 +17,34 @@ func SysCfgGetAll(Db *xorm.Engine) (*[]model.SysCfg, error) { // 获取一条记录 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, logx.Error(err) + if has, err := Db.Where("`k`=?", key).Get(&cfgList); err != nil || has == false { + return nil, err } return &cfgList, nil } // 返回最后插入id func SysCfgInsert(Db *xorm.Engine, key, val, memo string) bool { - cfg := model.SysCfg{Key: key, Val: val, Memo: memo} + cfg := model.SysCfg{K: key, V: val, Memo: memo} _, err := Db.InsertOne(&cfg) if err != nil { - logx.Error(err) return false } return true } func SysCfgUpdate(Db *xorm.Engine, key, val, memo string) bool { - cfg := model.SysCfg{Key: key, Val: val, Memo: memo} + cfg := model.SysCfg{K: key, V: val, Memo: memo} _, err := Db.Where("`key`=?", key).Cols("val,memo").Update(&cfg) if err != nil { - logx.Error(err) return false } return true } func SysCfgGetWithDb(eg *xorm.Engine, masterId string, HKey string) string { - cacheKey := fmt.Sprintf(md.AppCfgCacheKey, masterId) + HKey - get, err := cache.GetString(cacheKey) - if err != nil || get == "" { - cfg, err := SysCfgGetOne(eg, HKey) - if err != nil || cfg == nil { - _ = logx.Error(err) - return "" - } - - // key是否存在 - cacheKeyExist := false - if cache.Exists(cacheKey) { - cacheKeyExist = true - } - - // 设置缓存 - _, err = cache.SetEx(cacheKey, cfg.Val, 30) - if err != nil { - _ = logx.Error(err) - return "" - } - if !cacheKeyExist { // 如果是首次设置 设置过期时间 - _, err := cache.Expire(cacheKey, md.CfgCacheTime) - if err != nil { - _ = logx.Error(err) - return "" - } - } - return cfg.Val + cfg, err := SysCfgGetOne(eg, HKey) + if err != nil || cfg == nil { + return "" } - return get + return cfg.V } diff --git a/src/model/sys_cfg.go b/src/model/sys_cfg.go index 22d906b..5508c9a 100644 --- a/src/model/sys_cfg.go +++ b/src/model/sys_cfg.go @@ -1,7 +1,7 @@ package model type SysCfg struct { - Key string `json:"key" xorm:"not null pk comment('键') VARCHAR(127)"` - Val string `json:"val" xorm:"comment('值') TEXT"` + K string `json:"k" xorm:"not null pk comment('键') VARCHAR(127)"` + V string `json:"v" xorm:"comment('值') TEXT"` Memo string `json:"memo" xorm:"not null default '' comment('备注') VARCHAR(255)"` }