@@ -28,7 +28,8 @@ func NewConfigRepo(data *data.Data) config.ConfigRepo {
2828
2929func (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
5556func (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