Skip to content

Commit b522a16

Browse files
committed
feat: add GetConfigByKeyFromDB method to retrieve config directly
1 parent e5cb38b commit b522a16

3 files changed

Lines changed: 23 additions & 1 deletion

File tree

internal/repo/config/config_repo.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,18 @@ func (cr configRepo) GetConfigByKey(ctx context.Context, key string) (c *entity.
9999
return c, nil
100100
}
101101

102+
func (cr configRepo) GetConfigByKeyFromDB(ctx context.Context, key string) (c *entity.Config, err error) {
103+
c = &entity.Config{Key: key}
104+
exist, err := cr.data.DB.Context(ctx).Get(c)
105+
if err != nil {
106+
return nil, errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()
107+
}
108+
if !exist {
109+
return nil, fmt.Errorf("config not found by key: %s", key)
110+
}
111+
return c, nil
112+
}
113+
102114
func (cr configRepo) UpdateConfig(ctx context.Context, key string, value string) (err error) {
103115
// check if key exists
104116
oldConfig := &entity.Config{Key: key}

internal/service/config/config_service.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ import (
3131
type ConfigRepo interface {
3232
GetConfigByID(ctx context.Context, id int) (c *entity.Config, err error)
3333
GetConfigByKey(ctx context.Context, key string) (c *entity.Config, err error)
34+
GetConfigByKeyFromDB(ctx context.Context, key string) (c *entity.Config, err error)
3435
UpdateConfig(ctx context.Context, key, value string) (err error)
3536
}
3637

@@ -64,6 +65,15 @@ func (cs *ConfigService) GetStringValue(ctx context.Context, key string) (val st
6465
return cf.Value, nil
6566
}
6667

68+
// GetStringValueFromDB gets config string value directly from DB, bypassing cache.
69+
func (cs *ConfigService) GetStringValueFromDB(ctx context.Context, key string) (val string, err error) {
70+
cf, err := cs.configRepo.GetConfigByKeyFromDB(ctx, key)
71+
if err != nil {
72+
return "", err
73+
}
74+
return cf.Value, nil
75+
}
76+
6777
// GetArrayStringValue get config array string value
6878
func (cs *ConfigService) GetArrayStringValue(ctx context.Context, key string) (val []string, err error) {
6979
cf, err := cs.configRepo.GetConfigByKey(ctx, key)

internal/service/plugin_common/plugin_common_service.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ func (ps *PluginCommonService) initPluginData() {
144144
})
145145

146146
// init plugin status
147-
pluginStatus, err := ps.configService.GetStringValue(context.TODO(), constant.PluginStatus)
147+
pluginStatus, err := ps.configService.GetStringValueFromDB(context.TODO(), constant.PluginStatus)
148148
if err != nil {
149149
log.Error(err)
150150
} else {

0 commit comments

Comments
 (0)