Skip to content

Commit

Permalink
fix: do cache clear after polled
Browse files Browse the repository at this point in the history
  • Loading branch information
greenhat616 committed Aug 9, 2023
1 parent f9ed975 commit 89b7594
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 1 deletion.
45 changes: 44 additions & 1 deletion internal/logic/cache/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ package cache
import (
"context"

"github.com/gogf/gf/v2/errors/gerror"
"github.com/gogf/gf/v2/util/gconv"

"github.com/hitokoto-osc/reviewer/internal/service"

"github.com/gogf/gf/v2/frame/g"
Expand All @@ -21,7 +24,7 @@ func New() service.ICache {
}

func (s *sCache) RemovePrefix(ctx context.Context, prefix string) error {
keysToRemove := make([]any, 0)
keysToRemove := make([]any, 0, 20) //nolint:gomnd
keys, err := s.instance.KeyStrings(ctx)
if err != nil {
return err
Expand All @@ -36,3 +39,43 @@ func (s *sCache) RemovePrefix(ctx context.Context, prefix string) error {
}
return err
}

func (s *sCache) RemovePrefixes(ctx context.Context, prefixes []string) error {
keysToRemove := make([]any, 0, 20) //nolint:gomnd
keys, err := s.instance.KeyStrings(ctx)
if err != nil {
return err
}
for _, key := range keys {
for _, prefix := range prefixes {
if gstr.HasPrefix(key, prefix) {
keysToRemove = append(keysToRemove, key)
}
}
}
if len(keysToRemove) > 0 {
_, err = s.instance.Remove(ctx, keysToRemove...)
}
return err
}

func (s *sCache) RemoveCacheAfterPollUpdated(ctx context.Context, userID, pollID uint, sentenceUUID string) {
if e := service.Cache().RemovePrefixes(ctx, []string{
"poll:list",
"poll_logs:uid:" + gconv.String(userID),
}); e != nil {
e = gerror.Wrap(e, "failed to remove cache: ")
g.Log().Error(ctx, e)
}
if e := g.DB().GetCache().Removes(ctx, g.Slice{
"poll:id:" + gconv.String(pollID),
"poll:sentence_uuid:" + sentenceUUID,
"poll_log:id:" + gconv.String(pollID),
"poll_log:sentence_uuid:" + sentenceUUID,
"poll_marks:pid:" + gconv.String(pollID),
"user:poll:uid:" + gconv.String(userID),
}); e != nil {
e = gerror.Wrap(e, "failed to remove cache: ")
g.Log().Error(ctx, e)
}
}
2 changes: 2 additions & 0 deletions internal/logic/poll/poll.go
Original file line number Diff line number Diff line change
Expand Up @@ -321,5 +321,7 @@ func (s *sPoll) Poll(ctx context.Context, in *model.PollInput) error {
if err != nil {
return gerror.Wrap(err, "poll failed")
}
// Remove caches
go service.Cache().RemoveCacheAfterPollUpdated(ctx, in.UserID, in.PollID, in.SentenceUUID)
return nil
}
2 changes: 2 additions & 0 deletions internal/service/cache.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 89b7594

Please sign in to comment.