feat(config): set config cache time

This commit is contained in:
LinkinStars 2023-08-29 16:47:38 +08:00
parent 750fdb0eef
commit 758309d77b
3 changed files with 15 additions and 8 deletions

View File

@ -14,6 +14,7 @@ const (
SiteInfoCacheTime = 1 * time.Hour
ConfigID2KEYCacheKeyPrefix = "answer:config:id:"
ConfigKEY2ContentCacheKeyPrefix = "answer:config:key:"
ConfigCacheTime = 1 * time.Hour
ConnectorUserExternalInfoCacheKey = "answer:connector:"
ConnectorUserExternalInfoCacheTime = 10 * time.Minute
SiteMapQuestionCacheKeyPrefix = "answer:sitemap:question:%d"

View File

@ -2,6 +2,7 @@ package entity
import (
"encoding/json"
"github.com/segmentfault/pacman/log"
"github.com/answerdev/answer/pkg/converter"
)
@ -33,6 +34,9 @@ func (c *Config) JsonString() string {
// GetIntValue get int value
func (c *Config) GetIntValue() int {
if len(c.Value) == 0 {
log.Warnf("config value is empty, key: %s, value: %s", c.Key, c.Value)
}
return converter.StringToInt(c.Value)
}

View File

@ -28,7 +28,8 @@ func NewConfigRepo(data *data.Data) config.ConfigRepo {
func (cr configRepo) GetConfigByID(ctx context.Context, id int) (c *entity.Config, err error) {
cacheKey := fmt.Sprintf("%s%d", constant.ConfigID2KEYCacheKeyPrefix, id)
if cacheData, exist, err := cr.data.Cache.GetString(ctx, cacheKey); err == nil && exist {
cacheData, exist, err := cr.data.Cache.GetString(ctx, cacheKey)
if err == nil && exist && len(cacheData) > 0 {
c = &entity.Config{}
c.BuildByJSON([]byte(cacheData))
if c.ID > 0 {
@ -37,7 +38,7 @@ func (cr configRepo) GetConfigByID(ctx context.Context, id int) (c *entity.Confi
}
c = &entity.Config{}
exist, err := cr.data.DB.Context(ctx).ID(id).Get(c)
exist, err = cr.data.DB.Context(ctx).ID(id).Get(c)
if err != nil {
return nil, errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()
}
@ -46,7 +47,7 @@ func (cr configRepo) GetConfigByID(ctx context.Context, id int) (c *entity.Confi
}
// update cache
if err := cr.data.Cache.SetString(ctx, cacheKey, c.JsonString(), -1); err != nil {
if err := cr.data.Cache.SetString(ctx, cacheKey, c.JsonString(), constant.ConfigCacheTime); err != nil {
log.Error(err)
}
return c, nil
@ -54,7 +55,8 @@ func (cr configRepo) GetConfigByID(ctx context.Context, id int) (c *entity.Confi
func (cr configRepo) GetConfigByKey(ctx context.Context, key string) (c *entity.Config, err error) {
cacheKey := constant.ConfigKEY2ContentCacheKeyPrefix + key
if cacheData, exist, err := cr.data.Cache.GetString(ctx, cacheKey); err == nil && exist {
cacheData, exist, err := cr.data.Cache.GetString(ctx, cacheKey)
if err == nil && exist && len(cacheData) > 0 {
c = &entity.Config{}
c.BuildByJSON([]byte(cacheData))
if c.ID > 0 {
@ -63,7 +65,7 @@ func (cr configRepo) GetConfigByKey(ctx context.Context, key string) (c *entity.
}
c = &entity.Config{Key: key}
exist, err := cr.data.DB.Context(ctx).Get(c)
exist, err = cr.data.DB.Context(ctx).Get(c)
if err != nil {
return nil, errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()
}
@ -72,7 +74,7 @@ func (cr configRepo) GetConfigByKey(ctx context.Context, key string) (c *entity.
}
// update cache
if err := cr.data.Cache.SetString(ctx, cacheKey, c.JsonString(), -1); err != nil {
if err := cr.data.Cache.SetString(ctx, cacheKey, c.JsonString(), constant.ConfigCacheTime); err != nil {
log.Error(err)
}
return c, nil
@ -99,11 +101,11 @@ func (cr configRepo) UpdateConfig(ctx context.Context, key string, value string)
cacheVal := oldConfig.JsonString()
// update cache
if err := cr.data.Cache.SetString(ctx,
constant.ConfigKEY2ContentCacheKeyPrefix+key, cacheVal, -1); err != nil {
constant.ConfigKEY2ContentCacheKeyPrefix+key, cacheVal, constant.ConfigCacheTime); err != nil {
log.Error(err)
}
if err := cr.data.Cache.SetString(ctx,
fmt.Sprintf("%s%d", constant.ConfigID2KEYCacheKeyPrefix, oldConfig.ID), cacheVal, -1); err != nil {
fmt.Sprintf("%s%d", constant.ConfigID2KEYCacheKeyPrefix, oldConfig.ID), cacheVal, constant.ConfigCacheTime); err != nil {
log.Error(err)
}
return