Skip to content

Commit

Permalink
Add reading from cache
Browse files Browse the repository at this point in the history
  • Loading branch information
Polina Sheviakova committed Jul 26, 2024
1 parent c720996 commit 2c000e0
Show file tree
Hide file tree
Showing 13 changed files with 902 additions and 20 deletions.
3 changes: 2 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,14 @@ require (
github.com/gojuno/minimock/v3 v3.3.13
github.com/golang-jwt/jwt/v5 v5.2.1
github.com/golang/protobuf v1.5.4
github.com/gomodule/redigo v1.9.2
github.com/grpc-ecosystem/grpc-gateway/v2 v2.20.0
github.com/jackc/pgerrcode v0.0.0-20240316143900-6e2875d9b438
github.com/jackc/pgx/v5 v5.6.0
github.com/joho/godotenv v1.5.1
github.com/natefinch/lumberjack v2.0.0+incompatible
github.com/pkg/errors v0.9.1
github.com/polshe-v/microservices_common v0.0.0-20240614134025-0c890c19055d
github.com/polshe-v/microservices_common v0.0.0-20240725124217-0fcebed7fef8
github.com/prometheus/client_golang v1.19.1
github.com/rakyll/statik v0.1.7
github.com/rs/cors v1.11.0
Expand Down
6 changes: 4 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ github.com/golang-jwt/jwt/v5 v5.2.1 h1:OuVbFODueb089Lh128TAcimifWaLhJwVflnrgM17w
github.com/golang-jwt/jwt/v5 v5.2.1/go.mod h1:pqrtFR0X4osieyHYxtmOUWsAWrfe1Q5UVIyoH402zdk=
github.com/golang/protobuf v1.5.4 h1:i7eJL8qZTpSEXOPTxNKhASYpMn+8e5Q6AdndVa1dWek=
github.com/golang/protobuf v1.5.4/go.mod h1:lnTiLA8Wa4RWRcIUkrtSVa5nRhsEGBg48fD6rSs7xps=
github.com/gomodule/redigo v1.9.2 h1:HrutZBLhSIU8abiSfW8pj8mPhOyMYjZT/wcA4/L9L9s=
github.com/gomodule/redigo v1.9.2/go.mod h1:KsU3hiK/Ay8U42qpaJk+kuNa3C+spxapWpM+ywhcgtw=
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
Expand Down Expand Up @@ -66,8 +68,8 @@ github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/polshe-v/microservices_common v0.0.0-20240614134025-0c890c19055d h1:QQT0ZAHq5cmY3ExIzSwkb+pW5pLiayAostC2TO2/yfA=
github.com/polshe-v/microservices_common v0.0.0-20240614134025-0c890c19055d/go.mod h1:GXrHUWaU8yp9SPhdv/wyXcYM4fyvvXZYN8gelJRKNxk=
github.com/polshe-v/microservices_common v0.0.0-20240725124217-0fcebed7fef8 h1:yyv+1RSy4CwRUN+myBih6t+eCn+hzbGJFCJhy2VUH1w=
github.com/polshe-v/microservices_common v0.0.0-20240725124217-0fcebed7fef8/go.mod h1:RVZzF43a2rjqcgx2zz378b/dPHYCwSqY5czSW4ClbrU=
github.com/prometheus/client_golang v1.19.1 h1:wZWJDwK+NameRJuPGDhlnFgx8e8HN3XHQeLaYJFJBOE=
github.com/prometheus/client_golang v1.19.1/go.mod h1:mP78NwGzrVks5S2H6ab8+ZZGJLZUq1hoULYBAYBw1Ho=
github.com/prometheus/client_model v0.6.1 h1:ZKSh/rekM+n3CeS952MLRAdFwIKqeY8b62p8ais2e9E=
Expand Down
27 changes: 19 additions & 8 deletions internal/app/service_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/polshe-v/microservices_auth/internal/config/env"
"github.com/polshe-v/microservices_auth/internal/repository"
accessRepository "github.com/polshe-v/microservices_auth/internal/repository/access"
cacheRepository "github.com/polshe-v/microservices_auth/internal/repository/cache"
keyRepository "github.com/polshe-v/microservices_auth/internal/repository/key"
logRepository "github.com/polshe-v/microservices_auth/internal/repository/log"
userRepository "github.com/polshe-v/microservices_auth/internal/repository/user"
Expand Down Expand Up @@ -51,12 +52,15 @@ type serviceProvider struct {
keyRepository repository.KeyRepository
accessRepository repository.AccessRepository
logRepository repository.LogRepository
userService service.UserService
authService service.AuthService
accessService service.AccessService
userImpl *user.Implementation
authImpl *auth.Implementation
accessImpl *access.Implementation
cacheRepository repository.CacheRepository

userService service.UserService
authService service.AuthService
accessService service.AccessService

userImpl *user.Implementation
authImpl *auth.Implementation
accessImpl *access.Implementation

tokenOperations tokens.TokenOperations
}
Expand Down Expand Up @@ -196,7 +200,7 @@ func (s *serviceProvider) TxManager(ctx context.Context) db.TxManager {
return s.txManager
}

func (s *serviceProvider) RedisClient() cache.RedisClient {
func (s *serviceProvider) RedisClient() cache.Client {
if s.redisClient == nil {
s.redisClient = redis.NewClient(s.RedisPool(), s.RedisConfig().ConnectionTimeout())
}
Expand Down Expand Up @@ -246,9 +250,16 @@ func (s *serviceProvider) LogRepository(ctx context.Context) repository.LogRepos
return s.logRepository
}

func (s *serviceProvider) CacheRepository(ctx context.Context) repository.CacheRepository {
if s.cacheRepository == nil {
s.cacheRepository = cacheRepository.NewRepository(s.RedisClient())
}
return s.cacheRepository
}

func (s *serviceProvider) UserService(ctx context.Context) service.UserService {
if s.userService == nil {
s.userService = userService.NewService(s.UserRepository(ctx), s.LogRepository(ctx), s.TxManager(ctx))
s.userService = userService.NewService(s.UserRepository(ctx), s.CacheRepository(ctx), s.LogRepository(ctx), s.TxManager(ctx))
}
return s.userService
}
Expand Down
5 changes: 5 additions & 0 deletions internal/model/errors.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package model

import "github.com/pkg/errors"

var ErrorUserNotFound = errors.New("user not found")
29 changes: 29 additions & 0 deletions internal/repository/cache/converter/cache.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package converter

import (
"database/sql"
"time"

model "github.com/polshe-v/microservices_auth/internal/model"
modelRepo "github.com/polshe-v/microservices_auth/internal/repository/cache/model"
)

// ToUserFromRepo converts repository layer model to structure of service layer.
func ToUserFromRepo(user *modelRepo.User) *model.User {
var updatedAt sql.NullTime
if user.UpdatedAtNs != nil {
updatedAt = sql.NullTime{
Time: time.Unix(0, *user.UpdatedAtNs),
Valid: true,
}
}

return &model.User{
ID: user.ID,
Name: user.Name,
Email: user.Email,
Role: user.Role,
CreatedAt: time.Unix(0, user.CreatedAtNs),
UpdatedAt: updatedAt,
}
}
11 changes: 11 additions & 0 deletions internal/repository/cache/model/cache.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package model

// User type is the main structure for user.
type User struct {
ID int64 `redis:"id"`
Name string `redis:"name"`
Email string `redis:"email"`
Role string `redis:"role"`
CreatedAtNs int64 `redis:"created_at"`
UpdatedAtNs *int64 `redis:"updated_at"`
}
69 changes: 69 additions & 0 deletions internal/repository/cache/repository.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package cache

import (
"context"
"strconv"

redigo "github.com/gomodule/redigo/redis"

"github.com/polshe-v/microservices_auth/internal/model"
"github.com/polshe-v/microservices_auth/internal/repository"
"github.com/polshe-v/microservices_auth/internal/repository/cache/converter"
modelRepo "github.com/polshe-v/microservices_auth/internal/repository/cache/model"
"github.com/polshe-v/microservices_common/pkg/cache"
)

type repo struct {
client cache.Client
}

// NewRepository creates new object of repository layer.
func NewRepository(client cache.Client) repository.CacheRepository {
return &repo{client: client}
}

func (r *repo) CreateRecord(ctx context.Context, user *model.User) error {
var updatedAtNs *int64

if user.UpdatedAt.Valid {
updatedAt := user.UpdatedAt.Time.UnixNano()
updatedAtNs = &updatedAt
}

userRecord := modelRepo.User{
ID: user.ID,
Name: user.Name,
Email: user.Email,
Role: user.Role,
CreatedAtNs: user.CreatedAt.UnixNano(),
UpdatedAtNs: updatedAtNs,
}

idStr := strconv.FormatInt(user.ID, 10)
err := r.client.HSet(ctx, idStr, userRecord)
if err != nil {
return err
}

return nil
}

func (r *repo) GetRecord(ctx context.Context, id int64) (*model.User, error) {
idStr := strconv.FormatInt(id, 10)
values, err := r.client.HGetAll(ctx, idStr)
if err != nil {
return nil, err
}

if len(values) == 0 {
return nil, model.ErrorUserNotFound
}

var user modelRepo.User
err = redigo.ScanStruct(values, &user)
if err != nil {
return nil, err
}

return converter.ToUserFromRepo(&user), nil
}
1 change: 1 addition & 0 deletions internal/repository/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ package repository
//go:generate ./../../bin/minimock -g -i KeyRepository -o ./mocks/ -s "_minimock.go"
//go:generate ./../../bin/minimock -g -i AccessRepository -o ./mocks/ -s "_minimock.go"
//go:generate ./../../bin/minimock -g -i LogRepository -o ./mocks/ -s "_minimock.go"
//go:generate ./../../bin/minimock -g -i CacheRepository -o ./mocks/ -s "_minimock.go"
Loading

0 comments on commit 2c000e0

Please sign in to comment.