Skip to content

Commit

Permalink
feat(usersrv): actually cache things
Browse files Browse the repository at this point in the history
  • Loading branch information
olexsmir committed Oct 24, 2024
1 parent 113664b commit 4138987
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 14 deletions.
31 changes: 27 additions & 4 deletions internal/service/usersrv/usersrv.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package usersrv
import (
"context"
"errors"
"log/slog"
"time"

"github.com/gofrs/uuid/v5"
Expand Down Expand Up @@ -231,19 +232,41 @@ func (u *UserSrv) ParseJWTToken(token string) (jwtutil.Payload, error) {
}

func (u UserSrv) CheckIfUserExists(ctx context.Context, id uuid.UUID) (bool, error) {
if r, err := u.cache.GetUserIsExists(ctx, id.String()); err == nil {
if r, err := u.cache.GetIsExists(ctx, id.String()); err != nil {
slog.ErrorContext(ctx, "usercache", "err", err)
} else if err == nil {
return r, nil
}

return u.userstore.CheckIfUserExists(ctx, id)
isExists, err := u.userstore.CheckIfUserExists(ctx, id)
if err != nil {
return false, err
}

if err := u.cache.SetIsExists(ctx, id.String(), isExists); err != nil {
slog.Error("usercache", "err", err)
}

return isExists, nil
}

func (u UserSrv) CheckIfUserIsActivated(ctx context.Context, userID uuid.UUID) (bool, error) {
if r, err := u.cache.GetUserIsActivated(ctx, userID.String()); err == nil {
if r, err := u.cache.GetIsActivated(ctx, userID.String()); err != nil {
slog.ErrorContext(ctx, "usercache", "err", err)
} else if err == nil {
return r, nil
}

return u.userstore.CheckIfUserIsActivated(ctx, userID)
isActivated, err := u.userstore.CheckIfUserExists(ctx, userID)
if err != nil {
return false, err
}

if err := u.cache.SetIsActivated(ctx, userID.String(), isActivated); err != nil {
slog.Error("usercache", "err", err)
}

return isActivated, nil
}

func (u UserSrv) getTokens(userID uuid.UUID) (dtos.TokensDTO, error) {
Expand Down
18 changes: 8 additions & 10 deletions internal/store/rdb/usercache/usercache.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ import (
)

type UserCacheer interface {
SetUserIsExists(ctx context.Context, userID string, isExists bool) error
GetUserIsExists(ctx context.Context, userID string) (isExists bool, err error)
SetIsExists(ctx context.Context, userID string, isExists bool) error
GetIsExists(ctx context.Context, userID string) (isExists bool, err error)

SetUserIsActivated(ctx context.Context, userID string, isActivated bool) error
GetUserIsActivated(ctx context.Context, userID string) (isActivated bool, err error)
SetIsActivated(ctx context.Context, userID string, isActivated bool) error
GetIsActivated(ctx context.Context, userID string) (isActivated bool, err error)
}

var _ UserCacheer = (*UserCache)(nil)
Expand All @@ -31,34 +31,32 @@ func New(rdb *redis.Client, ttl time.Duration) *UserCache {
}
}

func (u *UserCache) SetUserIsExists(ctx context.Context, userID string, val bool) error {
func (u *UserCache) SetIsExists(ctx context.Context, userID string, val bool) error {
_, err := u.rdb.
Set(ctx, getKey("exists", userID), val, u.ttl).
Result()
return err
}

func (u *UserCache) GetUserIsExists(ctx context.Context, userID string) (bool, error) {
func (u *UserCache) GetIsExists(ctx context.Context, userID string) (bool, error) {
res, err := u.rdb.Get(ctx, getKey(userID, "exists")).Bool()
if err != nil {
slog.ErrorContext(ctx, "usercache", "err", err)
return false, err
}

return res, nil
}

func (u *UserCache) SetUserIsActivated(ctx context.Context, userID string, val bool) error {
func (u *UserCache) SetIsActivated(ctx context.Context, userID string, val bool) error {
_, err := u.rdb.
Set(ctx, getKey("activated", userID), val, u.ttl).
Result()
return err
}

func (u *UserCache) GetUserIsActivated(ctx context.Context, userID string) (bool, error) {
func (u *UserCache) GetIsActivated(ctx context.Context, userID string) (bool, error) {
res, err := u.rdb.Get(ctx, getKey(userID, "activated")).Bool()
if err != nil {
slog.ErrorContext(ctx, "usercache", "err", err)
return false, err
}
return res, nil
Expand Down

0 comments on commit 4138987

Please sign in to comment.