mirror of https://gitee.com/answerdev/answer.git
feat(config): set config cache time
This commit is contained in:
parent
750fdb0eef
commit
758309d77b
|
@ -14,6 +14,7 @@ const (
|
||||||
SiteInfoCacheTime = 1 * time.Hour
|
SiteInfoCacheTime = 1 * time.Hour
|
||||||
ConfigID2KEYCacheKeyPrefix = "answer:config:id:"
|
ConfigID2KEYCacheKeyPrefix = "answer:config:id:"
|
||||||
ConfigKEY2ContentCacheKeyPrefix = "answer:config:key:"
|
ConfigKEY2ContentCacheKeyPrefix = "answer:config:key:"
|
||||||
|
ConfigCacheTime = 1 * time.Hour
|
||||||
ConnectorUserExternalInfoCacheKey = "answer:connector:"
|
ConnectorUserExternalInfoCacheKey = "answer:connector:"
|
||||||
ConnectorUserExternalInfoCacheTime = 10 * time.Minute
|
ConnectorUserExternalInfoCacheTime = 10 * time.Minute
|
||||||
SiteMapQuestionCacheKeyPrefix = "answer:sitemap:question:%d"
|
SiteMapQuestionCacheKeyPrefix = "answer:sitemap:question:%d"
|
||||||
|
|
|
@ -2,6 +2,7 @@ package entity
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"github.com/segmentfault/pacman/log"
|
||||||
|
|
||||||
"github.com/answerdev/answer/pkg/converter"
|
"github.com/answerdev/answer/pkg/converter"
|
||||||
)
|
)
|
||||||
|
@ -33,6 +34,9 @@ func (c *Config) JsonString() string {
|
||||||
|
|
||||||
// GetIntValue get int value
|
// GetIntValue get int value
|
||||||
func (c *Config) GetIntValue() int {
|
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)
|
return converter.StringToInt(c.Value)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -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) {
|
func (cr configRepo) GetConfigByID(ctx context.Context, id int) (c *entity.Config, err error) {
|
||||||
cacheKey := fmt.Sprintf("%s%d", constant.ConfigID2KEYCacheKeyPrefix, id)
|
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 = &entity.Config{}
|
||||||
c.BuildByJSON([]byte(cacheData))
|
c.BuildByJSON([]byte(cacheData))
|
||||||
if c.ID > 0 {
|
if c.ID > 0 {
|
||||||
|
@ -37,7 +38,7 @@ func (cr configRepo) GetConfigByID(ctx context.Context, id int) (c *entity.Confi
|
||||||
}
|
}
|
||||||
|
|
||||||
c = &entity.Config{}
|
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 {
|
if err != nil {
|
||||||
return nil, errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()
|
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
|
// 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)
|
log.Error(err)
|
||||||
}
|
}
|
||||||
return c, nil
|
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) {
|
func (cr configRepo) GetConfigByKey(ctx context.Context, key string) (c *entity.Config, err error) {
|
||||||
cacheKey := constant.ConfigKEY2ContentCacheKeyPrefix + key
|
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 = &entity.Config{}
|
||||||
c.BuildByJSON([]byte(cacheData))
|
c.BuildByJSON([]byte(cacheData))
|
||||||
if c.ID > 0 {
|
if c.ID > 0 {
|
||||||
|
@ -63,7 +65,7 @@ func (cr configRepo) GetConfigByKey(ctx context.Context, key string) (c *entity.
|
||||||
}
|
}
|
||||||
|
|
||||||
c = &entity.Config{Key: key}
|
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 {
|
if err != nil {
|
||||||
return nil, errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()
|
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
|
// 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)
|
log.Error(err)
|
||||||
}
|
}
|
||||||
return c, nil
|
return c, nil
|
||||||
|
@ -99,11 +101,11 @@ func (cr configRepo) UpdateConfig(ctx context.Context, key string, value string)
|
||||||
cacheVal := oldConfig.JsonString()
|
cacheVal := oldConfig.JsonString()
|
||||||
// update cache
|
// update cache
|
||||||
if err := cr.data.Cache.SetString(ctx,
|
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)
|
log.Error(err)
|
||||||
}
|
}
|
||||||
if err := cr.data.Cache.SetString(ctx,
|
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)
|
log.Error(err)
|
||||||
}
|
}
|
||||||
return
|
return
|
||||||
|
|
Loading…
Reference in New Issue