Skip to content

Commit 758309d

Browse files
committed
feat(config): set config cache time
1 parent 750fdb0 commit 758309d

3 files changed

Lines changed: 15 additions & 8 deletions

File tree

internal/base/constant/cache_key.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ const (
1414
SiteInfoCacheTime = 1 * time.Hour
1515
ConfigID2KEYCacheKeyPrefix = "answer:config:id:"
1616
ConfigKEY2ContentCacheKeyPrefix = "answer:config:key:"
17+
ConfigCacheTime = 1 * time.Hour
1718
ConnectorUserExternalInfoCacheKey = "answer:connector:"
1819
ConnectorUserExternalInfoCacheTime = 10 * time.Minute
1920
SiteMapQuestionCacheKeyPrefix = "answer:sitemap:question:%d"

internal/entity/config_entity.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package entity
22

33
import (
44
"encoding/json"
5+
"github.com/segmentfault/pacman/log"
56

67
"github.com/answerdev/answer/pkg/converter"
78
)
@@ -33,6 +34,9 @@ func (c *Config) JsonString() string {
3334

3435
// GetIntValue get int value
3536
func (c *Config) GetIntValue() int {
37+
if len(c.Value) == 0 {
38+
log.Warnf("config value is empty, key: %s, value: %s", c.Key, c.Value)
39+
}
3640
return converter.StringToInt(c.Value)
3741
}
3842

internal/repo/config/config_repo.go

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@ func NewConfigRepo(data *data.Data) config.ConfigRepo {
2828

2929
func (cr configRepo) GetConfigByID(ctx context.Context, id int) (c *entity.Config, err error) {
3030
cacheKey := fmt.Sprintf("%s%d", constant.ConfigID2KEYCacheKeyPrefix, id)
31-
if cacheData, exist, err := cr.data.Cache.GetString(ctx, cacheKey); err == nil && exist {
31+
cacheData, exist, err := cr.data.Cache.GetString(ctx, cacheKey)
32+
if err == nil && exist && len(cacheData) > 0 {
3233
c = &entity.Config{}
3334
c.BuildByJSON([]byte(cacheData))
3435
if c.ID > 0 {
@@ -37,7 +38,7 @@ func (cr configRepo) GetConfigByID(ctx context.Context, id int) (c *entity.Confi
3738
}
3839

3940
c = &entity.Config{}
40-
exist, err := cr.data.DB.Context(ctx).ID(id).Get(c)
41+
exist, err = cr.data.DB.Context(ctx).ID(id).Get(c)
4142
if err != nil {
4243
return nil, errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()
4344
}
@@ -46,15 +47,16 @@ func (cr configRepo) GetConfigByID(ctx context.Context, id int) (c *entity.Confi
4647
}
4748

4849
// update cache
49-
if err := cr.data.Cache.SetString(ctx, cacheKey, c.JsonString(), -1); err != nil {
50+
if err := cr.data.Cache.SetString(ctx, cacheKey, c.JsonString(), constant.ConfigCacheTime); err != nil {
5051
log.Error(err)
5152
}
5253
return c, nil
5354
}
5455

5556
func (cr configRepo) GetConfigByKey(ctx context.Context, key string) (c *entity.Config, err error) {
5657
cacheKey := constant.ConfigKEY2ContentCacheKeyPrefix + key
57-
if cacheData, exist, err := cr.data.Cache.GetString(ctx, cacheKey); err == nil && exist {
58+
cacheData, exist, err := cr.data.Cache.GetString(ctx, cacheKey)
59+
if err == nil && exist && len(cacheData) > 0 {
5860
c = &entity.Config{}
5961
c.BuildByJSON([]byte(cacheData))
6062
if c.ID > 0 {
@@ -63,7 +65,7 @@ func (cr configRepo) GetConfigByKey(ctx context.Context, key string) (c *entity.
6365
}
6466

6567
c = &entity.Config{Key: key}
66-
exist, err := cr.data.DB.Context(ctx).Get(c)
68+
exist, err = cr.data.DB.Context(ctx).Get(c)
6769
if err != nil {
6870
return nil, errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()
6971
}
@@ -72,7 +74,7 @@ func (cr configRepo) GetConfigByKey(ctx context.Context, key string) (c *entity.
7274
}
7375

7476
// update cache
75-
if err := cr.data.Cache.SetString(ctx, cacheKey, c.JsonString(), -1); err != nil {
77+
if err := cr.data.Cache.SetString(ctx, cacheKey, c.JsonString(), constant.ConfigCacheTime); err != nil {
7678
log.Error(err)
7779
}
7880
return c, nil
@@ -99,11 +101,11 @@ func (cr configRepo) UpdateConfig(ctx context.Context, key string, value string)
99101
cacheVal := oldConfig.JsonString()
100102
// update cache
101103
if err := cr.data.Cache.SetString(ctx,
102-
constant.ConfigKEY2ContentCacheKeyPrefix+key, cacheVal, -1); err != nil {
104+
constant.ConfigKEY2ContentCacheKeyPrefix+key, cacheVal, constant.ConfigCacheTime); err != nil {
103105
log.Error(err)
104106
}
105107
if err := cr.data.Cache.SetString(ctx,
106-
fmt.Sprintf("%s%d", constant.ConfigID2KEYCacheKeyPrefix, oldConfig.ID), cacheVal, -1); err != nil {
108+
fmt.Sprintf("%s%d", constant.ConfigID2KEYCacheKeyPrefix, oldConfig.ID), cacheVal, constant.ConfigCacheTime); err != nil {
107109
log.Error(err)
108110
}
109111
return

0 commit comments

Comments
 (0)