Skip to content

Commit 8a037f0

Browse files
committed
feat(cache): upgrade cache interface
1 parent 3b1324b commit 8a037f0

15 files changed

Lines changed: 83 additions & 58 deletions

File tree

cmd/main.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package answercmd
22

33
import (
4+
"context"
45
"fmt"
56
"os"
67
"time"
@@ -39,7 +40,7 @@ var (
3940
// @name Authorization
4041
func Main() {
4142
log.SetLogger(zap.NewLogger(
42-
log.ParseLevel(logLevel), zap.WithName("answer"), zap.WithPath(logPath), zap.WithCallerFullPath()))
43+
log.ParseLevel(logLevel), zap.WithName("answer"), zap.WithPath(logPath)))
4344
Execute()
4445
}
4546

@@ -59,7 +60,7 @@ func runApp() {
5960
fmt.Println("answer Version:", constant.Version, " Revision:", constant.Revision)
6061

6162
defer cleanup()
62-
if err := app.Run(); err != nil {
63+
if err := app.Run(context.Background()); err != nil {
6364
panic(err)
6465
}
6566
}

go.mod

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ require (
2929
github.com/ory/dockertest/v3 v3.9.1
3030
github.com/robfig/cron/v3 v3.0.1
3131
github.com/scottleedavis/go-exif-remove v0.0.0-20230314195146-7e059d593405
32-
github.com/segmentfault/pacman v1.0.4
33-
github.com/segmentfault/pacman/contrib/cache/memory v0.0.0-20221219081300-f734f4a16aa0
32+
github.com/segmentfault/pacman v1.0.5-0.20230822075009-309985fb8700
33+
github.com/segmentfault/pacman/contrib/cache/memory v0.0.0-20230822075009-309985fb8700
3434
github.com/segmentfault/pacman/contrib/conf/viper v0.0.0-20221018072427-a15dd1434e05
3535
github.com/segmentfault/pacman/contrib/i18n v0.0.0-20230516093754-b76aef1c1150
3636
github.com/segmentfault/pacman/contrib/log/zap v0.0.0-20221018072427-a15dd1434e05

go.sum

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -642,10 +642,10 @@ github.com/scottleedavis/go-exif-remove v0.0.0-20230314195146-7e059d593405 h1:2i
642642
github.com/scottleedavis/go-exif-remove v0.0.0-20230314195146-7e059d593405/go.mod h1:rIxVzVLKlBwLxO+lC+k/I4HJfRQcemg/f/76Xmmzsec=
643643
github.com/sean-/seed v0.0.0-20170313163322-e2103e2c3529/go.mod h1:DxrIzT+xaE7yg65j358z/aeFdxmN0P9QXhEzd20vsDc=
644644
github.com/seccomp/libseccomp-golang v0.9.2-0.20210429002308-3879420cc921/go.mod h1:JA8cRccbGaA1s33RQf7Y1+q9gHmZX1yB/z9WDN1C6fg=
645-
github.com/segmentfault/pacman v1.0.4 h1:6UIXuMHUeYMWe5toflV9SXZQizRny1RczjZJLj9kul0=
646-
github.com/segmentfault/pacman v1.0.4/go.mod h1:5lNp5REd8QMThmBUvR3Fi9Y3AsOB4GRq7soCB4QLqOs=
647-
github.com/segmentfault/pacman/contrib/cache/memory v0.0.0-20221219081300-f734f4a16aa0 h1:4x0qG7H2M3qH7Yo2BhGrVlji1iTmRAWgINY/JyENeHs=
648-
github.com/segmentfault/pacman/contrib/cache/memory v0.0.0-20221219081300-f734f4a16aa0/go.mod h1:rmf1TCwz67dyM+AmTwSd1BxTo2AOYHj262lP93bOZbs=
645+
github.com/segmentfault/pacman v1.0.5-0.20230822075009-309985fb8700 h1:VqxiuNGQg86GEKxnzmJegZR2Ufr7EVXo68mdKaf+/MQ=
646+
github.com/segmentfault/pacman v1.0.5-0.20230822075009-309985fb8700/go.mod h1:5lNp5REd8QMThmBUvR3Fi9Y3AsOB4GRq7soCB4QLqOs=
647+
github.com/segmentfault/pacman/contrib/cache/memory v0.0.0-20230822075009-309985fb8700 h1:VZpexPTcr7sOxxYUGa/9cneyCESfisAhCNbaEuTpcwI=
648+
github.com/segmentfault/pacman/contrib/cache/memory v0.0.0-20230822075009-309985fb8700/go.mod h1:rmf1TCwz67dyM+AmTwSd1BxTo2AOYHj262lP93bOZbs=
649649
github.com/segmentfault/pacman/contrib/conf/viper v0.0.0-20221018072427-a15dd1434e05 h1:BlqTgc3/MYKG6vMI2MI+6o+7P4Gy5PXlawu185wPXAk=
650650
github.com/segmentfault/pacman/contrib/conf/viper v0.0.0-20221018072427-a15dd1434e05/go.mod h1:prPjFam7MyZ5b3S9dcDOt2tMPz6kf7C9c243s9zSwPY=
651651
github.com/segmentfault/pacman/contrib/i18n v0.0.0-20230516093754-b76aef1c1150 h1:OEuW1D7RGDE0CZDr0oGMw9Eiq7fAbD9C4WMrvSixamk=

internal/repo/auth/auth.go

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,15 @@ func NewAuthRepo(data *data.Data) auth.AuthRepo {
2727

2828
// GetUserCacheInfo get user cache info
2929
func (ar *authRepo) GetUserCacheInfo(ctx context.Context, accessToken string) (userInfo *entity.UserCacheInfo, err error) {
30-
userInfoCache, err := ar.data.Cache.GetString(ctx, constant.UserTokenCacheKey+accessToken)
30+
userInfoCache, exist, err := ar.data.Cache.GetString(ctx, constant.UserTokenCacheKey+accessToken)
3131
if err != nil {
3232
return nil, errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()
3333
}
3434
userInfo = &entity.UserCacheInfo{}
35-
err = json.Unmarshal([]byte(userInfoCache), userInfo)
36-
if err != nil {
37-
return nil, errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()
35+
if !exist {
36+
return nil, nil
3837
}
38+
_ = json.Unmarshal([]byte(userInfoCache), userInfo)
3939
return userInfo, nil
4040
}
4141

@@ -81,15 +81,15 @@ func (ar *authRepo) SetUserStatus(ctx context.Context, userID string, userInfo *
8181

8282
// GetUserStatus get user status
8383
func (ar *authRepo) GetUserStatus(ctx context.Context, userID string) (userInfo *entity.UserCacheInfo, err error) {
84-
userInfoCache, err := ar.data.Cache.GetString(ctx, constant.UserStatusChangedCacheKey+userID)
84+
userInfoCache, exist, err := ar.data.Cache.GetString(ctx, constant.UserStatusChangedCacheKey+userID)
8585
if err != nil {
8686
return nil, errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()
8787
}
88-
userInfo = &entity.UserCacheInfo{}
89-
err = json.Unmarshal([]byte(userInfoCache), userInfo)
90-
if err != nil {
91-
return nil, errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()
88+
if !exist {
89+
return nil, nil
9290
}
91+
userInfo = &entity.UserCacheInfo{}
92+
_ = json.Unmarshal([]byte(userInfoCache), userInfo)
9393
return userInfo, nil
9494
}
9595

@@ -104,16 +104,16 @@ func (ar *authRepo) RemoveUserStatus(ctx context.Context, userID string) (err er
104104

105105
// GetAdminUserCacheInfo get admin user cache info
106106
func (ar *authRepo) GetAdminUserCacheInfo(ctx context.Context, accessToken string) (userInfo *entity.UserCacheInfo, err error) {
107-
userInfoCache, err := ar.data.Cache.GetString(ctx, constant.AdminTokenCacheKey+accessToken)
107+
userInfoCache, exist, err := ar.data.Cache.GetString(ctx, constant.AdminTokenCacheKey+accessToken)
108108
if err != nil {
109109
err = errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()
110110
return
111111
}
112-
userInfo = &entity.UserCacheInfo{}
113-
err = json.Unmarshal([]byte(userInfoCache), userInfo)
114-
if err != nil {
115-
return nil, err
112+
if !exist {
113+
return nil, nil
116114
}
115+
userInfo = &entity.UserCacheInfo{}
116+
_ = json.Unmarshal([]byte(userInfoCache), userInfo)
117117
return userInfo, nil
118118
}
119119

@@ -144,7 +144,10 @@ func (ar *authRepo) RemoveAdminUserCacheInfo(ctx context.Context, accessToken st
144144
// AddUserTokenMapping add user token mapping
145145
func (ar *authRepo) AddUserTokenMapping(ctx context.Context, userID, accessToken string) (err error) {
146146
key := constant.UserTokenMappingCacheKey + userID
147-
resp, _ := ar.data.Cache.GetString(ctx, key)
147+
resp, _, err := ar.data.Cache.GetString(ctx, key)
148+
if err != nil {
149+
return err
150+
}
148151
mapping := make(map[string]bool, 0)
149152
if len(resp) > 0 {
150153
_ = json.Unmarshal([]byte(resp), &mapping)
@@ -157,7 +160,10 @@ func (ar *authRepo) AddUserTokenMapping(ctx context.Context, userID, accessToken
157160
// RemoveUserTokens Log out all users under this user id
158161
func (ar *authRepo) RemoveUserTokens(ctx context.Context, userID string, remainToken string) {
159162
key := constant.UserTokenMappingCacheKey + userID
160-
resp, _ := ar.data.Cache.GetString(ctx, key)
163+
resp, _, err := ar.data.Cache.GetString(ctx, key)
164+
if err != nil {
165+
return
166+
}
161167
mapping := make(map[string]bool, 0)
162168
if len(resp) > 0 {
163169
_ = json.Unmarshal([]byte(resp), &mapping)

internal/repo/captcha/captcha.go

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -43,19 +43,18 @@ func (cr *captchaRepo) SetActionType(ctx context.Context, unit, actionType, conf
4343
return
4444
}
4545

46-
func (cr *captchaRepo) GetActionType(ctx context.Context, unit, actionType string) (actioninfo *entity.ActionRecordInfo, err error) {
46+
func (cr *captchaRepo) GetActionType(ctx context.Context, unit, actionType string) (actionInfo *entity.ActionRecordInfo, err error) {
4747
now := time.Now()
4848
cacheKey := fmt.Sprintf("ActionRecord:%s@%s@%s", unit, actionType, now.Format("2006-1-02"))
49-
actioninfo = &entity.ActionRecordInfo{}
50-
res, err := cr.data.Cache.GetString(ctx, cacheKey)
49+
res, exist, err := cr.data.Cache.GetString(ctx, cacheKey)
5150
if err != nil {
52-
err = errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()
51+
return nil, err
5352
}
54-
err = json.Unmarshal([]byte(res), actioninfo)
55-
if err != nil {
56-
return actioninfo, nil
53+
actionInfo = &entity.ActionRecordInfo{}
54+
if exist {
55+
_ = json.Unmarshal([]byte(res), actionInfo)
5756
}
58-
return actioninfo, nil
57+
return actionInfo, nil
5958
}
6059

6160
func (cr *captchaRepo) DelActionType(ctx context.Context, unit, actionType string) (err error) {
@@ -79,9 +78,12 @@ func (cr *captchaRepo) SetCaptcha(ctx context.Context, key, captcha string) (err
7978

8079
// GetCaptcha get captcha from cache
8180
func (cr *captchaRepo) GetCaptcha(ctx context.Context, key string) (captcha string, err error) {
82-
captcha, err = cr.data.Cache.GetString(ctx, key)
81+
captcha, exist, err := cr.data.Cache.GetString(ctx, key)
8382
if err != nil {
84-
log.Debug(err)
83+
return "", err
84+
}
85+
if !exist {
86+
return "", fmt.Errorf("captcha not exist")
8587
}
8688
return captcha, nil
8789
}

internal/repo/config/config_repo.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ 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, err := cr.data.Cache.GetString(ctx, cacheKey); err == nil && len(cacheData) > 0 {
31+
if cacheData, exist, err := cr.data.Cache.GetString(ctx, cacheKey); err == nil && exist {
3232
c = &entity.Config{}
3333
c.BuildByJSON([]byte(cacheData))
3434
if c.ID > 0 {
@@ -54,7 +54,7 @@ func (cr configRepo) GetConfigByID(ctx context.Context, id int) (c *entity.Confi
5454

5555
func (cr configRepo) GetConfigByKey(ctx context.Context, key string) (c *entity.Config, err error) {
5656
cacheKey := constant.ConfigKEY2ContentCacheKeyPrefix + key
57-
if cacheData, err := cr.data.Cache.GetString(ctx, cacheKey); err == nil && len(cacheData) > 0 {
57+
if cacheData, exist, err := cr.data.Cache.GetString(ctx, cacheKey); err == nil && exist {
5858
c = &entity.Config{}
5959
c.BuildByJSON([]byte(cacheData))
6060
if c.ID > 0 {

internal/repo/export/email_repo.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,12 @@ func (e *emailRepo) SetCode(ctx context.Context, code, content string, duration
3333

3434
// VerifyCode verify the code if out of date
3535
func (e *emailRepo) VerifyCode(ctx context.Context, code string) (content string, err error) {
36-
content, err = e.data.Cache.GetString(ctx, code)
36+
content, exist, err := e.data.Cache.GetString(ctx, code)
3737
if err != nil {
38-
err = errors.InternalServer(reason.DatabaseError).WithError(err).WithStack()
38+
return "", err
3939
}
40-
return
40+
if !exist {
41+
return "", nil
42+
}
43+
return content, nil
4144
}

internal/repo/question/question_repo.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -259,8 +259,8 @@ func (qr *questionRepo) SitemapQuestions(ctx context.Context, page, pageSize int
259259

260260
// try to get sitemap data from cache
261261
cacheKey := fmt.Sprintf(constant.SiteMapQuestionCacheKeyPrefix, page)
262-
cacheData, err := qr.data.Cache.GetString(ctx, cacheKey)
263-
if err == nil && len(cacheKey) > 0 {
262+
cacheData, exist, err := qr.data.Cache.GetString(ctx, cacheKey)
263+
if err == nil && exist {
264264
_ = json.Unmarshal([]byte(cacheData), &questionIDList)
265265
return questionIDList, nil
266266
}

internal/repo/site_info/siteinfo_repo.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,12 +61,14 @@ func (sr *siteInfoRepo) GetByType(ctx context.Context, siteType string) (siteInf
6161
}
6262

6363
func (sr *siteInfoRepo) getCache(ctx context.Context, siteType string) (siteInfo *entity.SiteInfo) {
64-
siteInfo = &entity.SiteInfo{}
65-
siteInfoCache, err := sr.data.Cache.GetString(ctx, constant.SiteInfoCacheKey+siteType)
64+
siteInfoCache, exist, err := sr.data.Cache.GetString(ctx, constant.SiteInfoCacheKey+siteType)
6665
if err != nil {
6766
return nil
6867
}
69-
_ = json.Unmarshal([]byte(siteInfoCache), siteInfo)
68+
siteInfo = &entity.SiteInfo{}
69+
if exist {
70+
_ = json.Unmarshal([]byte(siteInfoCache), siteInfo)
71+
}
7072
return siteInfo
7173
}
7274

internal/repo/user_external_login/user_external_login_repo.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,10 +85,13 @@ func (ur *userExternalLoginRepo) SetCacheUserExternalLoginInfo(
8585
// GetCacheUserExternalLoginInfo cache user info for external login
8686
func (ur *userExternalLoginRepo) GetCacheUserExternalLoginInfo(
8787
ctx context.Context, key string) (info *schema.ExternalLoginUserInfoCache, err error) {
88-
res, err := ur.data.Cache.GetString(ctx, constant.ConnectorUserExternalInfoCacheKey+key)
88+
res, exist, err := ur.data.Cache.GetString(ctx, constant.ConnectorUserExternalInfoCacheKey+key)
8989
if err != nil {
9090
return info, err
9191
}
92-
_ = json.Unmarshal([]byte(res), &info)
92+
info = &schema.ExternalLoginUserInfoCache{}
93+
if exist {
94+
_ = json.Unmarshal([]byte(res), &info)
95+
}
9396
return info, nil
9497
}

0 commit comments

Comments
 (0)