58 lines
1.8 KiB
Go
58 lines
1.8 KiB
Go
package dao
|
|
|
|
import (
|
|
"context"
|
|
|
|
"rag-local/common"
|
|
"rag-local/kb/consts"
|
|
|
|
"github.com/gogf/gf/v2/database/gdb"
|
|
"github.com/gogf/gf/v2/frame/g"
|
|
"github.com/gogf/gf/v2/os/gcache"
|
|
)
|
|
|
|
var SystemConfig = &systemConfigDao{}
|
|
|
|
type systemConfigDao struct{}
|
|
|
|
func init() {
|
|
ctx := context.Background()
|
|
_, err := g.DB(consts.DbGroupSystem).Exec(ctx, `CREATE TABLE IF NOT EXISTS `+consts.TableNameSystemConfig+` (
|
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
cfg_key TEXT NOT NULL DEFAULT '',
|
|
cfg_value TEXT NOT NULL DEFAULT '',
|
|
updated_at DATETIME DEFAULT (datetime('now','localtime'))
|
|
)`)
|
|
if err != nil {
|
|
g.Log().Warningf(ctx, "create system_config table failed: %v", err)
|
|
}
|
|
if _, err := g.DB(consts.DbGroupSystem).Exec(ctx, "CREATE UNIQUE INDEX IF NOT EXISTS idx_system_config_key ON "+consts.TableNameSystemConfig+"(cfg_key)"); err != nil {
|
|
g.Log().Warningf(ctx, "create index idx_system_config_key failed: %v", err)
|
|
}
|
|
}
|
|
|
|
func (d *systemConfigDao) Get(ctx context.Context, key string) (string, error) {
|
|
r, err := g.DB(consts.DbGroupSystem).Model(consts.TableNameSystemConfig).Ctx(ctx).
|
|
Cache(gdb.CacheOption{Duration: common.CacheTTL(), Name: "system_config_Get_" + key}).
|
|
Fields("cfg_value").Where("cfg_key", key).One()
|
|
if err != nil {
|
|
return "", err
|
|
}
|
|
if r == nil {
|
|
return "", nil
|
|
}
|
|
return r["cfg_value"].String(), nil
|
|
}
|
|
|
|
func (d *systemConfigDao) Set(ctx context.Context, key, value string) error {
|
|
_, err := g.DB(consts.DbGroupSystem).Exec(ctx,
|
|
"INSERT INTO "+consts.TableNameSystemConfig+" (cfg_key, cfg_value, updated_at) VALUES (?, ?, datetime('now','localtime')) "+
|
|
"ON CONFLICT(cfg_key) DO UPDATE SET cfg_value=excluded.cfg_value, updated_at=datetime('now','localtime')",
|
|
key, value)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
_, _ = gcache.Remove(ctx, "system_config_Get_"+key)
|
|
return nil
|
|
}
|