Skip to content

Commit

Permalink
feat: implements redis on transaction ✨
Browse files Browse the repository at this point in the history
  • Loading branch information
MartinezAvellan committed Nov 18, 2024
1 parent 64db22f commit 7013ca2
Show file tree
Hide file tree
Showing 10 changed files with 222 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ package redis

import (
"context"
"github.com/LerianStudio/midaz/common"
"github.com/LerianStudio/midaz/common/mopentelemetry"
"time"

"github.com/LerianStudio/midaz/common"
"github.com/LerianStudio/midaz/common/mopentelemetry"
"github.com/LerianStudio/midaz/common/mredis"
)

Expand Down
3 changes: 3 additions & 0 deletions components/transaction/.env.example
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,10 @@ MONGO_PASSWORD=lerian
MONGO_PORT=5703

# REDIS
REDIS_HOST=redis
REDIS_PORT=5704
REDIS_USER=midaz
REDIS_PASSWORD=lerian

# LOG LEVEL
LOG_LEVEL=debug
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package redis

import (
"context"
"time"

"github.com/LerianStudio/midaz/common"
"github.com/LerianStudio/midaz/common/mopentelemetry"
"github.com/LerianStudio/midaz/common/mredis"
)

// RedisConsumerRepository is a Redis implementation of the Redis consumer.
type RedisConsumerRepository struct {
conn *mredis.RedisConnection
}

// NewConsumerRedis returns a new instance of RedisRepository using the given Redis connection.
func NewConsumerRedis(rc *mredis.RedisConnection) *RedisConsumerRepository {
r := &RedisConsumerRepository{
conn: rc,
}
if _, err := r.conn.GetClient(context.Background()); err != nil {
panic("Failed to connect on redis")
}

return r
}

func (rr *RedisConsumerRepository) Set(ctx context.Context, key, value string, ttl time.Duration) error {
logger := common.NewLoggerFromContext(ctx)
tracer := common.NewTracerFromContext(ctx)

ctx, span := tracer.Start(ctx, "redis.set")
defer span.End()

rds, err := rr.conn.GetClient(ctx)
if err != nil {
mopentelemetry.HandleSpanError(&span, "Failed to get redis", err)

return err
}

if ttl <= 0 {
ttl = mredis.REDIS_TTL
}
logger.Infof("value of ttl: %v", ttl)

statusCMD := rds.Set(ctx, key, value, ttl)
if statusCMD.Err() != nil {
mopentelemetry.HandleSpanError(&span, "Failed to set on redis", statusCMD.Err())

return statusCMD.Err()
}

return nil
}

func (rr *RedisConsumerRepository) Get(ctx context.Context, key string) error {

return nil
}

func (rr *RedisConsumerRepository) Del(ctx context.Context, key string) error {

return nil
}
4 changes: 4 additions & 0 deletions components/transaction/internal/app/command/command.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
m "github.com/LerianStudio/midaz/components/transaction/internal/domain/metadata"
o "github.com/LerianStudio/midaz/components/transaction/internal/domain/operation"
rmq "github.com/LerianStudio/midaz/components/transaction/internal/domain/rabbitmq"
rds "github.com/LerianStudio/midaz/components/transaction/internal/domain/redis"
t "github.com/LerianStudio/midaz/components/transaction/internal/domain/transaction"
)

Expand All @@ -28,4 +29,7 @@ type UseCase struct {

// RabbitMQRepo provides an abstraction on top of the producer rabbitmq.
RabbitMQRepo rmq.ProducerRepository

// RedisRepo provides an abstraction on top of the redis consumer.
RedisRepo rds.RedisRepository
}
4 changes: 4 additions & 0 deletions components/transaction/internal/app/query/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
m "github.com/LerianStudio/midaz/components/transaction/internal/domain/metadata"
o "github.com/LerianStudio/midaz/components/transaction/internal/domain/operation"
rmq "github.com/LerianStudio/midaz/components/transaction/internal/domain/rabbitmq"
rds "github.com/LerianStudio/midaz/components/transaction/internal/domain/redis"
t "github.com/LerianStudio/midaz/components/transaction/internal/domain/transaction"
)

Expand All @@ -28,4 +29,7 @@ type UseCase struct {

// RabbitMQRepo provides an abstraction on top of the consumer rabbitmq.
RabbitMQRepo rmq.ConsumerRepository

// RedisRepo provides an abstraction on top of the redis consumer.
RedisRepo rds.RedisRepository
}
15 changes: 15 additions & 0 deletions components/transaction/internal/domain/redis/redis_repository.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package redis

import (
"context"
"time"
)

// RedisRepository provides an interface for redis.
//
//go:generate mockgen --destination=../../gen/mock/redis/redis_repository_mock.go --package=mock . RedisRepository
type RedisRepository interface {
Set(ctx context.Context, key, value string, ttl time.Duration) error
Get(ctx context.Context, key string) error
Del(ctx context.Context, key string) error
}
19 changes: 19 additions & 0 deletions components/transaction/internal/gen/inject.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,11 @@ import (
"github.com/LerianStudio/midaz/common/mopentelemetry"
"github.com/LerianStudio/midaz/common/mpostgres"
"github.com/LerianStudio/midaz/common/mrabbitmq"
"github.com/LerianStudio/midaz/common/mredis"
"github.com/LerianStudio/midaz/common/mzap"
"github.com/LerianStudio/midaz/components/transaction/internal/adapters/database/mongodb"
"github.com/LerianStudio/midaz/components/transaction/internal/adapters/database/postgres"
"github.com/LerianStudio/midaz/components/transaction/internal/adapters/database/redis"
grpc "github.com/LerianStudio/midaz/components/transaction/internal/adapters/grpc"
rabbitmq "github.com/LerianStudio/midaz/components/transaction/internal/adapters/rabbitmq"
"github.com/LerianStudio/midaz/components/transaction/internal/app/command"
Expand All @@ -27,6 +29,7 @@ import (
m "github.com/LerianStudio/midaz/components/transaction/internal/domain/metadata"
o "github.com/LerianStudio/midaz/components/transaction/internal/domain/operation"
r "github.com/LerianStudio/midaz/components/transaction/internal/domain/rabbitmq"
rds "github.com/LerianStudio/midaz/components/transaction/internal/domain/redis"
t "github.com/LerianStudio/midaz/components/transaction/internal/domain/transaction"
httpHandler "github.com/LerianStudio/midaz/components/transaction/internal/ports/http"
"github.com/LerianStudio/midaz/components/transaction/internal/service"
Expand Down Expand Up @@ -118,6 +121,19 @@ func setupTelemetryProviders(cfg *service.Config) *mopentelemetry.Telemetry {
return t
}

func setupRedisConnection(cfg *service.Config, log mlog.Logger) *mredis.RedisConnection {
connStrSource := fmt.Sprintf("%s:%s", cfg.RedisHost, cfg.RedisPort)

return &mredis.RedisConnection{
Addr: connStrSource,
User: cfg.RedisUser,
Password: cfg.RedisPassword,
DB: 0,
Protocol: 3,
Logger: log,
}
}

var (
serviceSet = wire.NewSet(
common.InitLocalEnvConfig,
Expand All @@ -128,6 +144,7 @@ var (
setupCasdoorConnection,
setupGRPCConnection,
setupRabbitMQConnection,
setupRedisConnection,
service.NewConfig,
httpHandler.NewRouter,
service.NewServer,
Expand All @@ -138,6 +155,7 @@ var (
grpc.NewAccountGRPC,
rabbitmq.NewProducerRabbitMQ,
rabbitmq.NewConsumerRabbitMQ,
redis.NewConsumerRedis,
wire.Struct(new(httpHandler.TransactionHandler), "*"),
wire.Struct(new(httpHandler.OperationHandler), "*"),
wire.Struct(new(httpHandler.AssetRateHandler), "*"),
Expand All @@ -150,6 +168,7 @@ var (
wire.Bind(new(a.Repository), new(*grpc.AccountGRPCRepository)),
wire.Bind(new(r.ConsumerRepository), new(*rabbitmq.ConsumerRabbitMQRepository)),
wire.Bind(new(r.ProducerRepository), new(*rabbitmq.ProducerRabbitMQRepository)),
wire.Bind(new(rds.RedisRepository), new(*redis.RedisConsumerRepository)),
)

svcSet = wire.NewSet(
Expand Down

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

23 changes: 22 additions & 1 deletion components/transaction/internal/gen/wire_gen.go

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

4 changes: 4 additions & 0 deletions components/transaction/internal/service/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ type Config struct {
OtelServiceVersion string `env:"OTEL_RESOURCE_SERVICE_VERSION"`
OtelDeploymentEnv string `env:"OTEL_RESOURCE_DEPLOYMENT_ENVIRONMENT"`
OtelColExporterEndpoint string `env:"OTEL_EXPORTER_OTLP_ENDPOINT"`
RedisHost string `env:"REDIS_HOST"`
RedisPort string `env:"REDIS_PORT"`
RedisUser string `env:"REDIS_USER"`
RedisPassword string `env:"REDIS_PASSWORD"`
}

// NewConfig creates an instance of Config.
Expand Down

0 comments on commit 7013ca2

Please sign in to comment.