Skip to content

Commit

Permalink
feat: add cache for account existence
Browse files Browse the repository at this point in the history
  • Loading branch information
GalvinGao committed May 14, 2024
1 parent 39b7a79 commit b6ac0a5
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 5 deletions.
3 changes: 3 additions & 0 deletions internal/model/cache/caches.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ type Flusher func() error
var (
AccountByID *cache.Set[model.Account]
AccountByPenguinID *cache.Set[model.Account]
AccountExistence *cache.Set[int]

ItemDropSetByStageIDAndRangeID *cache.Set[[]int]
ItemDropSetByStageIdAndTimeRange *cache.Set[[]int]
Expand Down Expand Up @@ -116,9 +117,11 @@ func initializeCaches() {
// account
AccountByID = cache.NewSet[model.Account]("account#accountId")
AccountByPenguinID = cache.NewSet[model.Account]("account#penguinId")
AccountExistence = cache.NewSet[int]("accountExistence#accountId")

SetMap["account#accountId"] = AccountByID.Flush
SetMap["account#penguinId"] = AccountByPenguinID.Flush
SetMap["accountExistence#accountId"] = AccountExistence.Flush

// drop_info
ItemDropSetByStageIDAndRangeID = cache.NewSet[[]int]("itemDropSet#server|stageId|rangeId")
Expand Down
15 changes: 14 additions & 1 deletion internal/repo/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@ package repo

import (
"context"
"strconv"
"time"

"github.com/rs/zerolog/log"
"github.com/uptrace/bun"

"exusiai.dev/backend-next/internal/model"
"exusiai.dev/backend-next/internal/model/cache"
"exusiai.dev/backend-next/internal/pkg/pgerr"
"exusiai.dev/backend-next/internal/pkg/pgid"
"exusiai.dev/backend-next/internal/repo/selector"
Expand Down Expand Up @@ -74,12 +76,23 @@ func (r *Account) GetAccountByPenguinId(ctx context.Context, penguinId string) (
}

func (r *Account) IsAccountExistWithId(ctx context.Context, accountId int) bool {
var exist int
err := cache.AccountExistence.Get(strconv.Itoa(accountId), &exist)
if err == nil {
return exist == 1
}
account, err := r.sel.SelectOne(ctx, func(q *bun.SelectQuery) *bun.SelectQuery {
return q.Column("account_id").Where("account_id = ?", accountId)
})
if err != nil {
return false
}

return account != nil
exists := account != nil

if exists {
cache.AccountExistence.Set(strconv.Itoa(account.AccountID), 1, time.Hour*24)
}

return exists
}
7 changes: 4 additions & 3 deletions internal/repo/drop_report_extra.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,15 +73,16 @@ func (c *DropReportExtra) DeleteDropReportExtrasForArchive(ctx context.Context,
func (c *DropReportExtra) IsDropReportExtraMD5Exist(ctx context.Context, md5 string) bool {
var dropReportExtra model.DropReportExtra

count, err := c.db.NewSelect().
err := c.db.NewSelect().
Model(&dropReportExtra).
Where("md5 = ?", md5).
Count(ctx)
Limit(1).
Scan(ctx)
if err != nil {
return false
}

return count > 0
return true
}

func (r *DropReportExtra) CreateDropReportExtra(ctx context.Context, tx bun.Tx, report *model.DropReportExtra) error {
Expand Down
2 changes: 1 addition & 1 deletion internal/util/reportverifs/verify_md5.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ package reportverifs
import (
"context"

"exusiai.dev/gommon/constant"
"github.com/pkg/errors"

"exusiai.dev/backend-next/internal/model/types"
"exusiai.dev/backend-next/internal/repo"
"exusiai.dev/gommon/constant"
)

var ErrMD5Conflict = errors.New("report with specified md5 has already existed")
Expand Down

0 comments on commit b6ac0a5

Please sign in to comment.