-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #272 from LerianStudio/feature/MIDAZ-256
Feature/MIDAZ-256
- Loading branch information
Showing
24 changed files
with
523 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package mredis | ||
|
||
import ( | ||
"context" | ||
"go.uber.org/zap" | ||
|
||
"github.com/LerianStudio/midaz/common/mlog" | ||
"github.com/redis/go-redis/v9" | ||
) | ||
|
||
const RedisTTL = 300 | ||
|
||
// RedisConnection is a hub which deal with redis connections. | ||
type RedisConnection struct { | ||
Addr string | ||
User string | ||
Password string | ||
DB int | ||
Protocol int | ||
Client *redis.Client | ||
Connected bool | ||
Logger mlog.Logger | ||
} | ||
|
||
// Connect keeps a singleton connection with redis. | ||
func (rc *RedisConnection) Connect(ctx context.Context) error { | ||
rc.Logger.Info("Connecting to redis...") | ||
|
||
rdb := redis.NewClient(&redis.Options{ | ||
Addr: rc.Addr, | ||
Password: rc.Password, | ||
DB: rc.DB, | ||
Protocol: rc.Protocol, | ||
}) | ||
|
||
_, err := rdb.Ping(ctx).Result() | ||
if err != nil { | ||
rc.Logger.Infof("RedisConnection.Ping %v", | ||
zap.Error(err)) | ||
|
||
return err | ||
} | ||
|
||
rc.Logger.Info("Connected to redis ✅ \n") | ||
|
||
rc.Connected = true | ||
|
||
rc.Client = rdb | ||
|
||
return nil | ||
} | ||
|
||
// GetClient returns a pointer to the redis connection, initializing it if necessary. | ||
func (rc *RedisConnection) GetClient(ctx context.Context) (*redis.Client, error) { | ||
if rc.Client == nil { | ||
err := rc.Connect(ctx) | ||
if err != nil { | ||
rc.Logger.Infof("ERRCONECT %s", err) | ||
return nil, err | ||
} | ||
} | ||
|
||
return rc.Client, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
65 changes: 65 additions & 0 deletions
65
components/ledger/internal/adapters/database/redis/consumer.redis.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
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.RedisTTL | ||
} | ||
|
||
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 15 additions & 0 deletions
15
components/ledger/internal/domain/redis/redis_repository.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
83 changes: 83 additions & 0 deletions
83
components/ledger/internal/gen/mock/redis/redis_repository_mock.go
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Oops, something went wrong.