From 41389873a9a57724c28a03cbac433ca0beaf61ec Mon Sep 17 00:00:00 2001 From: Smirnov Oleksandr Date: Thu, 24 Oct 2024 18:54:03 +0300 Subject: [PATCH] feat(usersrv): actually cache things --- internal/service/usersrv/usersrv.go | 31 ++++++++++++++++++++--- internal/store/rdb/usercache/usercache.go | 18 ++++++------- 2 files changed, 35 insertions(+), 14 deletions(-) diff --git a/internal/service/usersrv/usersrv.go b/internal/service/usersrv/usersrv.go index 756d103..9dd2b99 100644 --- a/internal/service/usersrv/usersrv.go +++ b/internal/service/usersrv/usersrv.go @@ -3,6 +3,7 @@ package usersrv import ( "context" "errors" + "log/slog" "time" "github.com/gofrs/uuid/v5" @@ -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) { diff --git a/internal/store/rdb/usercache/usercache.go b/internal/store/rdb/usercache/usercache.go index 0c3ade6..880b6e1 100644 --- a/internal/store/rdb/usercache/usercache.go +++ b/internal/store/rdb/usercache/usercache.go @@ -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) @@ -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