You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@answer.apache.org by li...@apache.org on 2026/01/29 03:15:44 UTC
(answer) branch main updated: feat: add GetConfigByKeyFromDB method to retrieve config directly
This is an automated email from the ASF dual-hosted git repository.
linkinstar pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/answer.git
The following commit(s) were added to refs/heads/main by this push:
new b522a168 feat: add GetConfigByKeyFromDB method to retrieve config directly
b522a168 is described below
commit b522a168e34ed954cf9f530c205d55daa4d4c7a7
Author: LinkinStars <li...@foxmail.com>
AuthorDate: Thu Jan 29 11:15:08 2026 +0800
feat: add GetConfigByKeyFromDB method to retrieve config directly
---
internal/repo/config/config_repo.go | 12 ++++++++++++
internal/service/config/config_service.go | 10 ++++++++++
internal/service/plugin_common/plugin_common_service.go | 2 +-
3 files changed, 23 insertions(+), 1 deletion(-)
diff --git a/internal/repo/config/config_repo.go b/internal/repo/config/config_repo.go
index 3708aa3e..bf0035cb 100644
--- a/internal/repo/config/config_repo.go
+++ b/internal/repo/config/config_repo.go
@@ -99,6 +99,18 @@ func (cr configRepo) GetConfigByKey(ctx context.Context, key string) (c *entity.
return c, nil
}
+func (cr configRepo) GetConfigByKeyFromDB(ctx context.Context, key string) (c *entity.Config, err error) {
+ c = &entity.Config{Key: key}
+ exist, err := cr.data.DB.Context(ctx).Get(c)
+ if err != nil {
+ return nil, errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()
+ }
+ if !exist {
+ return nil, fmt.Errorf("config not found by key: %s", key)
+ }
+ return c, nil
+}
+
func (cr configRepo) UpdateConfig(ctx context.Context, key string, value string) (err error) {
// check if key exists
oldConfig := &entity.Config{Key: key}
diff --git a/internal/service/config/config_service.go b/internal/service/config/config_service.go
index 63aea221..0848d604 100644
--- a/internal/service/config/config_service.go
+++ b/internal/service/config/config_service.go
@@ -31,6 +31,7 @@ import (
type ConfigRepo interface {
GetConfigByID(ctx context.Context, id int) (c *entity.Config, err error)
GetConfigByKey(ctx context.Context, key string) (c *entity.Config, err error)
+ GetConfigByKeyFromDB(ctx context.Context, key string) (c *entity.Config, err error)
UpdateConfig(ctx context.Context, key, value string) (err error)
}
@@ -64,6 +65,15 @@ func (cs *ConfigService) GetStringValue(ctx context.Context, key string) (val st
return cf.Value, nil
}
+// GetStringValueFromDB gets config string value directly from DB, bypassing cache.
+func (cs *ConfigService) GetStringValueFromDB(ctx context.Context, key string) (val string, err error) {
+ cf, err := cs.configRepo.GetConfigByKeyFromDB(ctx, key)
+ if err != nil {
+ return "", err
+ }
+ return cf.Value, nil
+}
+
// GetArrayStringValue get config array string value
func (cs *ConfigService) GetArrayStringValue(ctx context.Context, key string) (val []string, err error) {
cf, err := cs.configRepo.GetConfigByKey(ctx, key)
diff --git a/internal/service/plugin_common/plugin_common_service.go b/internal/service/plugin_common/plugin_common_service.go
index 7d39a5aa..eb46b5ac 100644
--- a/internal/service/plugin_common/plugin_common_service.go
+++ b/internal/service/plugin_common/plugin_common_service.go
@@ -144,7 +144,7 @@ func (ps *PluginCommonService) initPluginData() {
})
// init plugin status
- pluginStatus, err := ps.configService.GetStringValue(context.TODO(), constant.PluginStatus)
+ pluginStatus, err := ps.configService.GetStringValueFromDB(context.TODO(), constant.PluginStatus)
if err != nil {
log.Error(err)
} else {