From d752b0566cfe412cd55780e1e20bf88e0d203d28 Mon Sep 17 00:00:00 2001 From: Satont Date: Mon, 16 Dec 2024 03:22:52 +0300 Subject: [PATCH] upd --- .../delivery/http-public/public/badges.go | 2 ++ .../services/badges-users/badges-users.go | 34 ------------------- 2 files changed, 2 insertions(+), 34 deletions(-) diff --git a/apps/api-gql/internal/delivery/http-public/public/badges.go b/apps/api-gql/internal/delivery/http-public/public/badges.go index acfc33295..62ac38d92 100644 --- a/apps/api-gql/internal/delivery/http-public/public/badges.go +++ b/apps/api-gql/internal/delivery/http-public/public/badges.go @@ -22,6 +22,8 @@ type badgeWithUsers struct { Users []string `json:"users"` } +// TODO: use some gin middleware for cache response + func (p *Public) HandleBadgesGet(c *gin.Context) { entities, err := p.badgesWithUsersService.GetMany( c.Request.Context(), diff --git a/apps/api-gql/internal/services/badges-users/badges-users.go b/apps/api-gql/internal/services/badges-users/badges-users.go index a8072a600..066030b1e 100644 --- a/apps/api-gql/internal/services/badges-users/badges-users.go +++ b/apps/api-gql/internal/services/badges-users/badges-users.go @@ -2,11 +2,8 @@ package badges_users import ( "context" - "time" - "github.com/goccy/go-json" "github.com/google/uuid" - "github.com/redis/go-redis/v9" "github.com/twirapp/twir/apps/api-gql/internal/entity" badges_users "github.com/twirapp/twir/libs/repositories/badges-users" "github.com/twirapp/twir/libs/repositories/badges-users/model" @@ -17,19 +14,16 @@ type Opts struct { fx.In BadgesUsersRepository badges_users.Repository - Redis *redis.Client } func New(opts Opts) *Service { return &Service{ badgesUsersRepository: opts.BadgesUsersRepository, - redis: opts.Redis, } } type Service struct { badgesUsersRepository badges_users.Repository - redis *redis.Client } type GetManyInput struct { @@ -45,19 +39,7 @@ func modelToEntity(b model.BadgeUser) entity.BadgeUser { } } -const badgeUsersCacheKey = "cache:twir:badges_users:" - func (c *Service) GetMany(ctx context.Context, input GetManyInput) ([]entity.BadgeUser, error) { - cachedUsers, _ := c.redis.Get(ctx, badgeUsersCacheKey+input.BadgeID.String()).Bytes() - if len(cachedUsers) > 0 { - var result []entity.BadgeUser - if err := json.Unmarshal(cachedUsers, &result); err != nil { - return nil, err - } - - return result, nil - } - selectedBadgeUsers, err := c.badgesUsersRepository.GetMany( ctx, badges_users.GetManyInput{ @@ -73,21 +55,5 @@ func (c *Service) GetMany(ctx context.Context, input GetManyInput) ([]entity.Bad result = append(result, modelToEntity(b)) } - if len(result) > 0 { - cacheBytes, err := json.Marshal(result) - if err != nil { - return nil, err - } - - if err := c.redis.Set( - ctx, - badgeUsersCacheKey+input.BadgeID.String(), - cacheBytes, - 1*time.Minute, - ).Err(); err != nil { - return nil, err - } - } - return result, nil }